|
Aegis2D
Modular C++26 2D game engine for networked games
|
Turns variable frame times into a whole number of fixed simulation steps. More...
#include <aegis2d/core/clock.hpp>
Public Member Functions | |
| FixedTimestep (Duration step, Duration max_frame=default_max_frame) | |
| Creates an empty accumulator. | |
| std::size_t | advance (Duration elapsed) noexcept |
| Adds the time elapsed since the previous frame. | |
| double | alpha () const noexcept |
| Fraction of a step left in the accumulator. | |
| Duration | step () const noexcept |
| Duration of one simulation step. | |
| Duration | max_frame () const noexcept |
| Maximum elapsed time taken into account per frame. | |
| Duration | accumulated () const noexcept |
| Time accumulated but not consumed by a step yet. | |
| void | reset () noexcept |
| Drops the accumulated time. | |
Static Public Attributes | |
| static constexpr Duration | default_max_frame = std::chrono::milliseconds{250} |
| Default cap on the time a single frame can add: 250 ms. | |
Turns variable frame times into a whole number of fixed simulation steps.
Each frame, advance() adds the real elapsed time to an accumulator and returns how many steps of step() fit in it; the remainder is kept for the next frame. The simulation therefore runs at the same rate whatever the frame rate, and alpha() tells how far the current frame is between two steps, for rendering interpolation.
Elapsed times longer than max_frame() are clamped: after a long stall (breakpoint, window drag), the simulation slows down instead of running hundreds of catch-up steps that would make the next frame even longer ("spiral of death").
|
explicit |
Creates an empty accumulator.
| step | Duration of one simulation step, e.g. step_from_rate(60). |
| max_frame | Maximum elapsed time taken into account per advance() call. |
| std::invalid_argument | if step is not positive or max_frame is shorter than step. |
|
noexcept |
Time accumulated but not consumed by a step yet.
[0, step()).
|
noexcept |
Adds the time elapsed since the previous frame.
| elapsed | Real time of the frame; negative values count as zero and values above max_frame() are clamped. |
|
noexcept |
Fraction of a step left in the accumulator.
[0, 1), used to interpolate between the last two states.
|
noexcept |
Maximum elapsed time taken into account per frame.
|
noexcept |
Drops the accumulated time.
|
noexcept |
Duration of one simulation step.
|
staticconstexpr |
Default cap on the time a single frame can add: 250 ms.