From 4419e9bac922c3f303bd628001c21b3105a70807 Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sat, 27 Sep 2025 13:57:15 +0200 Subject: [PATCH] Move gameloop implementation to gamelopp.cpp --- cpp/Makefile | 4 ++-- cpp/src/gameloop.cpp | 41 ++++++++++++++++++++++++++++++++++++++ cpp/src/gameloop.hpp | 47 ++++++++++---------------------------------- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/cpp/Makefile b/cpp/Makefile index d7903b6..03c6397 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -2,8 +2,8 @@ all: test pathfinding # TODO add extra warnings # TODO linter? -pathfinding: src/main.cpp src/array.hpp src/sprite.cpp - g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp +pathfinding: + g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp test: src/test.cpp src/array.hpp g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp diff --git a/cpp/src/gameloop.cpp b/cpp/src/gameloop.cpp index e69de29..65f1b18 100644 --- a/cpp/src/gameloop.cpp +++ b/cpp/src/gameloop.cpp @@ -0,0 +1,41 @@ +#include +#include + +#include "gameloop.hpp" + +#include "pathfindingdemo.hpp" +#include "window.hpp" +#include "user_input.hpp" +#include "log.hpp" + +void GameLoop::Run() { + LOG_INFO("Running the game"); + while (!m_Game->IsExitRequested()) { + m_Game->HandleActions(m_UserInput->GetActions()); + m_Game->UpdatePlayerVelocity(); + + m_Window->ClearWindow(); + + // draw the map (terrain tiles) + const Map &map = m_Game->GetMap(); + const auto &tiles = map.GetMapTiles(); + for (size_t row = 0; row < tiles.size(); row++) { + for (size_t col = 0; col < tiles[row].size(); col++) { + // LOG_DEBUG("Drawing rect (", row, ", ", col, ")"); + m_Window->DrawRect( + map.TileToWorld(TilePos{row, col}), + map.GetTileSize(), tiles[row][col]->R, tiles[row][col]->G, + tiles[row][col]->B, tiles[row][col]->A); + } + } + + // draw all the entities (player etc) + for (auto &entity : m_Game->GetEntities()) { + m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite()); + } + + m_Window->Flush(); + // TODO measure fps + std::this_thread::sleep_for(std::chrono::milliseconds(30)); + } +} diff --git a/cpp/src/gameloop.hpp b/cpp/src/gameloop.hpp index ceec9f5..8c7fa79 100644 --- a/cpp/src/gameloop.hpp +++ b/cpp/src/gameloop.hpp @@ -1,55 +1,28 @@ #pragma once -#include #include -#include #include "pathfindingdemo.hpp" #include "window.hpp" #include "user_input.hpp" -#include "log.hpp" class GameLoop { public: GameLoop() = default; + ~GameLoop() = default; - void Run() { - LOG_INFO("Running the game"); - while (!m_Game->IsExitRequested()) { - m_Game->HandleActions(m_UserInput->GetActions()); - m_Game->UpdatePlayerVelocity(); + GameLoop(const GameLoop&) = delete; + GameLoop(GameLoop&&) = delete; + GameLoop& operator=(const GameLoop&) = delete; + GameLoop& operator=(GameLoop&&) = delete; + + void Run(); - m_Window->ClearWindow(); - - // draw the map (terrain tiles) - const Map &map = m_Game->GetMap(); - const auto &tiles = map.GetMapTiles(); - for (size_t row = 0; row < tiles.size(); row++) { - for (size_t col = 0; col < tiles[row].size(); col++) { - // LOG_DEBUG("Drawing rect (", row, ", ", col, ")"); - m_Window->DrawRect( - map.TileToWorld(TilePos{row, col}), - map.GetTileSize(), tiles[row][col]->R, tiles[row][col]->G, - tiles[row][col]->B, tiles[row][col]->A); - } - } - - // draw all the entities (player etc) - for (auto &entity : m_Game->GetEntities()) { - m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite()); - } - - m_Window->Flush(); - // TODO measure fps - std::this_thread::sleep_for(std::chrono::milliseconds(30)); - } - } - - inline void SetGame(std::unique_ptr x) { + void SetGame(std::unique_ptr x) { m_Game = std::move(x); } - inline void SetWindow(std::unique_ptr x) { m_Window = std::move(x); } - inline void SetUserInput(std::unique_ptr x) { + void SetWindow(std::unique_ptr x) { m_Window = std::move(x); } + void SetUserInput(std::unique_ptr x) { m_UserInput = std::move(x); }