-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gra.cpp
109 lines (89 loc) · 1.78 KB
/
Gra.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
104
105
106
107
108
109
#include "Headers.h"
#include "Gra.h"
//INITALIZATION FUNCTIONS
void Gra::initWindow()
{
m_window = new sf::RenderWindow{ sf::VideoMode{Constants::windowWidth, Constants::windowHeigth},
"Path Finding", sf::Style::Close | sf::Style::Titlebar};
}
void Gra::initStates()
{
m_states.push(new MainMenuState{ m_window, &m_states});
}
//CONSTRUCTORS / DESRUCTORS
Gra::Gra()
{
this->initWindow();
this->initStates();
}
Gra::~Gra()
{
delete m_window;
while (!m_states.empty())
{
delete m_states.top();
m_states.pop();
}
}
//PUBLIC FUNCTIONS
void Gra::updateEvents()
{
while (m_window->pollEvent(m_event))
{
if (m_event.type == sf::Event::Closed)
m_window->close();
else if (m_event.type == sf::Event::KeyReleased && m_event.key.code == sf::Keyboard::Escape)
Constants::singleQuitClickDetection = true;
else if (m_event.type == sf::Event::MouseButtonReleased &&
m_event.mouseButton.button == sf::Mouse::Left)
{
Constants::insertStartPosition = true;
Constants::mouseRealeseDetection = true;
}
else if (m_event.type == sf::Event::MouseButtonReleased &&
m_event.mouseButton.button == sf::Mouse::Right)
{
Constants::insertEndPosition = true;
}
}
}
void Gra::endApplication()
{
std::cout << "Ending app";
}
void Gra::update()
{
this->updateEvents();
if (!m_states.empty())
{
m_states.top()->update(m_window);
if (m_states.top()->getQuit())
{
m_states.top()->endState();
delete m_states.top();
m_states.pop();
}
}
else //End application if m_states is empty
{
m_window->close();
this->endApplication();
}
}
void Gra::render()
{
m_window->clear();
if (!m_states.empty())
{
m_states.top()->render(m_window);
}
m_window->display();
}
void Gra::runGame()
{
while (m_window->isOpen())
{
this->update();
this->render();
}
}