forked from ps-group/sfml-packman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (68 loc) · 2.1 KB
/
main.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
#include "packman.h"
#include <SFML/Graphics.hpp>
// -- объявления констант --
constexpr unsigned ANTIALIASING_LEVEL = 8;
constexpr unsigned WINDOW_WIDTH = 800;
constexpr unsigned WINDOW_HEIGHT = 600;
constexpr unsigned MAX_FPS = 60;
// Функция создаёт окно приложения.
void createWindow(sf::RenderWindow& window)
{
sf::ContextSettings settings;
settings.antialiasingLevel = ANTIALIASING_LEVEL;
window.create(
sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT),
"PacMan Game Clone", sf::Style::Default, settings);
window.setFramerateLimit(MAX_FPS);
}
// Функция обрабатывает все события, скопившиеся в очереди событий SFML.
void handleEvents(sf::RenderWindow& window, Packman& packman)
{
sf::Event event;
while (window.pollEvent(event))
{
// Кнопка закрытия окна
if (event.type == sf::Event::Closed)
{
window.close();
}
// Клавиши управления пакманом
else if (event.type == sf::Event::KeyPressed)
{
handlePackmanKeyPress(event.key, packman);
}
else if (event.type == sf::Event::KeyReleased)
{
handlePackmanKeyRelease(event.key, packman);
}
}
}
// Функция обновляет состояние объектов на сцене.
void update(sf::Clock& clock, Packman& packman)
{
const float elapsedTime = clock.getElapsedTime().asSeconds();
clock.restart();
updatePackman(packman, elapsedTime);
}
// Функция рисует объекты на сцене.
void render(sf::RenderWindow& window, const Packman& packman)
{
window.clear();
window.draw(packman.shape);
window.display();
}
int main(int, char* [])
{
sf::RenderWindow window;
createWindow(window);
Packman packman;
initializePackman(packman);
sf::Clock clock;
while (window.isOpen())
{
handleEvents(window, packman);
update(clock, packman);
render(window, packman);
}
return 0;
}