-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
180 lines (154 loc) · 3.76 KB
/
Copy pathGame.cpp
File metadata and controls
180 lines (154 loc) · 3.76 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
#include "Game.h"
//Private function
void Game::initializeVariables()
{ //set up var
this->window = nullptr;
this->deltaTime = 0.f;
}
void Game::initializeWindow()
{ //set up window
std::ifstream ifs("Config/window.ini.txt");
this->videoModes = sf::VideoMode::getFullscreenModes();
this->videoMode = sf::VideoMode::getDesktopMode();
bool fullscreen = false;
std::string title = "None";
unsigned framerate_limit = 144;
bool vertical_sync_enabled = false;
unsigned antialiasing_level = 0;
if (ifs.is_open()) {
std::getline(ifs, title);
ifs >> videoMode.height >> videoMode.width;
ifs >> fullscreen;
ifs >> framerate_limit;
ifs >> antialiasing_level;
}
ifs.close();
icon.loadFromFile("Resources/icon.png");
this->window_settings.antialiasingLevel = antialiasing_level;
if(fullscreen)
this->window = new sf::RenderWindow(sf::VideoMode(this->videoMode), title, sf::Style::Fullscreen, window_settings);
else
this->window = new sf::RenderWindow(sf::VideoMode(this->videoMode), title, sf::Style::Titlebar|sf::Style::Close, window_settings);
this->window->setFramerateLimit(framerate_limit);
this->window->setVerticalSyncEnabled(vertical_sync_enabled);
this->window->setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
}
void Game::initKeys()
{
std::ifstream ifs("Config/supportedKeys.ini.txt");
if (ifs.is_open()) {
std::string key = "";
int key_value = 0;
while (ifs >> key >> key_value) {
this->supportedKeys[key] = key_value;
}
}
ifs.close();
}
void Game::initializeStates()
{
this->states.push(new MainMenuState(this->window,&this->supportedKeys,&this->states));
}
void Game::initMouse()
{
this->MouseTexture.loadFromFile("Resources/UI/Mouse.png");
this->Mouse.setTexture(&this->MouseTexture);
this->Mouse.setSize(sf::Vector2f(40, 40));
}
Game::Game()
{
this->initializeVariables();
this->initializeWindow();
this->initKeys();
this->initializeStates();
this->initMouse();
this->window->setMouseCursorVisible(false);
this->font.loadFromFile("Fonts/Minecraft.ttf");
this->StudentID = new gui::textbox(1100, 100, 200, 30, sf::Color::Transparent,
"64010324 Thanapob Parinyarat",
&this->font, 30, sf::Color::White,
nullptr, false,
sf::Color::Black,3.f);
}
Game::~Game()
{
delete this->window;
while (!this->states.empty()) {
delete this->states.top();
this->states.pop();
}
}
//Accessors
const bool Game::isWindowOpen() const
{
return this->window->isOpen();
}
//function
void Game::endGame()
{
std::cout << "Game End!!!\n";
this->window->close();
}
void Game::UpdateDeltaTime()
{
// Update the deltatime with the time is takes to update and render one frame
this->deltaTime = this->deltaTimeClock.restart().asSeconds();
}
void Game::pollEvents()
{
while (this->window->pollEvent(this->ev)) {
switch (this->ev.type)
{
case sf::Event::Closed:
this->endGame();
break;
}
}
}
void Game::UpdateMouse()
{
sf::Vector2f mousePosView;
mousePosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window));
this->Mouse.setPosition(mousePosView.x, mousePosView.y);
}
void Game::Update()
{
/*
Update game event
*/
this->pollEvents();
if (!this->states.empty()) {
this->states.top()->Update(this->deltaTime);
this->UpdateMouse();
//getQuit
if (this->states.top()->getQuit()) {
this->states.top()->endState();
delete this->states.top();
this->states.pop();
}
}
//Game End
else {
this->endGame();
}
}
void Game::Render()
{
/*
clear old frame
render game objects
display frame in window
*/
this->window->clear(sf::Color::Black);
//Draw game objects
if (!this->states.empty()) {
this->states.top()->Render();
window->draw(this->Mouse);
this->StudentID->Render(*window);
}
this->window->display();
}
void Game::Framerate()
{
std::cout << 1.f / deltaTime << std::endl;
}