Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::FixedTimestep Class Reference

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.
 

Detailed Description

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").

Constructor & Destructor Documentation

◆ FixedTimestep()

aegis2d::core::FixedTimestep::FixedTimestep ( Duration  step,
Duration  max_frame = default_max_frame 
)
explicit

Creates an empty accumulator.

Parameters
stepDuration of one simulation step, e.g. step_from_rate(60).
max_frameMaximum elapsed time taken into account per advance() call.
Exceptions
std::invalid_argumentif step is not positive or max_frame is shorter than step.

Member Function Documentation

◆ accumulated()

Duration aegis2d::core::FixedTimestep::accumulated ( ) const
noexcept

Time accumulated but not consumed by a step yet.

Returns
A duration in [0, step()).

◆ advance()

std::size_t aegis2d::core::FixedTimestep::advance ( Duration  elapsed)
noexcept

Adds the time elapsed since the previous frame.

Parameters
elapsedReal time of the frame; negative values count as zero and values above max_frame() are clamped.
Returns
Number of simulation steps to run this frame.

◆ alpha()

double aegis2d::core::FixedTimestep::alpha ( ) const
noexcept

Fraction of a step left in the accumulator.

Returns
A value in [0, 1), used to interpolate between the last two states.

◆ max_frame()

Duration aegis2d::core::FixedTimestep::max_frame ( ) const
noexcept

Maximum elapsed time taken into account per frame.

Returns
The cap given to the constructor.

◆ reset()

void aegis2d::core::FixedTimestep::reset ( )
noexcept

Drops the accumulated time.

◆ step()

Duration aegis2d::core::FixedTimestep::step ( ) const
noexcept

Duration of one simulation step.

Returns
The step given to the constructor.

Member Data Documentation

◆ default_max_frame

constexpr Duration aegis2d::core::FixedTimestep::default_max_frame = std::chrono::milliseconds{250}
staticconstexpr

Default cap on the time a single frame can add: 250 ms.


The documentation for this class was generated from the following file: