-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
103 lines (84 loc) · 1.91 KB
/
Game.cpp
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
#include "Game.h"
//Private functions
void Game::initWindow()
{
this->window = new sf::RenderWindow(this->global.getScreenSize(), "Game", sf::Style::Titlebar | sf::Style::Close);
this->window->setFramerateLimit(60);
}
//Constructors / Destructors
Game::Game()
{
this->initWindow();
}
Game::~Game()
{
delete this->window;
}
//Accessors
const bool Game::running() const
{
return this->window->isOpen();
}
//Functions
void Game::pollEvents()
{
/*
@return void
Event polling (buttons and keys)
*/
sf::Event event;
this->global.setEvent(event);
while (this->window->pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
this->window->close();
break;
}
}
}
void Game::updateMousePositions()
{
/*
@return void
Updates mouse positions.
- Mouse position relative to window (Vector2i);
- Mouse position relative to view (Vector2f).
*/
sf::Vector2i mousePosWindow = sf::Mouse::getPosition(*this->window);
sf::Vector2f mousePosView = this->window->mapPixelToCoords(mousePosWindow);
this->global.setMousePos(mousePosView);
}
void Game::update(float dt)
{
/*
@return void
- Updates the external inputs;
- Updates the game objects:
- Player;
- Enemies;
- Background.
*/
this->pollEvents();
this->updateMousePositions();
//Update game objects
this->entity.update(dt);
}
void Game::render()
{
/*
@return void
- Clear old frame;
- Render objects:
- Background; //example
- Player; //example
- Enemies. //example
- Display frame in window.
*/
//Clear old frame
this->window->clear(sf::Color::Black);
//Draw game objects
this->entity.render(window);
//Display window
this->window->display();
}