vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
clock.h
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5
6#include <glm/glm.hpp>
7
8#include "core/engine_config.h"
9
10namespace Vkm::Engine {
11
18class Clock {
19 public:
20 Clock() = default;
21 ~Clock() = default;
22
23 Clock(const Clock& other) = default;
24 Clock& operator=(const Clock& other) = default;
25
26 Clock(Clock && other) = default;
27 Clock& operator=(Clock && other) = default;
28
29 public:
37 void beginFrame();
38
47
56 void setTickRate(uint32_t ticksPerSecond) {
57 m_fixedStep = 1.0f / static_cast<float>(Config::clampTickRate(ticksPerSecond));
58 }
59
67 void setPaused(bool paused) { m_paused = paused; }
68
77 void setTimeScale(float scale) { m_timeScale = scale < 0.0f ? 0.0f : scale; }
78
89 void setPacing(float scale) { m_pacing = scale < 0.0f ? 0.0f : scale; }
90
99 void requestStep(int steps = 1) { if (steps > 0) m_pendingSteps += steps; }
100
101 float getDeltaTime() const { return m_deltaTime; }
102 float getSimDelta() const { return m_simDelta; }
103 float getFixedStep() const { return m_fixedStep; }
104
110 float getFixedAlpha() const {
111 return m_fixedStep > 0.0f ? glm::clamp(m_accumulator / m_fixedStep, 0.0f, 1.0f) : 0.0f;
112 }
113
121 float getFrameRate() const { return m_smoothedDelta > 0.0f ? 1.0f / m_smoothedDelta : 0.0f; }
122
128 float getFrameTime() const { return m_smoothedDelta * 1000.0f; }
129
130 bool isPaused() const { return m_paused; }
131
138 bool isSimulationStill() const { return m_simDelta <= 0.0f; }
139
140 float getTimeScale() const { return m_timeScale; }
141 float getPacing() const { return m_pacing; }
142
150 uint32_t getTick() const { return m_tick; }
151
152 private:
153 std::chrono::steady_clock::time_point m_last{};
154 bool m_started = false;
155
156 float m_fixedStep = 1.0f / static_cast<float>(Config::DEFAULT_TICK_RATE);
157
158 float m_deltaTime = 0.0f;
159 float m_simDelta = 0.0f;
160 float m_accumulator = 0.0f;
161 float m_smoothedDelta = 0.0f;
162
163 bool m_paused = false;
164 int m_pendingSteps = 0;
165 float m_timeScale = 1.0f;
166 float m_pacing = 1.0f;
167
168 uint32_t m_tick = 0;
169};
170
171} // namespace Vkm::Engine
bool isSimulationStill() const
Whether the world stands still this frame.
Definition clock.h:138
float getFixedAlpha() const
How far into the next fixed step this frame falls, 0..1.
Definition clock.h:110
float getFrameTime() const
The smoothed frame as a duration rather than a rate.
Definition clock.h:128
void setPacing(float scale)
Set how many fixed steps a second of wall time buys, as a multiple.
Definition clock.h:89
bool consumeFixedStep()
Take one fixed step if one is owed; the main-loop fixedUpdate condition.
void setTimeScale(float scale)
Set the slow-motion / fast-forward multiplier applied while running.
Definition clock.h:77
float getFrameRate() const
Frames per second, smoothed over roughly the last half second.
Definition clock.h:121
uint32_t getTick() const
Which fixed step is running, counting from one.
Definition clock.h:150
void requestStep(int steps=1)
Queue fixed-step advances, whatever the play state.
Definition clock.h:99
void setPaused(bool paused)
Pause or resume simulation time.
Definition clock.h:67
void setTickRate(uint32_t ticksPerSecond)
Set the simulation cadence, in ticks per second.
Definition clock.h:56
void beginFrame()
Open a new frame: measure the real delta, derive the sim delta, fill the accumulator.
constexpr uint32_t clampTickRate(uint32_t ticksPerSecond)
What a project's requested tick rate is honoured as.
Definition engine_config.h:59