-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicEngine.cpp
More file actions
63 lines (47 loc) · 1.49 KB
/
LogicEngine.cpp
File metadata and controls
63 lines (47 loc) · 1.49 KB
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
#include "LogicEngine.h"
dh::LogicEngine::LogicEngine(dh::GameDataRef m_gameData, float fMaxUpdatesPerSecond) : m_gameData(m_gameData)
{
this->fMaxUpdatesPerSecond = fMaxUpdatesPerSecond;
}
dh::LogicEngine::~LogicEngine()
{
if(m_updateThread.joinable())
m_updateThread.join();
}
void dh::LogicEngine::startLogicThread(std::function<void()> updateFunc)
{
m_updateThread = std::thread(&dh::LogicEngine::m_logicThread, this, updateFunc);
}
float dh::LogicEngine::getUpdateElapsedTime() const
{
return fUpdateElapsedTime;
}
void dh::LogicEngine::m_logicThread(std::function<void()> updateFunc)
{
std::cout << "\tRendering thread started." << std::endl;
while (this->m_gameData->bGameRunning)
{
m_updateLogicClock();
updateFunc();
}
}
void dh::LogicEngine::m_updateLogicClock()
{
double elapsedTimeDiff = 0.0;
//elapsed time update
this->updateTime = this->updateClk.restart();
this->fUpdateElapsedTime = this->updateTime.asSeconds();
fUpdateCounter += fUpdateElapsedTime;
if (this->fUpdateCounter >= 0.2f) {
this->fUpdateCounter -= 0.2f;
}
if (this->fUpdateElapsedTime >= 0.2f) {
this->fUpdateElapsedTime = 0.2f;
}
//lock the updates per second
if (this->fUpdateElapsedTime < fMaxUpdatesPerSecond) {
elapsedTimeDiff = fMaxUpdatesPerSecond - this->fUpdateElapsedTime;
this->fUpdateElapsedTime = static_cast<float>(fMaxUpdatesPerSecond);
std::this_thread::sleep_for(std::chrono::duration<float>(elapsedTimeDiff));
}
}