From 951d9c76c85b6b1830e5745b1d98ef5e12877947 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 10 Jan 2025 02:20:21 +0300 Subject: [PATCH] use floats for ctx.frame_number and ctx.random_seed --- include/twn_context.h | 6 +++--- src/game_object/twn_linux_game_object.c | 4 ++-- src/rendering/twn_gl_15_rendering.c | 2 +- src/twn_loop.c | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/twn_context.h b/include/twn_context.h index 8902982..f78b3d1 100644 --- a/include/twn_context.h +++ b/include/twn_context.h @@ -16,10 +16,10 @@ typedef struct Context { void *udata; /* which frame is it, starting from 0 at startup */ - uint64_t frame_number; + float frame_number; /* real time spent on one frame (in seconds) */ - /* townengine is fixed step based, so you don't have */ + /* townengine is fixed step based, so you don't have to use delta */ /* TODO: actually set it */ float frame_duration; @@ -30,7 +30,7 @@ typedef struct Context { Vec2 mouse_movement; /* is set on startup, should be used as source of randomness */ - uint64_t random_seed; + float random_seed; /* whether debugging logic should be enabled in user code */ bool debug; diff --git a/src/game_object/twn_linux_game_object.c b/src/game_object/twn_linux_game_object.c index 1fadfdc..b6ad120 100644 --- a/src/game_object/twn_linux_game_object.c +++ b/src/game_object/twn_linux_game_object.c @@ -19,7 +19,7 @@ static void (*game_end_callback)(void); static x_watcher *watcher; static void *handle = NULL; -static uint64_t last_tick_modified; +static float last_tick_modified; static bool loaded_after_modification = true; static SDL_mutex *lock; @@ -59,7 +59,7 @@ static void load_game_object(void) { handle = new_handle; - if (ctx.game.frame_number != 0) + if (fabsf(0.0f - ctx.game.frame_number) > 0.00001f) log_info("Game object was reloaded\n"); return; diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index 4bd9a52..9be0304 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -20,7 +20,7 @@ void start_render_frame(void) { void end_render_frame(void) { - if (!ctx.render_double_buffered || ctx.game.frame_number == 1) { + if (!ctx.render_double_buffered || (fabsf(1.0f - ctx.game.frame_number) < 0.00001f)) { issue_deferred_draw_commands(); SDL_GL_SwapWindow(ctx.window); arrsetlen(deferred_commands, 0); diff --git a/src/twn_loop.c b/src/twn_loop.c index 0c194ba..28f30d7 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -236,7 +236,7 @@ static void main_loop(void) { preserve_persistent_ctx_fields(); ctx.frame_accumulator -= ctx.desired_frametime; - ctx.game.frame_number = (ctx.game.frame_number % ULLONG_MAX) + 1; + ctx.game.frame_number++; ctx.game.initialization_needed = false; } } @@ -584,9 +584,9 @@ static bool initialize(void) { /* random seeding */ /* SDL_GetPerformanceCounter returns some platform-dependent number. */ /* it should vary between game instances. i checked! random enough for me. */ - ctx.game.random_seed = SDL_GetPerformanceCounter(); + ctx.game.random_seed = (float)(SDL_GetPerformanceCounter() % 16777216); srand((unsigned int)ctx.game.random_seed); - stbds_rand_seed(ctx.game.random_seed); + stbds_rand_seed((size_t)ctx.game.random_seed); /* main loop machinery */ toml_datum_t datum_ticks_per_second = toml_int_in(engine, "ticks_per_second");