aboutsummaryrefslogtreecommitdiff
path: root/simloop/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2026-05-02 14:58:00 -0700
committer3gg <3gg@shellblade.net>2026-05-02 14:58:00 -0700
commitb03c941b8b0ab51227aa52d10616dbd47f75e5d9 (patch)
tree0dcfdf2396804814c0d5d818c460874ced4b30b2 /simloop/src
parent6482f3995baf9515158d999db925ef35158cfba5 (diff)
Handle large time spikesHEADmain
Diffstat (limited to 'simloop/src')
-rw-r--r--simloop/src/simloop.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/simloop/src/simloop.c b/simloop/src/simloop.c
index d231ab3..4fa5f62 100644
--- a/simloop/src/simloop.c
+++ b/simloop/src/simloop.c
@@ -31,15 +31,23 @@ void simloop_update(Simloop* sim, simloop_time_t dt, SimloopOut* out) {
31 sim->clock += dt; 31 sim->clock += dt;
32 32
33 // Simulation update. 33 // Simulation update.
34 // If the update falls behind the clock, we advance by a single ddt increment 34 // If the simulation falls behind the clock, we advance by a single ddt
35 // per loop iteration here and give it a chance to catch up over subsequent 35 // increment per loop iteration here and give it a chance to catch up over
36 // iterations. 36 // subsequent iterations.
37 // This has the implication that percent_frame can fall out of range (>1) if 37 // This has the implication that percent_frame can fall out of range (>1) if
38 // we are not careful with how it is defined. See the general update function 38 // we are not careful with how it is defined. See the logic below.
39 // below. 39 // If the delta is too large, then we simply warp the simulation to the wall
40 const simloop_time_t delta = sim->clock - sim->update.time; 40 // clock. This avoids the appearance of the simulation playing in fast-forward
41 const bool update_this_tick = delta >= sim->update.ddt; 41 // as it tries to catch up. Large time spikes can typically occur at the start
42 sim->update.time += update_this_tick ? sim->update.ddt : 0; 42 // of the simulation when the application loads assets, compiles shaders, etc.
43 static const uint64_t max_catchup_frames = 10;
44 const simloop_time_t delta = sim->clock - sim->update.time;
45 const uint64_t delta_frames = delta / sim->update.ddt;
46 const bool update_this_tick = delta >= sim->update.ddt;
47 const bool warp = delta_frames > max_catchup_frames;
48 sim->update.time =
49 warp ? sim->clock
50 : (sim->update.time + (update_this_tick ? sim->update.ddt : 0));
43 51
44 // Loop-state update. 52 // Loop-state update.
45 sim->frame += (update_this_tick ? 1 : 0); 53 sim->frame += (update_this_tick ? 1 : 0);