-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
92 lines (67 loc) · 1.82 KB
/
Copy pathGame.cpp
File metadata and controls
92 lines (67 loc) · 1.82 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
#include "Game.h"
const float windowSizeRatioYtoX = 0.5625;
// initializer
void Game::initWindow(){
this->window = new sf::RenderWindow(sf::VideoMode(2500, 2000), "PD_final");
this->window ->setFramerateLimit(144);
this->window->setVerticalSyncEnabled(false);
}
void Game::initState(){ // push different state
this-> states.push(new mainMenuState(this->window, &this->states));
}
// constructor
Game::Game(){
this->initWindow();
this->initState();
}
// destructor
Game::~Game(){
delete this->window;
while(!this->states.empty()){
delete this->states.top();
this->states.pop();
}
}
// Functions
void Game:: updateDT(){
/* update the dt variable with the time it takes to update and render one frame */
this->dt = this->dtclock.restart().asSeconds();
}
void Game::updateSFLMEvents(){
while(this->window->pollEvent(this->event)){
if(this->event.type == sf::Event::Closed){
this->window->close();
}
}
}
void Game::update(){ // collect all the event
this-> updateDT();
this->updateSFLMEvents();
if(!this->states.empty()){ // state active
this->states.top()-> update(this->dt);
if (this->states.top()->getEnd()){
this->states.top()->endstate();
delete this->states.top();
this->states.pop();
}
}
// application end
else{
std::cout << "end the game" << std::endl;
this->window->close();
}
}
void Game::render(){ // respond to all the events
this->window->clear();
// Render object
if(!this->states.empty()){
this->states.top()-> render(this->window);
}
this->window->display();
}
void Game::run(){ // start game engine
while(this->window->isOpen()){
this->update();
this->render();
}
}