Use std::unique_ptr<> in ScreenManager
[supertux.git] / src / supertux / screen_manager.cpp
index 8232329..49468c2 100644 (file)
 #include "gui/menu_manager.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "scripting/time_scheduler.hpp"
-#include "supertux/constants.hpp"
 #include "supertux/console.hpp"
+#include "supertux/constants.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
 #include "supertux/main.hpp"
+#include "supertux/menu/menu_storage.hpp"
 #include "supertux/player_status.hpp"
 #include "supertux/resources.hpp"
-#include "supertux/screen_fade.hpp"
 #include "supertux/screen.hpp"
+#include "supertux/screen_fade.hpp"
 #include "supertux/timer.hpp"
 #include "video/drawing_context.hpp"
 #include "video/renderer.hpp"
@@ -43,6 +44,7 @@ static const int MAX_FRAME_SKIP = 2;
 
 ScreenManager::ScreenManager() :
   waiting_threads(),
+  m_menu_storage(new MenuStorage),
   m_menu_manager(new MenuManager),
   running(),
   speed(1.0), 
@@ -65,47 +67,39 @@ ScreenManager::~ScreenManager()
   using namespace scripting;
   delete TimeScheduler::instance;
   TimeScheduler::instance = NULL;
-
-  for(std::vector<Screen*>::iterator i = screen_stack.begin();
-      i != screen_stack.end(); ++i) {
-    delete *i;
-  }
 }
 
 void
-ScreenManager::push_screen(Screen* screen, ScreenFade* screen_fade)
+ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
 {
-  this->next_screen.reset(screen);
-  this->screen_fade.reset(screen_fade);
+  assert(!this->next_screen);
+  this->next_screen = std::move(screen);
+  this->screen_fade = std::move(screen_fade);
   nextpush = !nextpop;
   nextpop = false;
   speed = 1.0f;
 }
 
 void
-ScreenManager::exit_screen(ScreenFade* screen_fade)
+ScreenManager::exit_screen(std::unique_ptr<ScreenFade> screen_fade)
 {
-  next_screen.reset(NULL);
-  this->screen_fade.reset(screen_fade);
+  next_screen.reset();
+  this->screen_fade = std::move(screen_fade);
   nextpop = true;
   nextpush = false;
 }
 
 void
-ScreenManager::set_screen_fade(ScreenFade* screen_fade)
+ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
 {
-  this->screen_fade.reset(screen_fade);
+  this->screen_fade = std::move(screen_fade);
 }
 
 void
-ScreenManager::quit(ScreenFade* screen_fade)
+ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
 {
-  for(std::vector<Screen*>::iterator i = screen_stack.begin();
-      i != screen_stack.end(); ++i)
-    delete *i;
   screen_stack.clear();
-
-  exit_screen(screen_fade);
+  exit_screen(std::move(screen_fade));
 }
 
 void
@@ -248,9 +242,10 @@ ScreenManager::process_events()
 void
 ScreenManager::handle_screen_switch()
 {
-  while( (next_screen.get() != NULL || nextpop) &&
-         has_no_pending_fadeout()) {
-    if(current_screen.get() != NULL) {
+  while((next_screen || nextpop) &&
+        has_no_pending_fadeout())
+  {
+    if(current_screen) {
       current_screen->leave();
     }
 
@@ -259,22 +254,20 @@ ScreenManager::handle_screen_switch()
         running = false;
         break;
       }
-      next_screen.reset(screen_stack.back());
+      next_screen = std::move(screen_stack.back());
       screen_stack.pop_back();
     }
-    if(nextpush && current_screen.get() != NULL) {
-      screen_stack.push_back(current_screen.release());
+    if(nextpush && current_screen) {
+      screen_stack.push_back(std::move(current_screen));
     }
 
     nextpush = false;
     nextpop = false;
     speed = 1.0;
-    Screen* next_screen_ptr = next_screen.release();
-    next_screen.reset(0);
-    if(next_screen_ptr)
-      next_screen_ptr->setup();
-    current_screen.reset(next_screen_ptr);
-    screen_fade.reset(NULL);
+    current_screen = std::move(next_screen);
+    if(current_screen)
+      current_screen->setup();
+    screen_fade.reset();
 
     waiting_threads.wakeup();
   }
@@ -290,7 +283,7 @@ ScreenManager::run(DrawingContext &context)
   while(running) {
 
     handle_screen_switch();
-    if(!running || current_screen.get() == NULL)
+    if(!running || !current_screen)
       break;
 
     Uint32 ticks = SDL_GetTicks();