-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
187 lines (154 loc) · 3.58 KB
/
Game.cpp
File metadata and controls
187 lines (154 loc) · 3.58 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
#include "Game.h"
//Static functions
//Initializer functions
void Game::initVariables()
{
this->window = nullptr;
this->fullscreen = false;
this->dt = 0;
}
void Game::initWindow()
{
//Creates a SFML window using .ini
std::ifstream ifs("Config/window.ini");
this->videoModes = sf::VideoMode::getFullscreenModes();
std::string title = "None";
sf::VideoMode window_bounds = sf::VideoMode::getDesktopMode();
bool fullscreen = false;
unsigned framerate_limit = 120;
bool vertical_sync_enabled = false;
unsigned antialiasing_level = 0;
if (ifs.is_open())
{
std::getline(ifs, title);
ifs >> window_bounds.width >> window_bounds.height;
ifs >> fullscreen;
ifs >> framerate_limit;
ifs >> vertical_sync_enabled;
ifs >> antialiasing_level;
}
ifs.close();
this->fullscreen = false;
windowSettins.antialiasingLevel = antialiasing_level;
if (fullscreen)
this->window = new sf::RenderWindow(window_bounds, title, sf::Style::Fullscreen , windowSettins);
else
this->window = new sf::RenderWindow(window_bounds, title, sf::Style::Titlebar | sf::Style::Close, windowSettins);
this->window->setFramerateLimit(framerate_limit);
this->window->setKeyRepeatEnabled(false);
this->window->setVerticalSyncEnabled(vertical_sync_enabled);
}
void Game::initKeys()
{
std::ifstream ifs("Config/supported_keys.ini");
if (ifs.is_open())
{
std::string key = "";
int key_value = 0;
while (ifs >> key >> key_value)
{
this->supportedKeys[key] = key_value;
}
}
else
{
this->supportedKeys["Escape"] = sf::Keyboard::Key::Escape;
this->supportedKeys["A"] = sf::Keyboard::Key::A;
this->supportedKeys["D"] = sf::Keyboard::Key::D;
this->supportedKeys["W"] = sf::Keyboard::Key::W;
this->supportedKeys["S"] = sf::Keyboard::Key::S;
}
//DEBUG
for (auto i : this->supportedKeys)
{
std::cout << i.first << " " << i.second << "\n";
}
}
void Game::initStates()
{
this->states.push(new MainMenuState(this->window, &this->supportedKeys, &this->states, &this->sfEvent));
}
//Constructors/Destructors
Game::Game()
{
this->initVariables();
this->initWindow();
this->initKeys();
this->initStates();
}
Game::~Game()
{
delete this->window;
while (!this->states.empty())
{
delete this->states.top();
this->states.pop();
}
}
//Functions
//sf::Clock Game::getDtClock()
//{
//
//}
//Regular
void Game::endApplication()
{
std::cout << "Ending app" << "\n";
}
//Update
void Game::updateDt()
{
//Updates the dt variable with the time it takes to update and render one frame
this->dt = this->dtClock.restart().asSeconds();
system("cls");
//std::cout << this->dt << "\n";
}
void Game::updateSFMLEvents()
{
while (this->window->pollEvent(this->sfEvent))
{
if (this->sfEvent.type == sf::Event::Closed)
{
this->window->close();
}
}
}
void Game::update()
{
this->updateSFMLEvents();
if (!this->states.empty())
{
this->states.top()->update(this->dt);
if (this->states.top()->getQuit())
{
this->states.top()->endState();
delete this->states.top();
this->states.pop();
}
}
//App end
else
{
this->endApplication();
this->window->close();
}
}
//Render
void Game::render()
{
this->window->clear();
//Render items
if (!this->states.empty())
this->states.top()->render(this->window);
this->window->display();
}
//Core
void Game::run()
{
while (this->window->isOpen())
{
this->updateDt();
this->update();
this->render();
}
}