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

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ RenderFn

using aegis2d::core::MainLoop::RenderFn = std::function<void(double alpha)>

Rendering callback, called with FixedTimestep::alpha().

◆ UpdateFn

using aegis2d::core::MainLoop::UpdateFn = std::function<void(Duration step)>

Simulation callback, called with the fixed step duration.

Constructor & Destructor Documentation

◆ MainLoop()

aegis2d::core::MainLoop::MainLoop ( FixedTimestep  timestep)
explicitnoexcept

Creates a stopped loop.

Parameters
timestepStep and frame cap of the simulation, e.g. FixedTimestep{step_from_rate(60)}.

Member Function Documentation

◆ frame()

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.

Parameters
elapsedReal time since the previous frame.
Returns
Number of updates run during this frame.

◆ run()

void aegis2d::core::MainLoop::run ( )

Runs frames until stop() is called. A stop() made before run() is ignored.

◆ running()

bool aegis2d::core::MainLoop::running ( ) const
noexcept

Tells whether run() is looping.

Returns
true between the start of run() and the next stop().

◆ set_render()

void aegis2d::core::MainLoop::set_render ( RenderFn  render)

Sets the rendering callback (none by default: the loop then sleeps between steps).

Parameters
renderCallback called once per frame, after the updates.

◆ set_update()

void aegis2d::core::MainLoop::set_update ( UpdateFn  update)

Sets the simulation callback (none by default).

Parameters
updateCallback called once per fixed step.

◆ stop()

void aegis2d::core::MainLoop::stop ( )
noexcept

Makes run() return after the current frame. Thread-safe.

◆ ticks()

std::uint64_t aegis2d::core::MainLoop::ticks ( ) const
noexcept

Number of updates run since the loop was created.

Returns
The total step count.

◆ timestep()

const FixedTimestep & aegis2d::core::MainLoop::timestep ( ) const
noexcept

Fixed timestep of the loop.

Returns
The accumulator, read-only.

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