From 5458f3db7b08bf06c1f5c99a5851f6b270126b92 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 5 Apr 2026 14:48:01 -0700 Subject: Initial simloop module --- simloop/include/simloop.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 simloop/include/simloop.h (limited to 'simloop/include') diff --git a/simloop/include/simloop.h b/simloop/include/simloop.h new file mode 100644 index 0000000..f267d40 --- /dev/null +++ b/simloop/include/simloop.h @@ -0,0 +1,44 @@ +/* Simulation loop module. + * + * This implements a simulation loop but in a way that the client retains + * control flow. The client steps the loop and then checks whether the + * simulation must be updated and/or the result rendered. + */ +#pragma once + +#include + +#include + +typedef struct SimloopArgs { + int update_fps; ///< Update frame rate. Must be >0. + int max_render_fps; ///< Render frame rate cap. 0 to disable. + Timer* timer; ///< Timer that drives the simulation. +} SimloopArgs; + +typedef struct SimloopOut { + uint64_t frame; ///< Frame counter. + time_delta render_elapsed; ///< Amount of time elapsed in the rendering. + time_delta update_elapsed; ///< Amount of time elapsed in the simulation. + time_delta update_dt; ///< Delta time for simulation updates. + int updates_pending; ///< Number of frames the simulation should produce. + bool should_render; ///< Whether the simulation should be rendered. +} SimloopOut; + +typedef struct SimloopTimeline { + time_delta ddt; ///< Desired delta time. + time_point last_step; ///< Time of the last simulation step. +} SimloopTimeline; + +typedef struct Simloop { + SimloopTimeline update; ///< Update timeline. + SimloopTimeline render; ///< Render timeline. + uint64_t frame; ///< Frame counter. + Timer* timer; +} Simloop; + +/// Create a simulation loop. +Simloop simloop_make(const SimloopArgs*); + +/// Step the simulation loop. +void simloop_update(Simloop*, SimloopOut*); -- cgit v1.2.3