/// @file runtime/loop.h /// @brief Drives a Game on a fixed simulation step, without owning a window. #pragma once #include "core/clock.h" #include "core/fixed.h" #include "core/fixed_step.h" #include "core/types.h" #include "runtime/game.h" namespace kumo { /// @brief Settings for Loop. struct LoopConfig { Nanoseconds step = step_from_hz(60); ///< Fixed update duration. Nanoseconds max_frame = milliseconds(250); ///< Longest frame accepted. }; /// @brief Outcome of one rendered frame. struct FrameReport { int ticks_run = 0; ///< Fixed updates executed during this frame. uint64_t total_ticks = 0; ///< Total fixed updates executed since start. Fixed alpha; ///< Interpolation fraction passed to render(). }; /// @brief Connects a Clock and a FixedStep to a Game. /// /// Does not own a Window, so the same code drives both the windowed app /// (with SystemClock) and headless tests or replays (with ManualClock). class Loop { public: /// @brief Starts a loop reading frame durations from @p clock. /// /// @param clock Time source; must outlive the loop. /// @param config Step length and frame clamp. explicit Loop(Clock& clock, LoopConfig config = {}); /// @brief Measures elapsed time from the clock, runs 0 or more fixed updates, /// and renders one frame into @p frame. FrameReport step_frame(Game& game, FrameBuffer& frame); /// @brief Runs exactly @p count fixed updates without consulting the clock /// or rendering. Useful for deterministic tests and replays. void step_ticks(Game& game, uint64_t count); /// @brief Forgets elapsed time since the last frame (e.g. after loading). void reset_timer() { timer_.reset(); } uint64_t total_ticks() const { return next_tick_index_; } Nanoseconds step() const { return stepper_.step(); } Nanoseconds simulated_time() const { return static_cast(next_tick_index_) * stepper_.step(); } private: FrameTimer timer_; FixedStep stepper_; uint64_t next_tick_index_ = 0; }; } // namespace kumo