-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
89 lines (77 loc) · 1.67 KB
/
Game.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
83
84
85
86
87
88
89
#include "Game.h"
#include "GameState.h"
#include <chrono>
using namespace std;
using namespace sf;
Game::Game(const string& title, const unsigned int width, const unsigned int height)
{
m_window.create({ width, height }, title, Style::Titlebar | Style::Close);
m_window.setVerticalSyncEnabled(true);
}
Game::~Game()
{
while (!m_states.empty())
{
m_states.back()->cleanUp();
m_states.pop_back();
}
m_window.close();
}
void Game::run()
{
const float ftStep = 1.f, ftSlice = 1.f;
float lastFt = 0.f;
float currentSlice = 0.f;
while (m_status != Status::Ending)
{
auto startTime = chrono::high_resolution_clock::now();
m_window.clear();
m_states.back()->handleEvent(this);
currentSlice += lastFt;
while (currentSlice >= ftSlice)
{
m_states.back()->update(this, ftStep);
currentSlice -= ftSlice;
}
m_states.back()->draw(this);
m_window.display();
auto endTime = chrono::high_resolution_clock::now();
auto elapsedTime = endTime - startTime;
lastFt = chrono::duration_cast<chrono::duration<float, milli>>(elapsedTime).count();
#ifdef _DEBUG
int fps = int(1.f / (lastFt / 1000.0f));
m_window.setTitle(" FPS: " + to_string(fps));
#endif
}
}
void Game::changeState(GameState* state)
{
if (!m_states.empty())
{
m_states.back()->cleanUp();
m_states.pop_back();
}
m_states.emplace_back(state);
m_states.back()->init(this);
}
void Game::pushState(GameState* state)
{
if (!m_states.empty())
{
m_states.back()->pause();
}
m_states.emplace_back(state);
m_states.back()->init(this);
}
void Game::popState()
{
if (!m_states.empty())
{
m_states.back()->cleanUp();
m_states.pop_back();
}
if (!m_states.empty())
{
m_states.back()->resume();
}
}