2023-04-26 22:48:15 +02:00
|
|
|
#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-05-07 12:27:11 +02:00
|
|
|
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)),
|
2023-05-24 14:00:51 +02:00
|
|
|
gameObjects(),
|
|
|
|
views()
|
2023-05-04 23:01:27 +02:00
|
|
|
{
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
2023-05-07 12:27:11 +02:00
|
|
|
Game::~Game()
|
|
|
|
{
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
|
|
|
delete gameObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
void Game::run()
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
sf::Clock clock;
|
|
|
|
sf::Time TimeSinceLastUpdate = sf::seconds(0);
|
2023-04-26 22:48:15 +02:00
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
while (window->isOpen())
|
|
|
|
{
|
2023-05-07 12:27:11 +02:00
|
|
|
|
2023-05-17 14:13:39 +02:00
|
|
|
InputMapper::getInstance()->processEvents();
|
2023-05-03 15:31:52 +02:00
|
|
|
TimeSinceLastUpdate += clock.restart();
|
2023-05-04 23:01:27 +02:00
|
|
|
while (TimeSinceLastUpdate >= FRAME_TIME)
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
TimeSinceLastUpdate -= FRAME_TIME;
|
2023-04-27 23:05:19 +02:00
|
|
|
|
2023-05-03 15:31:52 +02:00
|
|
|
update();
|
2023-05-17 14:13:39 +02:00
|
|
|
InputMapper::getInstance()->processEvents();
|
2023-04-27 23:05:19 +02:00
|
|
|
}
|
2023-05-03 15:31:52 +02:00
|
|
|
drawFrame();
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
void Game::exit()
|
|
|
|
{
|
2023-04-28 22:59:24 +02:00
|
|
|
window->close();
|
2023-04-27 23:05:19 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
void Game::drawFrame()
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
window->clear(sf::Color::Black);
|
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-05-20 00:10:03 +02:00
|
|
|
if (gameObject->isActive)
|
|
|
|
{
|
|
|
|
gameObject->draw(window.get());
|
|
|
|
}
|
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-05-04 23:01:27 +02:00
|
|
|
void Game::addGameObject(GameObject *gameObject)
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
gameObjects.push_back(gameObject);
|
|
|
|
}
|
2023-05-03 01:33:14 +02:00
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
void Game::update()
|
|
|
|
{
|
2023-05-14 22:23:25 +02:00
|
|
|
// Basic Updates
|
2023-05-04 23:01:27 +02:00
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-05-20 00:10:03 +02:00
|
|
|
if (gameObject->isActive)
|
|
|
|
{
|
2023-05-24 14:00:51 +02:00
|
|
|
gameObject->update();
|
2023-05-20 00:10:03 +02:00
|
|
|
}
|
2023-05-03 15:31:52 +02:00
|
|
|
}
|
2023-05-14 22:23:25 +02:00
|
|
|
|
|
|
|
// Late updates
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-05-20 00:10:03 +02:00
|
|
|
if (gameObject->isActive)
|
|
|
|
{
|
2023-05-24 14:00:51 +02:00
|
|
|
gameObject->lateUpdate();
|
2023-05-20 00:10:03 +02:00
|
|
|
}
|
2023-05-14 22:23:25 +02:00
|
|
|
}
|
2023-05-20 00:10:03 +02:00
|
|
|
|
|
|
|
InputMapper::getInstance()->updateIdentityEvents();
|
2023-05-03 15:31:52 +02:00
|
|
|
}
|
2023-05-17 14:13:39 +02:00
|
|
|
|
|
|
|
std::shared_ptr<Game> Game::getInstance()
|
|
|
|
{
|
2023-05-20 00:10:03 +02:00
|
|
|
if (singletonInstance == nullptr)
|
|
|
|
{
|
2023-05-17 14:13:39 +02:00
|
|
|
throw std::runtime_error("Game instance has to be initialized first.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return singletonInstance;
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:10:03 +02:00
|
|
|
std::shared_ptr<Game> Game::constructInstance(const std::shared_ptr<sf::RenderWindow> &window)
|
2023-05-17 14:13:39 +02:00
|
|
|
{
|
2023-05-20 00:10:03 +02:00
|
|
|
if (singletonInstance != nullptr)
|
|
|
|
{
|
2023-05-17 14:13:39 +02:00
|
|
|
throw std::runtime_error("Game instance has already been initialized.");
|
|
|
|
}
|
|
|
|
|
|
|
|
singletonInstance = std::make_shared<Game>(window);
|
|
|
|
|
|
|
|
return singletonInstance;
|
|
|
|
}
|
2023-05-24 14:00:51 +02:00
|
|
|
|
|
|
|
void Game::registerView(TrackingView *view)
|
|
|
|
{
|
|
|
|
views.push_back(view);
|
|
|
|
}
|