Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
Clock.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <limits>
5
6namespace shkyera {
7
8template <size_t MaxNanosecondCount = std::numeric_limits<size_t>::max()>
9struct Clock {
10 float deltaTime{1.0 / 60.0};
11 float scaleTime{0.0f};
12
13 void reset() {
14 const auto curTime = std::chrono::high_resolution_clock::now();
15 const auto nanosecondDiff = std::min(
16 MaxNanosecondCount,
17 static_cast<size_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(curTime - _lastReset).count()));
18
19 deltaTime = scaleTime * static_cast<float>(nanosecondDiff) * 1e-9;
20 _lastReset = curTime;
21 }
22
23 bool isPaused() { return scaleTime == 0.0f; }
24
25 void pause() { scaleTime = 0.0f; }
26
27 void start(float speed = 1.0f) { scaleTime = speed; }
28
29 private:
30 std::chrono::high_resolution_clock::time_point _lastReset{};
31};
32
33namespace clock {
34extern Clock<static_cast<size_t>(1e9 / 30)> Game;
35}
36
37} // namespace shkyera
Clock< static_cast< size_t >(1e9/30)> Game
Definition Clock.cpp:5
Definition Asset.hpp:6
Definition Clock.hpp:9
void start(float speed=1.0f)
Definition Clock.hpp:27
bool isPaused()
Definition Clock.hpp:23
void reset()
Definition Clock.hpp:13
void pause()
Definition Clock.hpp:25