Toast Engine
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1
9
10#pragma once
11#include <atomic>
12#include <chrono>
13#include <cstdint>
14#include <toast/export.hpp>
15
16namespace toast {
17class Engine;
18}
19
20struct TOAST_API TimeSnapshot {
21 double delta = 0.0;
22 double raw_delta = 0.0;
23 double render_delta = 0.0;
24 double uptime = 0.0;
25 uint64_t frame = 0;
26 uint64_t render_frame = 0;
27 bool paused = false;
28};
29
37class TOAST_API Time final {
38 using clock_t = std::chrono::high_resolution_clock;
39 using time_point_t = clock_t::time_point;
40 friend struct toast::Engine;
41
42public:
43 Time(const Time&) = delete;
44 auto operator=(const Time&) -> Time& = delete;
45 Time(Time&&) = delete;
46 auto operator=(Time&&) -> Time& = delete;
47
48 ~Time();
49
50 static auto get() noexcept -> Time&;
51
52 void tick() noexcept;
53 void renderTick() noexcept;
54
55 static auto delta() noexcept -> double;
56 static auto rawDelta() noexcept -> double;
57 static auto renderDelta() noexcept -> double;
58
59 static auto frame() noexcept -> uint64_t;
60 static auto renderFrame() noexcept -> uint64_t;
61
62 static auto snapshot() noexcept -> TimeSnapshot;
63
64 static auto scale() noexcept -> double;
65 static void scale(double value);
66
67 static void pause() noexcept;
68 static void resume() noexcept;
69 static auto paused() noexcept -> bool;
70
71 static auto tps() noexcept -> double;
72 static auto fps() noexcept -> double;
73
74 static auto uptime() noexcept -> double;
75 static auto system() noexcept -> double;
76 static void setMaxDelta(double delta);
77
78private:
79 Time();
80 static inline Time* instance = nullptr;
81
82 std::atomic<double> m_max_delta {1.0 / 15.0};
83 std::atomic<double> m_delta_scale {1.0};
84
85 // main thread state
86 time_point_t m_now;
87 time_point_t m_previous;
88 time_point_t m_start_time;
89 uint64_t m_sim_frame = 0;
90
91 // render thread state
92 time_point_t m_now_render;
93 time_point_t m_previous_render;
94 uint64_t m_render_count = 0;
95
96 // published state
97 alignas(64) std::atomic<double> m_pub_delta {0.0};
98 std::atomic<double> m_pub_raw_delta {0.0};
99 std::atomic<uint64_t> m_pub_frame {0};
100 std::atomic<bool> m_pub_paused {false};
101 std::atomic<bool> m_paused {false};
102 std::atomic<uint32_t> m_sim_seq {0};
103
104 alignas(64) std::atomic<double> m_pub_render_delta {0.0};
105 std::atomic<uint64_t> m_pub_render_frame {0};
106 std::atomic<uint32_t> m_render_seq {0};
107};
void tick() noexcept
Updates the main thread clock.
Definition time.cpp:30
static auto scale() noexcept -> double
Definition time.cpp:144
static auto get() noexcept -> Time &
Definition time.cpp:25
static auto frame() noexcept -> uint64_t
Definition time.cpp:80
static auto delta() noexcept -> double
Definition time.cpp:68
static auto uptime() noexcept -> double
Definition time.cpp:152
static auto rawDelta() noexcept -> double
Definition time.cpp:72
static auto renderFrame() noexcept -> uint64_t
Definition time.cpp:84
static void resume() noexcept
Resumes simulation time.
Definition time.cpp:126
static auto fps() noexcept -> double
Definition time.cpp:139
static auto tps() noexcept -> double
Definition time.cpp:134
static auto snapshot() noexcept -> TimeSnapshot
Definition time.cpp:88
static auto system() noexcept -> double
Definition time.cpp:157
static void pause() noexcept
Pauses simulation time.
Definition time.cpp:122
void renderTick() noexcept
Updates the render thread clock.
Definition time.cpp:55
static void setMaxDelta(double delta)
Sets the maximum delta the simulation will report.
Definition time.cpp:162
static auto paused() noexcept -> bool
Definition time.cpp:130
static auto renderDelta() noexcept -> double
Definition time.cpp:76
Definition engine.hpp:25
Definition time.hpp:20