/* 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*);