aboutsummaryrefslogtreecommitdiff
path: root/simloop/src
diff options
context:
space:
mode:
Diffstat (limited to 'simloop/src')
-rw-r--r--simloop/src/simloop.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/simloop/src/simloop.c b/simloop/src/simloop.c
index 11f4d6d..aa2b6b7 100644
--- a/simloop/src/simloop.c
+++ b/simloop/src/simloop.c
@@ -12,12 +12,13 @@ Simloop simloop_make(const SimloopArgs* args) {
12 assert(args->update_fps > 0); 12 assert(args->update_fps > 0);
13 13
14 return (Simloop){ 14 return (Simloop){
15 .frame = 0, 15 .frame = 0,
16 .update = (SimloopTimeline){.ddt = ddt_from_fps(args->update_fps), 16 .update = (SimloopTimeline){.ddt = ddt_from_fps(args->update_fps),
17 .last_step = args->timer->start_time}, 17 .last_step = args->timer->start_time},
18 .render = (SimloopTimeline){ .ddt = ddt_from_fps(args->max_render_fps), 18 .render = (SimloopTimeline){ .ddt = ddt_from_fps(args->max_render_fps),
19 .last_step = args->timer->start_time}, 19 .last_step = args->timer->start_time},
20 .timer = args->timer, 20 .timer = args->timer,
21 .first_iter = true,
21 }; 22 };
22} 23}
23 24
@@ -26,15 +27,16 @@ static time_delta time_elapsed(const Simloop* sim, time_point t) {
26 return time_diff(sim->timer->start_time, t); 27 return time_diff(sim->timer->start_time, t);
27} 28}
28 29
29static int step_update(const Simloop* sim, SimloopTimeline* timeline) { 30static bool step_update(const Simloop* sim, SimloopTimeline* timeline) {
30 assert(sim); 31 assert(sim);
31 assert(timeline); 32 assert(timeline);
32 assert(timeline->ddt > 0); 33 assert(timeline->ddt > 0);
33 34
34 const time_delta dt = time_diff(timeline->last_step, sim->timer->last_tick); 35 const time_delta dt = time_diff(timeline->last_step, sim->timer->last_tick);
35 const time_delta steps = dt / timeline->ddt; 36 const bool should_step = dt >= timeline->ddt;
36 timeline->last_step = time_add(timeline->last_step, dt); 37 timeline->last_step =
37 return (int)steps; 38 should_step ? time_add(timeline->last_step, dt) : timeline->last_step;
39 return should_step;
38} 40}
39 41
40static bool step_render(const Simloop* sim, SimloopTimeline* timeline) { 42static bool step_render(const Simloop* sim, SimloopTimeline* timeline) {
@@ -43,7 +45,7 @@ static bool step_render(const Simloop* sim, SimloopTimeline* timeline) {
43 45
44 bool render = false; 46 bool render = false;
45 if (timeline->ddt > 0) { 47 if (timeline->ddt > 0) {
46 render = step_update(sim, timeline) > 0; 48 render = step_update(sim, timeline);
47 } else { 49 } else {
48 timeline->last_step = sim->timer->last_tick; 50 timeline->last_step = sim->timer->last_tick;
49 render = true; 51 render = true;
@@ -55,12 +57,12 @@ void simloop_update(Simloop* sim, SimloopOut* out) {
55 assert(sim); 57 assert(sim);
56 assert(out); 58 assert(out);
57 59
58 const int new_frames = step_update(sim, &sim->update); 60 out->should_update = step_update(sim, &sim->update);
59 out->updates_pending = new_frames;
60 out->should_render = 61 out->should_render =
61 step_render(sim, &sim->render) || 62 step_render(sim, &sim->render) ||
62 (sim->frame == 0); // Trigger an initial render on the first frame. 63 (sim->first_iter); // Trigger an initial render on the first frame.
63 sim->frame += new_frames; 64 sim->frame += (out->should_update ? 1 : 0);
65 sim->first_iter = false;
64 out->frame = sim->frame; 66 out->frame = sim->frame;
65 out->render_elapsed = time_elapsed(sim, sim->render.last_step); 67 out->render_elapsed = time_elapsed(sim, sim->render.last_step);
66 out->update_elapsed = time_elapsed(sim, sim->update.last_step); 68 out->update_elapsed = time_elapsed(sim, sim->update.last_step);