-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.inl
119 lines (96 loc) · 2.53 KB
/
game.inl
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "game.h"
void Game::InitVariables() {
window_ = nullptr;
dt = 0.f;
grid_size_ = 64.f;
}
void Game::InitGraphicsSettings() {
grfx_settings_.LoadFromFile("Config/graphics.s2");
}
void Game::InitWindow() {
if (grfx_settings_.fullscreen)
window_ = new sf::RenderWindow(grfx_settings_.resolution,
grfx_settings_.title, sf::Style::Fullscreen,
grfx_settings_.context_settings);
else
window_ =
new sf::RenderWindow(grfx_settings_.resolution, grfx_settings_.title,
sf::Style::Titlebar | sf::Style::Close,
grfx_settings_.context_settings);
window_->setFramerateLimit(grfx_settings_.frame_rate_limit);
window_->setVerticalSyncEnabled(grfx_settings_.vertical_sync);
}
void Game::InitKeys() {
std::ifstream ifs("Config/supported_keys.s2");
if (ifs.is_open()) {
_::AString key = "";
int key_value = 0;
while (ifs >> key >> key_value) {
supported_keys_[key] = key_value;
}
}
ifs.close();
#if DEBUG_THIS
for (auto i : supported_keys_) {
std::cout << i.first << " " << i.second << "\n";
}
#endif
}
void Game::InitStateData() {
state_data_.window_ = window_;
state_data_.gfx_settings_ = &grfx_settings_;
state_data_.supported_keys_ = &supported_keys_;
state_data_.states_ = &states_;
state_data_.grid_size_ = grid_size_;
}
void Game::InitStates() { states_.Push(new StateMenuMain(&state_data_)); }
Game::Game() {
InitVariables();
InitGraphicsSettings();
InitWindow();
InitKeys();
InitStateData();
InitStates();
}
Game::~Game() {
delete window_;
while (!states_.Empty()) {
delete states_.Top();
states_.Pop();
}
}
void Game::EndApplication() { std::cout << "\nEnding Application."; }
void Game::UpdateTimeDelta() { dt = time_delta_.restart().asSeconds(); }
void Game::UpdateEvents() {
while (window_->pollEvent(event_)) {
if (event_.type == sf::Event::Closed) window_->close();
}
}
void Game::Update() {
UpdateEvents();
if (!states_.IsEmpty()) {
if (window_->HasFocus()) {
states_.Top()->Update(dt);
if (states_.Top()->GetQuit()) {
states_.Top()->EndState();
delete states_.Top();
states_.pop();
}
}
} else {
EndApplication();
window_->close();
}
}
void Game::Render() {
window_->clear();
if (!states_.empty()) states_.Top()->Render();
window_->display();
}
void Game::Run() {
while (window_->isOpen()) {
UpdateTimeDelta();
Update();
Render();
}
}