diff --git a/apps/demos/bunnymark/game.c b/apps/demos/bunnymark/game.c index c19e6e8..2241ebf 100644 --- a/apps/demos/bunnymark/game.c +++ b/apps/demos/bunnymark/game.c @@ -66,7 +66,7 @@ void game_tick(void) { // First tick, initalizing data // Allocating State struct to store data there if (!ctx.udata) - ctx.udata = ccalloc(1, sizeof(State)); + ctx.udata = calloc(1, sizeof(State)); } input_action("add_a_bit", CONTROL_LEFT_MOUSE); diff --git a/apps/demos/platformer/game.c b/apps/demos/platformer/game.c index 5d5ba37..4e203a1 100644 --- a/apps/demos/platformer/game.c +++ b/apps/demos/platformer/game.c @@ -12,7 +12,7 @@ void game_tick(void) { if (ctx.initialization_needed) { if (!ctx.udata) { - ctx.udata = ccalloc(1, sizeof (State)); + ctx.udata = calloc(1, sizeof (State)); State *state = ctx.udata; state->ctx = &ctx; diff --git a/apps/demos/platformer/player.c b/apps/demos/platformer/player.c index 93f95c0..f5e70d1 100644 --- a/apps/demos/platformer/player.c +++ b/apps/demos/platformer/player.c @@ -203,7 +203,7 @@ static void calc_collisions_y(Player *player) { Player *player_create(World *world) { - Player *player = cmalloc(sizeof *player); + Player *player = malloc(sizeof *player); *player = (Player) { .world = world, diff --git a/apps/demos/platformer/scenes/ingame.c b/apps/demos/platformer/scenes/ingame.c index b110e9b..1498c1e 100644 --- a/apps/demos/platformer/scenes/ingame.c +++ b/apps/demos/platformer/scenes/ingame.c @@ -34,7 +34,7 @@ static void ingame_end(State *state) { Scene *ingame_scene(State *state) { (void)state; - SceneIngame *new_scene = ccalloc(1, sizeof *new_scene); + SceneIngame *new_scene = calloc(1, sizeof *new_scene); new_scene->base.tick = ingame_tick; new_scene->base.end = ingame_end; diff --git a/apps/demos/platformer/scenes/title.c b/apps/demos/platformer/scenes/title.c index 52bee62..9f09a21 100644 --- a/apps/demos/platformer/scenes/title.c +++ b/apps/demos/platformer/scenes/title.c @@ -5,9 +5,8 @@ #include "twn_game_api.h" -#include - #include +#include static void title_tick(State *state) { @@ -26,8 +25,9 @@ static void title_tick(State *state) { /* draw the tick count as an example of dynamic text */ size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1; - char *text_str = cmalloc(text_str_len); + char *text_str = malloc(text_str_len); snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number); + free(text_str); const char *font = "fonts/kenney-pixel.ttf"; float text_h = 32; @@ -64,7 +64,7 @@ static void title_end(State *state) { Scene *title_scene(State *state) { (void)state; - SceneTitle *new_scene = ccalloc(1, sizeof *new_scene); + SceneTitle *new_scene = calloc(1, sizeof *new_scene); new_scene->base.tick = title_tick; new_scene->base.end = title_end; diff --git a/apps/demos/platformer/world.c b/apps/demos/platformer/world.c index facc516..b7bf899 100644 --- a/apps/demos/platformer/world.c +++ b/apps/demos/platformer/world.c @@ -45,7 +45,7 @@ static void drawdef_debug(struct World *world) { struct World *world_create(void) { - struct World *world = cmalloc(sizeof *world); + struct World *world = malloc(sizeof *world); *world = (struct World) { .tiles = NULL, @@ -58,9 +58,9 @@ struct World *world_create(void) { /* create the tilemap */ /* it simply stores what's in each tile as a 2d array */ /* on its own, it's entirely unrelated to drawing or logic */ - world->tilemap = cmalloc(sizeof *world->tilemap * world->tilemap_height); + world->tilemap = malloc(sizeof *world->tilemap * world->tilemap_height); for (size_t i = 0; i < world->tilemap_height; ++i) { - world->tilemap[i] = cmalloc(sizeof **world->tilemap * world->tilemap_width); + world->tilemap[i] = malloc(sizeof **world->tilemap * world->tilemap_width); for (size_t j = 0; j < world->tilemap_width; ++j) { world->tilemap[i][j] = TILE_TYPE_VOID; @@ -81,7 +81,7 @@ struct World *world_create(void) { /* the tiles array contains data meant to be used by other logic */ /* most importantly, it is used to draw the tiles */ const size_t tile_count = world->tilemap_height * world->tilemap_width; - world->tiles = cmalloc(sizeof *world->tiles * tile_count); + world->tiles = malloc(sizeof *world->tiles * tile_count); update_tiles(world); return world; diff --git a/apps/demos/scenery/game.c b/apps/demos/scenery/game.c index d2454ec..520db4a 100644 --- a/apps/demos/scenery/game.c +++ b/apps/demos/scenery/game.c @@ -14,11 +14,11 @@ void game_tick(void) { if (ctx.initialization_needed) { if (!ctx.udata) { - ctx.udata = ccalloc(1, sizeof (State)); + ctx.udata = calloc(1, sizeof (State)); State *state = ctx.udata; state->ctx = &ctx; - state->scene = ingame_scene(state); + state->scene = title_scene(state); } } diff --git a/apps/demos/scenery/scenes/ingame.c b/apps/demos/scenery/scenes/ingame.c index ee41de9..23dabac 100644 --- a/apps/demos/scenery/scenes/ingame.c +++ b/apps/demos/scenery/scenes/ingame.c @@ -13,7 +13,7 @@ #define TERRAIN_FREQUENCY 0.15f -#define TERRAIN_DISTANCE 100 +#define TERRAIN_DISTANCE 64 #define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2) #define PLAYER_HEIGHT 0.6f @@ -237,7 +237,7 @@ static void ingame_end(State *state) { Scene *ingame_scene(State *state) { (void)state; - SceneIngame *new_scene = ccalloc(1, sizeof *new_scene); + SceneIngame *new_scene = calloc(1, sizeof *new_scene); new_scene->base.tick = ingame_tick; new_scene->base.end = ingame_end; diff --git a/apps/demos/scenery/scenes/title.c b/apps/demos/scenery/scenes/title.c index b0c4c0c..4ef7264 100644 --- a/apps/demos/scenery/scenes/title.c +++ b/apps/demos/scenery/scenes/title.c @@ -23,7 +23,7 @@ static void title_tick(State *state) { /* draw the tick count as an example of dynamic text */ size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1; - char *text_str = cmalloc(text_str_len); + char *text_str = malloc(text_str_len); snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number); const char *font = "/fonts/kenney-pixel.ttf"; @@ -34,8 +34,8 @@ static void title_tick(State *state) { (Rect) { .x = 0, .y = 0, - .w = text_w, - .h = text_h, + .w = (float)text_w, + .h = (float)text_h, }, (Color) { 0, 0, 0, 255 } ); @@ -53,7 +53,7 @@ static void title_end(State *state) { Scene *title_scene(State *state) { (void)state; - SceneTitle *new_scene = ccalloc(1, sizeof *new_scene); + SceneTitle *new_scene = calloc(1, sizeof *new_scene); new_scene->base.tick = title_tick; new_scene->base.end = title_end; diff --git a/include/twn_util.h b/include/twn_util.h index a528300..9c8b12a 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -18,12 +18,9 @@ #define DEG2RAD (M_PI / 180) #define RAD2DEG (180 / M_PI) +/* TODO: shouldn't be a thing */ #ifndef TWN_NOT_C - TWN_API void *cmalloc(size_t size); - TWN_API void *crealloc(void *ptr, size_t size); - TWN_API void *ccalloc(size_t num, size_t size); - TWN_API void log_info(const char *restrict format, ...); TWN_API void log_critical(const char *restrict format, ...); TWN_API void log_warn(const char *restrict format, ...); diff --git a/src/rendering/twn_deferred_commands.h b/src/rendering/twn_deferred_commands.h index 1c553b2..5e8429e 100644 --- a/src/rendering/twn_deferred_commands.h +++ b/src/rendering/twn_deferred_commands.h @@ -43,7 +43,7 @@ typedef struct { TextureKey texture_key; GPUTexture gpu_texture; - /* could be either `element_count` with supplied `element_buffer`, or this, but not both */ + /* could be either `element_count` with supplied `element_buffer`, or `primitive_count`, but not both */ uint32_t primitive_count; uint32_t element_buffer; uint32_t element_count; diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index b16af82..1c839d9 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -229,7 +229,8 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) { void finish_vertex_builder(VertexBufferBuilder *builder) { - glUnmapBuffer(GL_ARRAY_BUFFER); + if (!glUnmapBuffer(GL_ARRAY_BUFFER)) + CRY("finish_vertex_builder", "Error unmapping a vertex array buffer"); glBindBuffer(GL_ARRAY_BUFFER, 0); builder->base = 0; diff --git a/src/rendering/twn_gl_any_rendering.c b/src/rendering/twn_gl_any_rendering.c index 9c1f771..8d08831 100644 --- a/src/rendering/twn_gl_any_rendering.c +++ b/src/rendering/twn_gl_any_rendering.c @@ -31,6 +31,7 @@ void delete_vertex_buffer(VertexBuffer buffer) { void specify_vertex_buffer(VertexBuffer buffer, void const *data, size_t bytes) { glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, bytes, data, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); } diff --git a/src/rendering/twn_rects.c b/src/rendering/twn_rects.c index b82797d..cc79669 100644 --- a/src/rendering/twn_rects.c +++ b/src/rendering/twn_rects.c @@ -90,6 +90,8 @@ void render_rect_batch(const Primitive2D primitives[], (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, rect.color); } + + finish_vertex_builder(&payload); } finally_render_quads(primitives, batch, vertex_array); diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index 750d987..a0a00b8 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -244,6 +244,8 @@ void render_sprite_batch(const Primitive2D primitives[], push_quad_payload_to_vertex_buffer_builder(batch, i, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color); } + + finish_vertex_builder(&payload); } finally_render_quads(primitives, batch, vertex_array); diff --git a/src/twn_util_c.h b/src/twn_util_c.h index c867ea3..8c13c63 100644 --- a/src/twn_util_c.h +++ b/src/twn_util_c.h @@ -11,6 +11,10 @@ #define MAX SDL_max #define MIN SDL_min +void *cmalloc(size_t size); +void *crealloc(void *ptr, size_t size); +void *ccalloc(size_t num, size_t size); + void cry_impl(const char *file, const int line, const char *title, const char *text); #define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text) #define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())