Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
loop.hpp
Go to the documentation of this file.
1
3#pragma once
4
6#include <aegis2d/core/export.hpp>
7
8#include <atomic>
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12
13namespace aegis2d::core {
14
23class AEGIS2D_CORE_EXPORT MainLoop {
24public:
26 using UpdateFn = std::function<void(Duration step)>;
28 using RenderFn = std::function<void(double alpha)>;
29
33 explicit MainLoop(FixedTimestep timestep) noexcept;
34
37 void set_update(UpdateFn update);
38
42 void set_render(RenderFn render);
43
45 void run();
46
48 void stop() noexcept;
49
52 [[nodiscard]] bool running() const noexcept;
53
58 std::size_t frame(Duration elapsed);
59
62 [[nodiscard]] std::uint64_t ticks() const noexcept;
63
66 [[nodiscard]] const FixedTimestep& timestep() const noexcept;
67
68private:
69 FixedTimestep timestep_;
70 UpdateFn update_;
71 RenderFn render_;
72 std::atomic<bool> running_{false};
73 std::uint64_t ticks_ = 0;
74};
75
76} // namespace aegis2d::core
Turns variable frame times into a whole number of fixed simulation steps.
Definition clock.hpp:32
Drives a game: fixed-rate updates, then one render per frame.
Definition loop.hpp:23
std::function< void(double alpha)> RenderFn
Rendering callback, called with FixedTimestep::alpha().
Definition loop.hpp:28
MainLoop(FixedTimestep timestep) noexcept
Creates a stopped loop.
void stop() noexcept
Makes run() return after the current frame. Thread-safe.
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.
std::function< void(Duration step)> UpdateFn
Simulation callback, called with the fixed step duration.
Definition loop.hpp:26
void set_update(UpdateFn update)
Sets the simulation callback (none by default).
Fixed-timestep accumulator and monotonic stopwatch.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::chrono::nanoseconds Duration
Duration type used for simulation time (nanoseconds).
Definition clock.hpp:14
Definition endpoint.hpp:85