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-03 15:31:52 +02:00
|
|
|
#include "../config.h"
|
2023-04-26 22:48:15 +02:00
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), gameObjects()
|
|
|
|
{
|
2023-04-26 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
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-03 15:31:52 +02:00
|
|
|
processEvents();
|
|
|
|
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();
|
|
|
|
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-03 15:31:52 +02:00
|
|
|
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
|
|
|
Game::~Game()
|
|
|
|
{
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-04-27 23:05:19 +02:00
|
|
|
delete gameObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
for (auto &gameObject: gameObjects)
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
gameObject->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:01:27 +02:00
|
|
|
void Game::processEvents()
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
sf::Event event;
|
2023-05-04 23:01:27 +02:00
|
|
|
while (window->pollEvent(event))
|
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
2023-05-03 15:31:52 +02:00
|
|
|
case sf::Event::KeyPressed:
|
2023-05-04 23:01:27 +02:00
|
|
|
// Close game on Escape or Q in DEV Mode
|
|
|
|
if (DEVELOPER_MODE && (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q))
|
|
|
|
{
|
|
|
|
exit();
|
|
|
|
}
|
2023-05-03 15:31:52 +02:00
|
|
|
break;
|
|
|
|
case sf::Event::KeyReleased:
|
|
|
|
break;
|
|
|
|
case sf::Event::Closed:
|
|
|
|
exit();
|
|
|
|
break;
|
2023-05-04 23:01:27 +02:00
|
|
|
case sf::Event::Resized:
|
|
|
|
handleWindowResize(event);
|
|
|
|
break;
|
2023-05-03 15:31:52 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-04 23:01:27 +02:00
|
|
|
|
|
|
|
void Game::handleWindowResize(const sf::Event &event)
|
|
|
|
{
|
|
|
|
window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
|
|
|
|
}
|