-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpdateSystem.cpp
82 lines (67 loc) · 1.81 KB
/
UpdateSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <UpdateSystem.h>
#include <ZeroTimer.h>
UpdateSystem::UpdateSystem(float targetFps)
: m_targetFps(targetFps), m_frameTime(1.0f / targetFps)
{
}
void UpdateSystem::RegisterCallback(std::function<void(float, float)> callback, std::string name)
{
if (m_callbacks.find(name) != m_callbacks.end())
{
return;
}
m_callbacks.insert({ name, callback });
}
void UpdateSystem::UnregisterCallback(std::string name)
{
m_callbacks.erase(name);
}
void UpdateSystem::Run()
{
unsigned int previousTime = GetTickCount_();
float accumulatedTime = 0.0f;
while (m_running)
{
unsigned int currentTime = GetTickCount_();
float deltaTime = (currentTime - previousTime) / 1000.0f;
previousTime = currentTime;
accumulatedTime += deltaTime;
accumulatedTime = (std::min)(accumulatedTime, 1.0f);
int numSteps = static_cast<int>(std::floor(accumulatedTime / m_frameTime));
if (numSteps == 0)
{
continue;
}
float timePerStep = accumulatedTime / numSteps;
accumulatedTime -= numSteps * timePerStep;
for (int i = 0; i < numSteps; i++)
{
FixedUpdate(timePerStep);
}
for (auto& callback : m_callbacks)
{
callback.second(timePerStep, m_fps);
}
if (m_targetFps > 0)
{
float targetFrameTime = 1.0f / m_targetFps;
float timeToSleep = targetFrameTime - timePerStep;
if (timeToSleep > 0.0f)
{
Sleep((int)timeToSleep);
}
}
m_fps = 1.0f / (timePerStep + accumulatedTime);
}
}
void UpdateSystem::Stop()
{
m_running = false;
}
float UpdateSystem::GetFPS() const
{
return m_fps;
}
void UpdateSystem::FixedUpdate(float deltaTime)
{
}