From 456d7f883ca34ec83692ff3879ca5f5717e82b33 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 2 May 2026 14:59:18 -0700 Subject: Use the simloop library --- app/src/app.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'app/src') diff --git a/app/src/app.c b/app/src/app.c index 8262378..c056ddd 100644 --- a/app/src/app.c +++ b/app/src/app.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -13,9 +14,8 @@ typedef struct GfxApp { GfxAppState* app_state; GfxAppCallbacks callbacks; - int max_fps; - double update_delta_time; GLFWwindow* window; + Simloop simloop; } GfxApp; /// Storing the application state in a global variable so that we can call the @@ -32,17 +32,9 @@ static void loop(GfxApp* app) { assert(app); assert(app->window); - const time_delta min_frame_time = - app->max_fps > 0 ? time_delta_from_sec(1.0 / (double)(app->max_fps)) : 0; - const time_delta update_dt = time_delta_from_sec(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->app_state, time_delta_to_sec(time), - time_delta_to_sec(update_dt)); + time_delta time = 0; + Timer timer = timer_make(); + SimloopOut simout = {}; // Warm up the rendering before entering the main loop. A renderer can // compile shaders and do other initialization the first time it renders a @@ -53,25 +45,21 @@ static void loop(GfxApp* app) { timer_start(&timer); while (!glfwWindowShouldClose(app->window)) { timer_tick(&timer); - time_budget += timer.delta_time; + simloop_update(&app->simloop, timer.delta_time, &simout); + + glfwPollEvents(); - while (time_budget >= update_dt) { + if (simout.should_update) { (*app->callbacks.update)( app, app->app_state, time_delta_to_sec(time), - time_delta_to_sec(update_dt)); - - time += update_dt; - time_budget -= update_dt; + time_delta_to_sec(simout.update_dt)); } (*app->callbacks.render)(app, app->app_state); glfwSwapBuffers(app->window); - glfwPollEvents(); - const time_point frame_end = time_now(); - const time_delta frame_time = time_diff(timer.last_tick, frame_end); - if ((min_frame_time > 0) && (frame_time < min_frame_time)) { - time_sleep(min_frame_time - frame_time); + if (simout.throttle > 0) { + time_sleep(simout.throttle); } } } @@ -82,11 +70,9 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { bool success = false; - g_gfx_app.app_state = desc->app_state; - g_gfx_app.callbacks = *callbacks; - g_gfx_app.max_fps = desc->max_fps; - g_gfx_app.update_delta_time = desc->update_delta_time; - g_gfx_app.window = nullptr; + g_gfx_app.app_state = desc->app_state; + g_gfx_app.callbacks = *callbacks; + g_gfx_app.window = nullptr; if (!glfwInit()) { LOGE("glfwInit() failed"); @@ -138,6 +124,8 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { // Set GLFW callbacks now that the application has been initialized. glfwSetWindowSizeCallback(g_gfx_app.window, on_resize); + g_gfx_app.simloop = simloop_make(&(SimloopArgs){ + .update_fps = desc->update_fps, .max_render_fps = desc->max_render_fps}); loop(&g_gfx_app); (*g_gfx_app.callbacks.shutdown)(&g_gfx_app, g_gfx_app.app_state); -- cgit v1.2.3