Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
clock.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/core/export.hpp>
6
7#include <chrono>
8#include <cstddef>
9#include <cstdint>
10
11namespace aegis2d::core {
12
14using Duration = std::chrono::nanoseconds;
15
19[[nodiscard]] constexpr Duration step_from_rate(std::uint32_t ticks_per_second) noexcept {
20 return Duration{std::chrono::seconds{1}} / ticks_per_second;
21}
22
32class AEGIS2D_CORE_EXPORT FixedTimestep {
33public:
35 static constexpr Duration default_max_frame = std::chrono::milliseconds{250};
36
42 explicit FixedTimestep(Duration step, Duration max_frame = default_max_frame);
43
48 std::size_t advance(Duration elapsed) noexcept;
49
52 [[nodiscard]] double alpha() const noexcept;
53
56 [[nodiscard]] Duration step() const noexcept;
57
60 [[nodiscard]] Duration max_frame() const noexcept;
61
64 [[nodiscard]] Duration accumulated() const noexcept;
65
67 void reset() noexcept;
68
69private:
70 Duration step_;
71 Duration max_frame_;
72 Duration accumulator_{};
73};
74
76class AEGIS2D_CORE_EXPORT Stopwatch {
77public:
79 using Clock = std::chrono::steady_clock;
80
82 Stopwatch() noexcept;
83
86 [[nodiscard]] Duration elapsed() const noexcept;
87
90 Duration restart() noexcept;
91
92private:
93 Clock::time_point start_;
94};
95
96} // namespace aegis2d::core
Turns variable frame times into a whole number of fixed simulation steps.
Definition clock.hpp:32
double alpha() const noexcept
Fraction of a step left in the accumulator.
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.
Measures elapsed time with the monotonic std::chrono::steady_clock.
Definition clock.hpp:76
std::chrono::steady_clock Clock
Clock the stopwatch reads; it never goes backwards.
Definition clock.hpp:79
Stopwatch() noexcept
Starts measuring from now.
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
constexpr Duration step_from_rate(std::uint32_t ticks_per_second) noexcept
Length of one step for a given update rate.
Definition clock.hpp:19