From bc0ad0b9bc269d1b0bf870fde8c66d808e58a63d Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 4 May 2023 23:01:27 +0200 Subject: [PATCH] Handling window resize and fixed spelling --- src/config.h | 8 ++- src/coordinates/coordinate_transformer.cpp | 2 +- src/game/game.cpp | 59 ++++++++++++++++------ src/game/game.h | 17 ++++--- src/game/game_factory.cpp | 1 + src/game/game_factory.hpp | 2 +- src/main.cpp | 2 +- 7 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/config.h b/src/config.h index a551d8c..eafcd1d 100644 --- a/src/config.h +++ b/src/config.h @@ -3,12 +3,18 @@ #include +#define DEVELOPER_MODE true + +// FPS #define FRAME_RATE 60 #define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE)) +// Window settings #define ANTIALIASINGLEVEL 8 +#define KEY_REPEAT_ENABLED false -#define WOLRD_TO_ISO_SCALE 50.0f +// Graphic settings +#define WORLD_TO_ISO_SCALE 50.0f #endif //HOLESOME_CONFIG_H diff --git a/src/coordinates/coordinate_transformer.cpp b/src/coordinates/coordinate_transformer.cpp index 650ae43..ab286d0 100644 --- a/src/coordinates/coordinate_transformer.cpp +++ b/src/coordinates/coordinate_transformer.cpp @@ -12,7 +12,7 @@ IsometricCoordinates CoordinateTransformer::worldToIsometric(WorldCoordinates wo Eigen::Vector3f worldCoordinatesVector; worldCoordinatesVector << worldCoordinates.x, worldCoordinates.y, worldCoordinates.z; - Eigen::Vector3f isoCoordinatesVector = worldToIsometricMatrix * worldCoordinatesVector * WOLRD_TO_ISO_SCALE; + Eigen::Vector3f isoCoordinatesVector = worldToIsometricMatrix * worldCoordinatesVector * WORLD_TO_ISO_SCALE; return IsometricCoordinates( isoCoordinatesVector.x(), // x diff --git a/src/game/game.cpp b/src/game/game.cpp index 2ef4a87..6f5e814 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5,17 +5,21 @@ #include "game.h" #include "../config.h" -Game::Game(std::shared_ptr window) : window(std::move(window)), gameObjects() { +Game::Game(std::shared_ptr window) : window(std::move(window)), gameObjects() +{ } -void Game::run() { +void Game::run() +{ sf::Clock clock; sf::Time TimeSinceLastUpdate = sf::seconds(0); - while (window->isOpen()) { + while (window->isOpen()) + { processEvents(); TimeSinceLastUpdate += clock.restart(); - while (TimeSinceLastUpdate >= FRAME_TIME) { + while (TimeSinceLastUpdate >= FRAME_TIME) + { TimeSinceLastUpdate -= FRAME_TIME; update(); @@ -25,50 +29,73 @@ void Game::run() { } } -void Game::exit() { +void Game::exit() +{ window->close(); } -void Game::drawFrame() { +void Game::drawFrame() +{ window->clear(sf::Color::Black); - for (auto &gameObject: gameObjects) { + for (auto &gameObject: gameObjects) + { gameObject->draw(window.get()); } window->display(); } -Game::~Game() { - for (auto &gameObject: gameObjects) { +Game::~Game() +{ + for (auto &gameObject: gameObjects) + { delete gameObject; } } -void Game::addGameObject(GameObject *gameObject) { +void Game::addGameObject(GameObject *gameObject) +{ gameObjects.push_back(gameObject); } -void Game::update() { - for (auto &gameObject: gameObjects) { +void Game::update() +{ + for (auto &gameObject: gameObjects) + { gameObject->update(); } } - -void Game::processEvents() { +void Game::processEvents() +{ sf::Event event; - while (window->pollEvent(event)) { - switch (event.type) { + while (window->pollEvent(event)) + { + switch (event.type) + { case sf::Event::KeyPressed: + // 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(); + } break; case sf::Event::KeyReleased: break; case sf::Event::Closed: exit(); break; + case sf::Event::Resized: + handleWindowResize(event); + break; default: break; } } } + +void Game::handleWindowResize(const sf::Event &event) +{ + window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height))); +} diff --git a/src/game/game.h b/src/game/game.h index 37d3fc9..760f75c 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -10,28 +10,29 @@ #include "../logging/easylogging++.h" #include "game_object.h" -class Game { -private: - std::shared_ptr window; - std::vector gameObjects; - +class Game +{ public: explicit Game(std::shared_ptr window); ~Game(); void run(); - /*** - * Stops the game and closes the window. - */ void exit(); void addGameObject(GameObject *gameObject); + +private: + std::shared_ptr window; + std::vector gameObjects; + void drawFrame(); void update(); void processEvents(); + + void handleWindowResize(const sf::Event &event); }; diff --git a/src/game/game_factory.cpp b/src/game/game_factory.cpp index 8f980d4..04f5f1d 100644 --- a/src/game/game_factory.cpp +++ b/src/game/game_factory.cpp @@ -44,4 +44,5 @@ sf::ContextSettings GameFactory::getAdditionalSettings() { void GameFactory::applyAdditionalWindowConfig(sf::RenderWindow *window) { window->setFramerateLimit(FRAME_RATE); + window->setKeyRepeatEnabled(KEY_REPEAT_ENABLED); } diff --git a/src/game/game_factory.hpp b/src/game/game_factory.hpp index c042084..f8055e4 100644 --- a/src/game/game_factory.hpp +++ b/src/game/game_factory.hpp @@ -9,7 +9,7 @@ class GameFactory { public: - static std::shared_ptr createWindowed(const std::string &title, int width, int height); + static std::shared_ptr createWindowed(const std::string &title, int width = 960, int height = 540); static std::shared_ptr createFullscreen(const std::string &title); private: diff --git a/src/main.cpp b/src/main.cpp index c0841a4..50c9d4b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char *argv[]) { START_EASYLOGGINGPP(argc, argv); - auto game = GameFactory::createFullscreen("Holesome"); + auto game = GameFactory::createWindowed("Holesome"); game->addGameObject(new GridDebugLayer(0, 50, 0, 50));