From 02ec7cd07213e267fda7e1e67b62f55e92a2f32c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 20 Jan 2024 15:48:02 -0800 Subject: Use time_delta instead of double to track time. --- gfx-app/src/gfx_app.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'gfx-app') diff --git a/gfx-app/src/gfx_app.c b/gfx-app/src/gfx_app.c index 2d54e3d..a313ce8 100644 --- a/gfx-app/src/gfx_app.c +++ b/gfx-app/src/gfx_app.c @@ -30,15 +30,16 @@ static void loop(GfxApp* app) { assert(app); assert(app->window); - const double min_frame_time = - app->max_fps > 0 ? 1.0 / (double)(app->max_fps) : 0.0; - const double update_dt = app->update_delta_time; - double time = 0.0; - double time_budget = 0.0; - Timer timer = timer_make(); + const time_delta min_frame_time = + app->max_fps > 0 ? sec_to_time_delta(1.0 / (double)(app->max_fps)) : 0; + const time_delta update_dt = sec_to_time_delta(app->update_delta_time); + time_delta time = 0; + time_delta time_budget = 0; + Timer timer = timer_make(); // Warm up the update to initialize the application's state. - (*app->callbacks.update)(app->app_state, time, update_dt); + (*app->callbacks.update)( + app->app_state, time_delta_to_sec(time), time_delta_to_sec(update_dt)); // Warm up the rendering before entering the main loop. A renderer can // compile shaders and do other initialization the first time it renders a @@ -49,10 +50,13 @@ static void loop(GfxApp* app) { timer_start(&timer); while (!glfwWindowShouldClose(app->window)) { timer_tick(&timer); - time_budget += time_delta_to_sec(timer.delta_time); + time_budget += timer.delta_time; while (time_budget >= update_dt) { - (*app->callbacks.update)(app->app_state, time, update_dt); + (*app->callbacks.update)( + app->app_state, time_delta_to_sec(time), + time_delta_to_sec(update_dt)); + time += update_dt; time_budget -= update_dt; } @@ -63,7 +67,7 @@ static void loop(GfxApp* app) { const time_point frame_end = time_now(); const time_delta frame_time = time_diff(timer.last_tick, frame_end); - if (min_frame_time > 0.0 && frame_time < min_frame_time) { + if ((min_frame_time > 0) && (frame_time < min_frame_time)) { time_sleep(min_frame_time - frame_time); } } -- cgit v1.2.3