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-28 22:59:24 +02:00
|
|
|
#include <SFML/Graphics/RenderWindow.hpp>
|
2023-04-26 22:48:15 +02:00
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), isRunning(false), gameObjects()
|
|
|
|
{
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +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;
|
2023-04-28 22:59:24 +02:00
|
|
|
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{};
|
2023-04-28 22:59:24 +02:00
|
|
|
while (window->pollEvent(event))
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
// If the event is to close the window, then close it
|
2023-04-28 22:59:24 +02:00
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q)
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
void Game::exit()
|
|
|
|
{
|
2023-04-26 22:48:15 +02:00
|
|
|
isRunning = false;
|
2023-04-28 22:59:24 +02:00
|
|
|
window->close();
|
2023-04-27 23:05:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
void Game::renderFrame()
|
|
|
|
{
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
|
|
|
gameObject->draw(*window, sf::RenderStates::Default);
|
2023-04-27 23:05:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
window->display();
|
2023-04-27 23:05:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
Game::~Game()
|
|
|
|
{
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
delete gameObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 22:59:24 +02:00
|
|
|
void Game::addGameObject(GameObject *gameObject)
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
gameObjects.push_back(gameObject);
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|