CoriEngine
Loading...
Searching...
No Matches
Time.cpp
Go to the documentation of this file.
1#include "Time.hpp"
2#include <SDL3/SDL_timer.h>
3#include "Input.hpp"
4
5namespace Cori {
6 namespace Core {
7 GameTimer::GameTimer() {
9 m_LastTime = SDL_GetPerformanceCounter();
10 }
11
12 std::string GameTimer::FormatTime_MS_to_M_S_MS(const double milliseconds) {
13 auto minutes = static_cast<int32_t>(milliseconds / 60000);
14 auto seconds = static_cast<int32_t>((milliseconds - minutes * 60000) / 1000);
15 auto ms = static_cast<int32_t>(milliseconds) % 1000;
16
17 return std::format("{}:{:02}:{:03}", minutes, seconds, ms);
18 }
19
20 std::string GameTimer::FormatTime_S_to_M_S_MS(const double seconds) {
21 return FormatTime_MS_to_M_S_MS(seconds * 1000.0);
22 }
23
24 void GameTimer::SetManualTickStep(const bool state) {
25 m_ManualStep = state;
26 }
27
28 void GameTimer::Update() {
30 const uint64_t now = SDL_GetPerformanceCounter();
31 m_DeltaTime = static_cast<double>(now - m_LastTime) / SDL_GetPerformanceFrequency();
32 m_LastTime = now;
33
34 m_Time += m_DeltaTime;
35
36 if (!m_ManualStep) {
37 m_Accumulator += m_DeltaTime;
38
39 if (m_Timestep != 0) {
40 while (m_Accumulator >= m_Timestep) {
41
42 m_TickrateUpdateFunc(*this);
43
44 m_Accumulator -= m_Timestep;
45 }
46
47 m_TickAlpha = m_Accumulator / m_Timestep;
48 }
49 } else {
50 static bool oneshot = true;
52 if (oneshot) {
53 oneshot = false;
54 m_ManualTickGate = true;
55 }
56 } else {
57 oneshot = true;
58 }
59
61 m_ManualTickGate = true;
62 }
63
64 if (m_ManualTickGate) {
65 m_Accumulator += m_DeltaTime;
66
67 if (m_Timestep != 0) {
68 while (m_Accumulator >= m_Timestep) {
69
70 m_TickrateUpdateFunc(*this);
71 m_ManualTickGate = false;
72
73 m_Accumulator -= m_Timestep;
74 }
75
76 m_TickAlpha = m_Accumulator / m_Timestep;
77 }
78 }
79 }
80
81 }
82
83 void GameTimer::SetTickrate(const uint16_t tickrate) {
84 m_Tickrate = tickrate;
85 m_Timestep = 1.0f / static_cast<float>(tickrate);
86 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Core::Self, Logger::Tags::Core::GameTimer }, "Tickrate set to '{}', timestep '{}'", tickrate, m_Timestep);
87 }
88
90 m_Start = SDL_GetPerformanceCounter();
91 }
92
93 double ManualTimer::End() const {
94 const uint64_t end = SDL_GetPerformanceCounter();
95 return static_cast<double>(end - m_Start) * 1000.0f / SDL_GetPerformanceFrequency();
96 }
97 }
98}
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
void SetTickrate(const uint16_t tickrate)
Changes the tickrate.
Definition Time.cpp:83
static std::string FormatTime_S_to_M_S_MS(const double seconds)
Formats seconds into a string with a format Min:Sec:Ms.
Definition Time.cpp:20
void SetManualTickStep(const bool state)
Enables or disables manual tick step.
Definition Time.cpp:24
static std::string FormatTime_MS_to_M_S_MS(const double milliseconds)
Formats milliseconds into a string with a format Min:Sec:Ms.
Definition Time.cpp:12
static bool IsKeyDown(const CoriKeycode keycode)
Checks if a specific keyboard key is down.
Definition Input.cpp:5
double End() const
Stops the manual timer.
Definition Time.cpp:93
void Start()
Start the manual timer.
Definition Time.cpp:89
Core systems of the engine are here.
Global engine namespace.
static constexpr char GameTimer[]
Definition Logger.hpp:96
static constexpr char Self[]
Definition Logger.hpp:83