-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
81 lines (65 loc) · 2.66 KB
/
Engine.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
#include "Engine.h"
/////////////////////////////////// Window ///////////////////////////////////
Engine::Engine() // Конструктор, инициализирующий экран с параметрами и камеру
{
srand(time(nullptr));
window.create(videoMode,
"croissant",
sf::Style::Fullscreen
);
view.setSize((float) videoMode.width / viewScale, (float) videoMode.height / viewScale);
renderTexture.create(videoMode.width,videoMode.height);
screenHolder = new ScreenHolder(&window, &renderTexture, &clock);
globals = new Globals(&window);
}
Engine::~Engine()
{
delete screenHolder;
delete globals;
EffectsHolders::Map::getInstance().clear();
}
/////////////////////////////////// Main loop ///////////////////////////////////
void Engine::runEngine() // Метод, запускающий игру
{
globals->map->loadMap(1, &window);
while (window.isOpen())
{
float deltaTime = clock.restart().asSeconds();
globals->player->move(deltaTime, globals->map->wallBounds, &view);
globals->updateAllMobs(screenHolder);
EffectsHolders::Map::getInstance().update();
checkEvents();
draw();
}
}
/////////////////////////////////// Action'ы ///////////////////////////////////
void Engine::checkEvents() // Метод, проверяющий все события с экраном в игре
{
sf::Event event = {};
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) screenHolder->pauseScreen->open();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::I)) screenHolder->inventoryScreen->open(globals->player);
}
}
}
/////////////////////////////////// Отрисовка ///////////////////////////////////
void Engine::draw() // Метод, вызывающий отрисовку всех объектов в игре
{
/* Отрисовка объектов на рендер-текстуру */
renderTexture.setView(view);
renderTexture.clear(sf::Color(38, 31, 51));
renderTexture.draw(*globals->map);
for (auto &mob : globals->allMobs) renderTexture.draw(*mob);
renderTexture.draw(*globals->player);
renderTexture.draw(EffectsHolders::Map::getInstance());
renderTexture.display();
/* Отрисовка рендер-текстуры на окне */
renderSprite.setTexture(renderTexture.getTexture());
window.clear();
window.draw(renderSprite);
window.display();
}