#include #include #include #include "game.h" Game::Game(std::shared_ptr window) : window(std::move(window)), gameObjects() { InputMapper::setGame(this); } Game::~Game() { for (auto &gameObject: gameObjects) { delete gameObject; } } void Game::run() { sf::Clock clock; sf::Time TimeSinceLastUpdate = sf::seconds(0); while (window->isOpen()) { InputMapper::processEvents(); TimeSinceLastUpdate += clock.restart(); while (TimeSinceLastUpdate >= FRAME_TIME) { TimeSinceLastUpdate -= FRAME_TIME; update(); InputMapper::processEvents(); } drawFrame(); } } void Game::exit() { window->close(); } void Game::drawFrame() { window->clear(sf::Color::Black); for (auto &gameObject: gameObjects) { gameObject->draw(window.get()); } window->display(); } void Game::addGameObject(GameObject *gameObject) { gameObjects.push_back(gameObject); } void Game::update() { // Basic Updates for (auto &gameObject: gameObjects) { gameObject->update(this); } // Late updates for (auto &gameObject: gameObjects) { gameObject->lateUpdate(this); } }