From bb170ff42b30dd44c2352bd2c93ad2974241721c Mon Sep 17 00:00:00 2001 From: onelin Date: Thu, 13 Mar 2025 18:16:42 +0100 Subject: Cap the FPS to fps target --- src/core/src/loop.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 1663219..db2033d 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -382,10 +382,16 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { render_present(p->window); const f64 rendertime_dt = get_time() - rendertime_begin; + /* Regulate FPS */ + f64 frame_end = get_time(); + const f64 fps_diff = fps_cap - (frame_end - now); + + if (fps_cap >= (frame_end - now)) { + delay(fps_diff); + } /* Print stats */ - const f64 now = get_time(); - const f64 dt_measurement = now - last_fps_measurement; + const f64 dt_measurement = frame_end - last_fps_measurement; /* only make measurements once a second */ if (dt_measurement > 1000.0) { @@ -400,7 +406,7 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { , rendertime_dt ); - last_fps_measurement = now; + last_fps_measurement = frame_end; last_fps_ticks = ticks; } } -- cgit v1.3