2023-04-26 22:48:15 +02:00
|
|
|
//
|
|
|
|
// Created by max on 26.04.23.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <utility>
|
2023-04-27 23:05:19 +02:00
|
|
|
#include <SFML/Window/Event.hpp>
|
2023-04-26 22:48:15 +02:00
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
|
2023-04-27 23:05:19 +02:00
|
|
|
Game::Game(std::shared_ptr<Renderer> renderer) : renderer(std::move(renderer)), isRunning(false), gameObjects() {
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Game::run() {
|
|
|
|
if (isRunning) {
|
2023-04-27 23:05:19 +02:00
|
|
|
LOG(WARNING) << "Game is already running";
|
2023-04-26 22:48:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isRunning = true;
|
|
|
|
while (isRunning) {
|
2023-04-27 23:05:19 +02:00
|
|
|
renderFrame();
|
2023-04-26 22:48:15 +02:00
|
|
|
|
2023-04-27 23:05:19 +02:00
|
|
|
// Process any events that have occurred since the last iteration
|
|
|
|
sf::Event event{};
|
|
|
|
while (renderer->pollEvent(event)) {
|
|
|
|
// If the event is to close the window, then close it
|
|
|
|
if (event.type == sf::Event::Closed) {
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q) {
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 23:05:19 +02:00
|
|
|
void Game::exit() {
|
2023-04-26 22:48:15 +02:00
|
|
|
isRunning = false;
|
2023-04-27 23:05:19 +02:00
|
|
|
renderer->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::renderFrame() {
|
|
|
|
for (auto &gameObject: gameObjects) {
|
|
|
|
renderer->draw(gameObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::~Game() {
|
|
|
|
for (auto &gameObject: gameObjects) {
|
|
|
|
delete gameObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::addGameObject(GameObject *gameObject) {
|
|
|
|
gameObjects.push_back(gameObject);
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|