From e5fe20ad83e01b3c8262ac90af984da47d8a16a1 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 12 Apr 2026 10:48:04 -0700 Subject: Work in terms of time deltas; remove dependency on timer module --- simloop/src/simloop.c | 56 +++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'simloop/src') diff --git a/simloop/src/simloop.c b/simloop/src/simloop.c index 606f5ed..bd5a72d 100644 --- a/simloop/src/simloop.c +++ b/simloop/src/simloop.c @@ -2,9 +2,9 @@ #include -static uint64_t ddt_from_fps(int fps) { +static simloop_time_t ddt_from_fps(int fps) { static constexpr double NANOSECONDS = 1e9; - return (fps == 0) ? 0 : (uint64_t)(NANOSECONDS / (double)fps); + return (fps == 0) ? 0 : (simloop_time_t)(NANOSECONDS / (double)fps); } Simloop simloop_make(const SimloopArgs* args) { @@ -12,31 +12,30 @@ Simloop simloop_make(const SimloopArgs* args) { assert(args->update_fps > 0); return (Simloop){ - .frame = 0, - .update = (SimloopTimeline){.ddt = ddt_from_fps(args->update_fps), - .last_step = args->timer->start_time}, - .render = (SimloopTimeline){ .ddt = ddt_from_fps(args->max_render_fps), - .last_step = args->timer->start_time}, - .timer = args->timer, - .first_iter = true, + .frame = 0, + .update = + (SimloopTimeline){ + .ddt = ddt_from_fps(args->update_fps), + .time = 0, + }, + .render = + (SimloopTimeline){ + .ddt = ddt_from_fps(args->max_render_fps), + .time = 0, + }, + .first_iter = true, .updates_since_last_render = false, }; } -static time_delta time_elapsed(const Simloop* sim, time_point t) { - assert(sim); - return time_diff(sim->timer->start_time, t); -} - static bool step_update(const Simloop* sim, SimloopTimeline* timeline) { assert(sim); assert(timeline); assert(timeline->ddt > 0); - const time_delta dt = time_diff(timeline->last_step, sim->timer->last_tick); - const bool should_step = dt >= timeline->ddt; - timeline->last_step = - should_step ? time_add(timeline->last_step, dt) : timeline->last_step; + const simloop_time_t dt = sim->clock - timeline->time; + const bool should_step = dt >= timeline->ddt; + timeline->time += should_step ? timeline->ddt : 0; return should_step; } @@ -48,32 +47,37 @@ static bool step_render(const Simloop* sim, SimloopTimeline* timeline) { if (timeline->ddt > 0) { render = step_update(sim, timeline); } else { - timeline->last_step = sim->timer->last_tick; - render = true; + timeline->time = sim->clock; + render = true; } return render; } -void simloop_update(Simloop* sim, SimloopOut* out) { +void simloop_update(Simloop* sim, simloop_time_t dt, SimloopOut* out) { assert(sim); assert(out); + sim->clock += dt; + // Simulation update. const bool updated = step_update(sim, &sim->update); - out->should_update = updated; sim->updates_since_last_render = sim->updates_since_last_render || updated; + // Simulation render. const bool rendered = (sim->updates_since_last_render && step_render(sim, &sim->render)) || (sim->first_iter); // Trigger an initial render on the first frame. - out->should_render = rendered; sim->updates_since_last_render = sim->updates_since_last_render && !out->should_render; + // Loop state update. sim->frame += (updated ? 1 : 0); - sim->first_iter = false; + sim->first_iter = false; + out->frame = sim->frame; - out->render_elapsed = time_elapsed(sim, sim->render.last_step); - out->update_elapsed = time_elapsed(sim, sim->update.last_step); + out->render_elapsed = sim->render.time; + out->update_elapsed = sim->update.time; out->update_dt = sim->update.ddt; + out->should_update = updated; + out->should_render = rendered; } -- cgit v1.2.3