From 7074e7499a3e8dc17d2dee20e9e0642faf382ab3 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 30 Jan 2025 05:10:38 +0300 Subject: [PATCH 1/2] ugh? --- CMakeLists.txt | 2 +- src/rendering/twn_draw.c | 4 ++-- src/twn_filewatch.c | 7 +++++++ src/twn_timer.c | 3 ++- src/twn_timer_c.h | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30b79b6..78e7c57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,7 +210,7 @@ function(give_options_without_warnings target) target_compile_definitions(${target} PRIVATE $<$:TWN_FEATURE_DYNLIB_GAME> - $<$:_GNU_SOURCE>) + _GNU_SOURCE) endfunction() diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index 3f528bb..2f06d3c 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -415,7 +415,7 @@ void render(void) { void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom) { bool const orthographic = fabsf(0.0f - fov) < 0.00001f; - if (!orthographic && fov >= M_PIf) + if (!orthographic && fov >= (float)(M_PI)) log_warn("Invalid fov given (%f)", (double)fov); Camera const camera = { @@ -447,7 +447,7 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position, float zoom) { bool const orthographic = fabsf(0.0f - fov) < 0.00001f; - if (!orthographic && fov >= M_PIf) + if (!orthographic && fov >= (float)(M_PI)) log_warn("Invalid fov given (%f)", (double)fov); (void)roll; diff --git a/src/twn_filewatch.c b/src/twn_filewatch.c index e24490b..09a24ef 100644 --- a/src/twn_filewatch.c +++ b/src/twn_filewatch.c @@ -29,6 +29,8 @@ static void filewatch_callback(dmon_watch_id watch_id, const char* oldfilepath, void* user) { + (void)watch_id; (void)rootdir; (void)filepath; (void)oldfilepath; + enum FilewatchAction faction; switch (action) { @@ -41,6 +43,7 @@ static void filewatch_callback(dmon_watch_id watch_id, case DMON_ACTION_MODIFY: faction = FILEWATCH_ACTION_FILE_MODIFIED; break; + case DMON_ACTION_MOVE: default: return; } @@ -74,6 +77,8 @@ bool filewatch_add_directory(char const *dir, FileatchCallback callback) { }; arrpush(filewatch_directories, w); dmon_watch(dir, filewatch_callback, DMON_WATCHFLAGS_RECURSIVE, (void *)(intptr_t)(arrlen(filewatch_directories) - 1)); + + return true; } @@ -91,6 +96,8 @@ bool filewatch_add_file(char const *filepath, FileatchCallback callback) { }; arrpush(filewatch_files, f); dmon_watch("./", filewatch_callback, 0, (void *)(intptr_t)(-arrlen(filewatch_files))); + + return true; } diff --git a/src/twn_timer.c b/src/twn_timer.c index 1e14312..ac3716e 100644 --- a/src/twn_timer.c +++ b/src/twn_timer.c @@ -2,6 +2,7 @@ #include "twn_engine_context_c.h" #include +#include static SDL_TimerID sanity_timer; @@ -19,7 +20,7 @@ static uint32_t sanity_timer_handler(uint32_t interval, void *data) { /* TODO: figure out the most portable way to do it */ /* TODO: different type of behavior is possible, especially for debugging */ - quick_exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } diff --git a/src/twn_timer_c.h b/src/twn_timer_c.h index 292a8ad..b0e96f7 100644 --- a/src/twn_timer_c.h +++ b/src/twn_timer_c.h @@ -2,7 +2,7 @@ #define TWN_TIMER_H #include -#include +#include bool start_sanity_timer(uint32_t milliseconds_to_expire); bool end_sanity_timer(void); From bd89c4b938a796111ac5ba22ec46111a4f397925 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 30 Jan 2025 21:57:20 +0300 Subject: [PATCH 2/2] fix mixing up of SDL and libc allocators, proper flushing of quad build buffers --- apps/demos/bunnymark/game.c | 2 +- apps/demos/platformer/game.c | 2 +- apps/demos/platformer/player.c | 2 +- apps/demos/platformer/scenes/ingame.c | 2 +- apps/demos/platformer/scenes/title.c | 8 ++++---- apps/demos/platformer/world.c | 8 ++++---- apps/demos/scenery/game.c | 4 ++-- apps/demos/scenery/scenes/ingame.c | 4 ++-- apps/demos/scenery/scenes/title.c | 8 ++++---- include/twn_util.h | 5 +---- src/rendering/twn_deferred_commands.h | 2 +- src/rendering/twn_gl_15_rendering.c | 3 ++- src/rendering/twn_gl_any_rendering.c | 1 + src/rendering/twn_rects.c | 2 ++ src/rendering/twn_sprites.c | 2 ++ src/twn_util_c.h | 4 ++++ 16 files changed, 33 insertions(+), 26 deletions(-) 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())