-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
238 lines (199 loc) · 4.48 KB
/
Game.cpp
File metadata and controls
238 lines (199 loc) · 4.48 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "stdafx.h"
#include "Game.h"
//private: Static functions:
//private: Initialization functions:
void Game::initVariables()
{
this->window = nullptr;
this->dt = 0.f;
this->gridSize = 64.f;
}
//For loadFromFile():
//Should use '../' before the file path to be able to play the game without Visual Studio.
//For fstream:
//Should use '\\' instead of '/' in a file path and put this file to the .exe location,
//to be able to play the game without Visual Studio.
void Game::initGraphicsSettings(const std::string file_path)
{
this->gfxSettings.loadFromFile(file_path);
}
//Creates a SFML window, using options from a .ini file.
void Game::initWindow()
{
/*if (this->gfxSettings.fullscreen == 0)
{
this->window = new sf::RenderWindow
(
this->gfxSettings.resolution,
this->gfxSettings.title,
sf::Style::Titlebar | sf::Style::Close,
this->gfxSettings.contextSettings
);
}
else if (this->gfxSettings.fullscreen == 1)
{
this->window = new sf::RenderWindow
(
this->gfxSettings.resolution,
this->gfxSettings.title,
sf::Style::Fullscreen,
this->gfxSettings.contextSettings
);
}
else if (this->gfxSettings.fullscreen == 2)
{
this->window = new sf::RenderWindow
(
this->gfxSettings.resolution,
this->gfxSettings.title,
sf::Style::None,
this->gfxSettings.contextSettings
);
}
else
{
this->window = new sf::RenderWindow
(
this->gfxSettings.resolution,
this->gfxSettings.title,
sf::Style::Default,
this->gfxSettings.contextSettings
);
}*/
this->window = new sf::RenderWindow
(
this->gfxSettings.resolution,
this->gfxSettings.title,
this->gfxSettings.style,
this->gfxSettings.contextSettings
);
this->window->setFramerateLimit(this->gfxSettings.frameRateLimit);
this->window->setVerticalSyncEnabled(this->gfxSettings.verticalSync);
//this->window->setMouseCursorGrabbed(true);
}
//Should use '\\' instead of '/' in a file path and put this file to the .exe location,
//to be able to play the game without Visual Studio.
void Game::initKeys(const std::string file_path)
{
std::ifstream in;
in.open(file_path);
if (in.fail())
{
std::cout << "ERROR::Game::initKeys::Failed to load: " << file_path << std::endl;
}
else if (in.is_open())
{
std::string key = "";
int key_value = 0;
while (in >> key >> key_value)
{
this->supportedKeys[key] = key_value;
}
}
in.close();
//Debug
std::cout << "Game::initKeys::Supported keys:" << std::endl;
for (auto i : this->supportedKeys)
{
std::cout << i.first << " " << i.second << std::endl;
}
}
void Game::initStateData()
{
this->stateData.window = this->window;
this->stateData.gfxSettings = &this->gfxSettings;
this->stateData.supportedKeys = &this->supportedKeys;
this->stateData.states = &this->states;
this->stateData.gridSize = this->gridSize;
}
void Game::initStates()
{
//Main menu
this->states.push(new MainMenuState(&this->stateData));
}
//Constructors and Destructor:
Game::Game()
{
this->initVariables();
this->initGraphicsSettings("Config/graphics.ini");
this->initWindow();
this->initKeys("Config/supported_keys.ini");
this->initStateData();
this->initStates();
}
Game::~Game()
{
//Deleting SFML window
delete this->window;
while (this->states.empty() == false)
{
delete this->states.top();
this->states.pop();
}
}
//Accessors:
//Modifiers:
//Functions:
void Game::endApplication()
{
std::cout << "Game::endApplication::Ending Application!" << std::endl;
}
//Updates dt variable with the time it takes to update and render one frame.
void Game::updateDt()
{
this->dt = this->dtClock.restart().asSeconds();
}
void Game::updateEventsSFML()
{
while (this->window->pollEvent(this->sfEvent))
{
if (this->sfEvent.type == sf::Event::Closed)
{
this->window->close();
}
}
}
void Game::update()
{
//Updating states
if (this->states.empty() == false)
{
this->states.top()->update(this->dt);
if (this->states.top()->getQuit() == true)
{
this->states.top()->endState();
delete this->states.top();
this->states.pop();
}
}
else //Application end
{
this->endApplication();
this->window->close();
}
}
void Game::render()
{
//Clearing frame with black color
this->window->clear(sf::Color::Black);
//Rendering states
if (this->states.empty() == false)
{
this->states.top()->render();
}
//Displaying frame
this->window->display();
}
void Game::run()
{
while (this->window->isOpen())
{
this->updateEventsSFML();
this->updateDt();
if (this->window->hasFocus())
{
this->update();
}
this->render();
}
}