Changed to smart pointers
authorNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 18:35:10 +0000 (19:35 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Sat, 11 Nov 2023 18:35:10 +0000 (19:35 +0100)
src/States.cc
src/States.h
src/_main.cc

index 143f2dc94ab9885e7bdb3bb7cd18716688609d8f..e79e9e170ea4458c26bc42c7e1efc22fd158ba88 100644 (file)
@@ -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<Game_state*>(context.current_state)); 
-        context.next_state = new Pause_menu{};
+    {   
+        context.running_game.reset(static_cast<Game_state*>(context.current_state.release()));
+        
+        context.next_state = std::make_unique<Pause_menu>();
         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<Game_state>();
     }
 }
 
@@ -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<Start_menu>();
+        context.running_game.release();
     }
 }
 
index 9af2a73a1269e7c49e0eb330001e21797d5a36b6..fc36a5ba7151a42ae91668452585a32f4d987cbd 100644 (file)
@@ -67,9 +67,9 @@ private:
 
 struct Context 
 {
-    State* current_state {nullptr};
-    State* next_state {nullptr};
-    Game_state* running_game {nullptr};
+    std::unique_ptr<State> current_state {nullptr};
+    std::unique_ptr<State> next_state {nullptr};
+    std::unique_ptr<Game_state> running_game {nullptr};
 };
 
 #endif
\ No newline at end of file
index bb0cf086097850d12b38d41f99669a4189fcdc7d..630aa2bde6bdb2dd2b611120c5effc636b2125fa 100644 (file)
@@ -20,7 +20,7 @@ int main ()
     };
 
     Context game_context{};
-    game_context.current_state = new Start_menu{};
+    game_context.current_state = std::make_unique<Start_menu>();
     
     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;
         }