-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.cpp
More file actions
139 lines (109 loc) · 3.25 KB
/
Copy pathGameEngine.cpp
File metadata and controls
139 lines (109 loc) · 3.25 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "GameEngine.h"
#include "GameWindow.h"
#include "GameEngineConfig.h"
#include "LevelManager.h"
#include "AudioManager.h"
#include "Player.h"
#include "LoaderManager.h"
#include <SFML/Graphics.hpp>
#include <iostream>
GameEngine::GameEngine() {
}
GameEngine& GameEngine::getInstance() {
static GameEngine instance;
return instance;
}
void GameEngine::init() {
std::cout << "Inicjalizacja gry..." << std::endl;
LoaderManager::getInstance()->showLoadingScreen();
LoaderManager::getInstance()->loadAllAssets();
this->levelManager = LevelManager::getSingleton();
this->audioManager = AudioManager::getInstance();
setScale();
}
void GameEngine::render() {
if (levelManager != nullptr) {
levelManager->drawLevel();
}
for (Explosion* explosion : explosions) {
explosion->draw();
}
}
void GameEngine::update() {
if (levelManager != nullptr) {
this->levelManager->updateLevel();
}
for (int i = 0; i < explosions.size(); ++i) {
explosions[i]->update();
if (explosions[i]->isFinished()) {
delete explosions[i];
explosions.erase(explosions.begin() + i);
--i;
}
}
}
void GameEngine::start() {
this->window = GameWindow::createWindow();
window.setKeyRepeatEnabled(true);
sf::View view = window.getDefaultView();
this->init();
while (window.isOpen()) {
deltaTime = deltaClock.restart().asSeconds();
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>() || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) window.close();
if (const auto* resized = event->getIf<sf::Event::Resized>()) {
view.setSize({
static_cast<float>(resized->size.x),
static_cast<float>(resized->size.y)
});
view.setCenter({ view.getSize().x / 2.f, view.getSize().y / 2.f });
window.setView(view);
setScale();
}
}
this->update();
window.clear();
this->render();
window.display();
}
}
sf::RenderWindow& GameEngine::getWindow() {
return this->window;
}
float GameEngine::getScale() const {
if (windowScale == -1) {
return 1.f;
}
return this->windowScale;
}
void GameEngine::setScale() {
if (!this->window.isOpen()) {
this->windowScale = 1.f;
return;
}
sf::Vector2u size = this->window.getSize();
/*float ratio = (float)this->window.getSize().x / this->window.getSize().y;
float targetRatio = (float)GameEngineConfig::WINDOW_WIDTH / GameEngineConfig::WINDOW_HEIGHT;
std::cout << ratio << " " << targetRatio << std::endl;*/
this->windowScale = 1;
return;
//if (ratio > targetRatio) {
// std::cout << "A: ";
// //std::cout << targetRatio << " " << "A" << " " << this->window.getSize().y << " " << GameEngineConfig::WINDOW_HEIGHT << " " << (float)this->window.getSize().y / GameEngineConfig::WINDOW_HEIGHT << std::endl;
// this->windowScale = (float)this->window.getSize().y / GameEngineConfig::WINDOW_HEIGHT;
// std::cout << this->windowScale << std::endl;
// return;
//}
// std::cout << "B: ";
// this->windowScale = (float)this->window.getSize().x / GameEngineConfig::WINDOW_WIDTH;
// std::cout << this->windowScale << std::endl;
}
float GameEngine::getDeltaTime() const {
return deltaTime;
}
void GameEngine::showCursor(bool value) {
this->window.setMouseCursorVisible(value);
}
void GameEngine::addExplosion(Explosion* explosion) {
explosions.push_back(explosion);
}