|
Aegis2D
Modular C++26 2D game engine for networked games
|
Drives a game: fixed-rate updates, then one render per frame. More...
#include <aegis2d/core/loop.hpp>
Public Types | |
| using | UpdateFn = std::function< void(Duration step)> |
| Simulation callback, called with the fixed step duration. | |
| using | RenderFn = std::function< void(double alpha)> |
| Rendering callback, called with FixedTimestep::alpha(). | |
Public Member Functions | |
| MainLoop (FixedTimestep timestep) noexcept | |
| Creates a stopped loop. | |
| void | set_update (UpdateFn update) |
| Sets the simulation callback (none by default). | |
| void | set_render (RenderFn render) |
| Sets the rendering callback (none by default: the loop then sleeps between steps). | |
| void | run () |
| Runs frames until stop() is called. A stop() made before run() is ignored. | |
| void | stop () noexcept |
| Makes run() return after the current frame. Thread-safe. | |
| bool | running () const noexcept |
| Tells whether run() is looping. | |
| std::size_t | frame (Duration elapsed) |
| Runs a single frame for a given elapsed time, without measuring or sleeping. | |
| std::uint64_t | ticks () const noexcept |
| Number of updates run since the loop was created. | |
| const FixedTimestep & | timestep () const noexcept |
| Fixed timestep of the loop. | |
Drives a game: fixed-rate updates, then one render per frame.
Each frame, the loop measures the real elapsed time, runs the update callback as many times as the FixedTimestep asks, then calls the render callback with the interpolation factor. Without a render callback (dedicated server), the loop sleeps until the next step is due instead of spinning.
Callbacks run on the thread that called run(). stop() can be called from any thread, including from a callback.
| using aegis2d::core::MainLoop::RenderFn = std::function<void(double alpha)> |
Rendering callback, called with FixedTimestep::alpha().
| using aegis2d::core::MainLoop::UpdateFn = std::function<void(Duration step)> |
Simulation callback, called with the fixed step duration.
|
explicitnoexcept |
Creates a stopped loop.
| timestep | Step and frame cap of the simulation, e.g. FixedTimestep{step_from_rate(60)}. |
| std::size_t aegis2d::core::MainLoop::frame | ( | Duration | elapsed | ) |
Runs a single frame for a given elapsed time, without measuring or sleeping.
Used by run(), and by tests and tools that drive the loop by hand.
| elapsed | Real time since the previous frame. |
| void aegis2d::core::MainLoop::run | ( | ) |
|
noexcept |
| void aegis2d::core::MainLoop::set_render | ( | RenderFn | render | ) |
Sets the rendering callback (none by default: the loop then sleeps between steps).
| render | Callback called once per frame, after the updates. |
| void aegis2d::core::MainLoop::set_update | ( | UpdateFn | update | ) |
Sets the simulation callback (none by default).
| update | Callback called once per fixed step. |
|
noexcept |
Makes run() return after the current frame. Thread-safe.
|
noexcept |
Number of updates run since the loop was created.
|
noexcept |
Fixed timestep of the loop.