From f735117edd7cacc8432261e884e834ff00b31dcc Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 23 Oct 2025 10:59:51 +0200 Subject: [PATCH] Fix lifetime issues --- src/Game.cpp | 16 ++++++++-------- src/Game.hpp | 6 ++++-- src/main.cpp | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 163725e..07c72bd 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -2,11 +2,11 @@ #include "Player.hpp" #include "Game.hpp" -Game::Game() -{ - renderer = new GameRenderer("Zombo Shooter", 800, 450); - player = new Player(renderer); -} +#include + +Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer) {} + +Game::~Game() = default; void Game::run() const { @@ -18,10 +18,10 @@ void Game::run() const break; } - renderer->clear_screen(0x80, 0x40, 0xFF, 0xFF); + renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF); - player->draw(); + player.draw(); - renderer->flush(); + renderer.flush(); } } diff --git a/src/Game.hpp b/src/Game.hpp index 361b9bb..bbfc822 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -7,12 +7,14 @@ class Game { private: - GameRenderer *renderer; - Player *player; + GameRenderer renderer; + Player player; public: Game(); + ~Game(); + void run() const; }; diff --git a/src/main.cpp b/src/main.cpp index 6ccf308..2b1eae8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,9 +2,9 @@ int main() { - const Game *game = new Game(); + const Game game = Game(); - game->run(); + game.run(); return 0; }