From b70aefd5a8e26fc4da843ac9d547eced786e05c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Sat, 11 Nov 2023 19:35:10 +0100 Subject: [PATCH] Changed to smart pointers --- src/States.cc | 14 ++++++++------ src/States.h | 6 +++--- src/_main.cc | 3 ++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/States.cc b/src/States.cc index 143f2dc..e79e9e1 100644 --- a/src/States.cc +++ b/src/States.cc @@ -7,9 +7,10 @@ Game_state::Game_state() : game_map{}, player{}, pause_game{false} void Game_state::update(Context& context) { if (pause_game) - { - context.running_game = std::move(static_cast(context.current_state)); - context.next_state = new Pause_menu{}; + { + context.running_game.reset(static_cast(context.current_state.release())); + + context.next_state = std::make_unique(); pause_game = false; return; } @@ -59,7 +60,7 @@ void Start_menu::update(Context& context) { if (exit_menu) { - context.next_state = new Game_state{}; + context.next_state = std::make_unique(); } } @@ -87,7 +88,7 @@ void Start_menu::handle_input(sf::Event& event) } } -Pause_menu::Pause_menu() : texture{}, sprite{}, exit_game{false}, resume_game{false} +Pause_menu::Pause_menu() : texture{}, sprite{}, resume_game{false}, exit_game{false} { texture.loadFromFile("assets/kir.png"); sprite.setTexture(texture); @@ -105,7 +106,8 @@ void Pause_menu::update(Context& context) } else if (exit_game) { - context.next_state = new Start_menu{}; + context.next_state = std::make_unique(); + context.running_game.release(); } } diff --git a/src/States.h b/src/States.h index 9af2a73..fc36a5b 100644 --- a/src/States.h +++ b/src/States.h @@ -67,9 +67,9 @@ private: struct Context { - State* current_state {nullptr}; - State* next_state {nullptr}; - Game_state* running_game {nullptr}; + std::unique_ptr current_state {nullptr}; + std::unique_ptr next_state {nullptr}; + std::unique_ptr running_game {nullptr}; }; #endif \ No newline at end of file diff --git a/src/_main.cc b/src/_main.cc index bb0cf08..630aa2b 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -20,7 +20,7 @@ int main () }; Context game_context{}; - game_context.current_state = new Start_menu{}; + game_context.current_state = std::make_unique(); sf::Clock game_clock; while (window.isOpen()) @@ -46,6 +46,7 @@ int main () if (game_context.next_state != nullptr) { + game_context.current_state.release(); game_context.current_state = std::move(game_context.next_state); game_context.next_state = nullptr; } -- 2.30.2