-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
101 lines (70 loc) · 1.92 KB
/
Game.cpp
File metadata and controls
101 lines (70 loc) · 1.92 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
//
// Created by danit on 21.10.2022.
//
#include "Game.h"
#include "States/PausedState.h"
/// Init functions
void Game::initWindow() {
this->window.create(sf::VideoMode(800, 600), "C++/SFML Mario");
this->window.setVerticalSyncEnabled(true);
this->window.setFramerateLimit(60);
}
void Game::initStates() {
this->states.push(std::make_shared<MainMenuState>(this->window, this->states));
}
/// Constructor / Destructor
Game::Game() {
this->dt = 0.f;
this->initWindow();
this->initStates();
}
Game::~Game() {
while (!this->states.empty()) {
this->states.pop();
}
}
/// Functions
void Game::updateDt() {
/// dt = the time it takes to render one frame
this->dt = this->dtClock.restart().asSeconds();
}
void Game::updateEvents() {
while (this->window.pollEvent(this->event)) {
if (this->event.type == sf::Event::Closed)
this->window.close();
if (this->event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
this->states.top()->setQuit(true);
if (this->event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::P && this->states.top()->getType() == 1)
this->states.push(std::make_shared<PausedState>(this->window, this->states));
}
}
void Game::update() {
this->updateEvents();
if (!this->states.empty()) {
this->states.top()->update(this->dt);
if (this->states.top()->getQuit()) {
this->states.pop();
}
}
/// Aplication end
else {
this->window.close();
}
}
void Game::render() {
window.clear();
if (!this->states.empty())
this->states.top()->render(this->window);
window.display();
}
void Game::run() {
while (this->window.isOpen()) {
this->updateDt();
this->update();
this->render();
}
}
Game &Game::get_game() {
static Game game;
return game;
}