Use std::unique_ptr<> in ScreenManager
authorIngo Ruhnke <grumbel@gmail.com>
Fri, 8 Aug 2014 02:56:16 +0000 (04:56 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Fri, 8 Aug 2014 03:53:51 +0000 (05:53 +0200)
12 files changed:
src/scripting/functions.cpp
src/supertux/game_session.cpp
src/supertux/levelintro.cpp
src/supertux/main.cpp
src/supertux/menu/contrib_world_menu.cpp
src/supertux/menu/main_menu.cpp
src/supertux/menu/worldmap_menu.cpp
src/supertux/screen_manager.cpp
src/supertux/screen_manager.hpp
src/supertux/textscroller.cpp
src/supertux/world.cpp
src/worldmap/worldmap.cpp

index ba0fc3f..f5a07a9 100644 (file)
@@ -74,17 +74,17 @@ void exit_screen()
 
 void fadeout_screen(float seconds)
 {
-  g_screen_manager->set_screen_fade(new FadeOut(seconds));
+  g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>(new FadeOut(seconds)));
 }
 
 void shrink_screen(float dest_x, float dest_y, float seconds)
 {
-  g_screen_manager->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
+  g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>(new ShrinkFade(Vector(dest_x, dest_y), seconds)));
 }
 
 void abort_screenfade()
 {
-  g_screen_manager->set_screen_fade(NULL);
+  g_screen_manager->set_screen_fade(std::unique_ptr<ScreenFade>());
 }
 
 std::string translate(const std::string& text)
@@ -94,7 +94,7 @@ std::string translate(const std::string& text)
 
 void display_text_file(const std::string& filename)
 {
-  g_screen_manager->push_screen(new TextScroller(filename));
+  g_screen_manager->push_screen(std::unique_ptr<Screen>(new TextScroller(filename)));
 }
 
 void load_worldmap(const std::string& filename)
@@ -104,7 +104,7 @@ void load_worldmap(const std::string& filename)
   if(World::current() == NULL)
     throw std::runtime_error("Can't start WorldMap without active world.");
 
-  g_screen_manager->push_screen(new WorldMap(filename, World::current()->get_player_status()));
+  g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(filename, World::current()->get_player_status())));
 }
 
 void load_level(const std::string& filename)
@@ -112,7 +112,7 @@ void load_level(const std::string& filename)
   if(GameSession::current() == NULL)
     throw std::runtime_error("Can't start level without active level.");
 
-  g_screen_manager->push_screen(new GameSession(filename, GameSession::current()->get_player_status()));
+  g_screen_manager->push_screen(std::unique_ptr<Screen>(new GameSession(filename, GameSession::current()->get_player_status())));
 }
 
 void import(HSQUIRRELVM vm, const std::string& filename)
index 2b57b04..f6bd71a 100644 (file)
 #include "object/player.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "supertux/gameconfig.hpp"
-#include "supertux/levelintro.hpp"
 #include "supertux/globals.hpp"
-#include "supertux/player_status.hpp"
-#include "supertux/screen_manager.hpp"
-#include "supertux/menu/menu_storage.hpp"
+#include "supertux/levelintro.hpp"
 #include "supertux/menu/game_menu.hpp"
+#include "supertux/menu/menu_storage.hpp"
 #include "supertux/menu/options_menu.hpp"
+#include "supertux/player_status.hpp"
+#include "supertux/screen_fade.hpp"
+#include "supertux/screen_manager.hpp"
 #include "supertux/sector.hpp"
 #include "util/file_system.hpp"
 #include "util/gettext.hpp"
@@ -416,7 +417,7 @@ GameSession::setup()
   int total_stats_to_be_collected = level->stats.total_coins + level->stats.total_badguys + level->stats.total_secrets;
   if ((!levelintro_shown) && (total_stats_to_be_collected > 0)) {
     levelintro_shown = true;
-    g_screen_manager->push_screen(new LevelIntro(level.get(), best_level_statistics));
+    g_screen_manager->push_screen(std::unique_ptr<Screen>(new LevelIntro(level.get(), best_level_statistics)));
   }
 }
 
index 8db1251..bd8f34d 100644 (file)
@@ -60,7 +60,7 @@ LevelIntro::update(float elapsed_time)
      || controller->pressed(Controller::ACTION)
      || controller->pressed(Controller::MENU_SELECT)
      || controller->pressed(Controller::PAUSE_MENU)) {
-    g_screen_manager->exit_screen(new FadeOut(0.1));
+    g_screen_manager->exit_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.1)));
   }
 
   player_sprite_py += player_sprite_vy * elapsed_time;
index 00a58a9..40fab4c 100644 (file)
@@ -37,14 +37,15 @@ extern "C" {
 #include "control/input_manager.hpp"
 #include "math/random_generator.hpp"
 #include "physfs/ifile_stream.hpp"
-#include "physfs/physfs_sdl.hpp"
 #include "physfs/physfs_file_system.hpp"
+#include "physfs/physfs_sdl.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
 #include "supertux/player_status.hpp"
-#include "supertux/screen_manager.hpp"
 #include "supertux/resources.hpp"
+#include "supertux/screen_fade.hpp"
+#include "supertux/screen_manager.hpp"
 #include "supertux/title_screen.hpp"
 #include "util/file_system.hpp"
 #include "util/gettext.hpp"
@@ -540,8 +541,9 @@ Main::run(int argc, char** argv)
 
       if(g_config->start_level.size() > 4 &&
          g_config->start_level.compare(g_config->start_level.size() - 5, 5, ".stwm") == 0) {
-        g_screen_manager->push_screen(new worldmap::WorldMap(
-                                 FileSystem::basename(g_config->start_level), default_playerstatus.get()));
+        g_screen_manager->push_screen(std::unique_ptr<Screen>(
+                                        new worldmap::WorldMap(
+                                          FileSystem::basename(g_config->start_level), default_playerstatus.get())));
       } else {
         std::unique_ptr<GameSession> session (
           new GameSession(FileSystem::basename(g_config->start_level), default_playerstatus.get()));
@@ -554,10 +556,10 @@ Main::run(int argc, char** argv)
 
         if(g_config->record_demo != "")
           session->record_demo(g_config->record_demo);
-        g_screen_manager->push_screen(session.release());
+        g_screen_manager->push_screen(std::move(session));
       }
     } else {
-      g_screen_manager->push_screen(new TitleScreen(default_playerstatus.get()));
+      g_screen_manager->push_screen(std::unique_ptr<Screen>(new TitleScreen(default_playerstatus.get())));
     }
 
     g_screen_manager->run(context);
index 5d22b8e..b4ef4a5 100644 (file)
@@ -19,6 +19,7 @@
 #include "audio/sound_manager.hpp"
 #include "gui/menu_item.hpp"
 #include "supertux/globals.hpp"
+#include "supertux/screen_fade.hpp"
 #include "supertux/screen_manager.hpp"
 #include "supertux/title_screen.hpp"
 #include "supertux/world.hpp"
@@ -50,8 +51,9 @@ ContribWorldMenu::check_menu()
     if (get_item_by_id(index).kind == MN_ACTION) 
     {
       sound_manager->stop_music();
-      GameSession* session = new GameSession(m_current_world.get_level_filename(index), m_current_world.get_player_status());
-      g_screen_manager->push_screen(session);
+      g_screen_manager->push_screen(
+        std::unique_ptr<Screen>(
+          new GameSession(m_current_world.get_level_filename(index), m_current_world.get_player_status())));
     }
   }
 }
index 5849be7..21b1fa0 100644 (file)
@@ -24,6 +24,7 @@
 #include "supertux/menu/addon_menu.hpp"
 #include "supertux/menu/options_menu.hpp"
 #include "supertux/menu/contrib_menu.hpp"
+#include "supertux/screen_fade.hpp"
 #include "supertux/screen_manager.hpp"
 #include "supertux/textscroller.hpp"
 #include "supertux/title_screen.hpp"
@@ -72,12 +73,12 @@ MainMenu::check_menu()
 
     case MNID_CREDITS:
       MenuManager::instance().set_current(NULL);
-      g_screen_manager->push_screen(new TextScroller("credits.txt"),
-                                    new FadeOut(0.5));
+      g_screen_manager->push_screen(std::unique_ptr<Screen>(new TextScroller("credits.txt")),
+                                    std::unique_ptr<ScreenFade>(new FadeOut(0.5)));
       break;
 
     case MNID_QUITMAINMENU:
-      g_screen_manager->quit(new FadeOut(0.25));
+      g_screen_manager->quit(std::unique_ptr<ScreenFade>(new FadeOut(0.25)));
       sound_manager->stop_music(0.25);
       break;
   }
index 29b8777..c54f0c8 100644 (file)
@@ -19,6 +19,7 @@
 #include "gui/menu_manager.hpp"
 #include "supertux/menu/menu_storage.hpp"
 #include "supertux/menu/options_menu.hpp"
+#include "supertux/screen_fade.hpp"
 #include "supertux/screen_manager.hpp"
 #include "util/gettext.hpp"
 
index 724adc2..49468c2 100644 (file)
@@ -67,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
@@ -250,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();
     }
 
@@ -261,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();
   }
@@ -292,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();
index 3794da6..b0ae9d2 100644 (file)
@@ -39,8 +39,8 @@ public:
   ~ScreenManager();
 
   void run(DrawingContext &context);
-  void exit_screen(ScreenFade* fade = NULL);
-  void quit(ScreenFade* fade = NULL);
+  void exit_screen(std::unique_ptr<ScreenFade> fade = {});
+  void quit(std::unique_ptr<ScreenFade> fade = {});
   void set_speed(float speed);
   float get_speed() const;
   bool has_no_pending_fadeout() const;
@@ -51,8 +51,8 @@ public:
   void take_screenshot();
 
   // push new screen on screen_stack
-  void push_screen(Screen* screen, ScreenFade* fade = NULL);
-  void set_screen_fade(ScreenFade* fade);
+  void push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> fade = {});
+  void set_screen_fade(std::unique_ptr<ScreenFade> fade);
 
   /// threads that wait for a screenswitch
   scripting::ThreadQueue waiting_threads;
@@ -77,7 +77,7 @@ private:
   std::unique_ptr<Screen> current_screen;
   std::unique_ptr<Console> console;
   std::unique_ptr<ScreenFade> screen_fade;
-  std::vector<Screen*> screen_stack;
+  std::vector<std::unique_ptr<Screen> > screen_stack;
   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
 };
 
index f5e7863..c99d361 100644 (file)
@@ -107,7 +107,7 @@ TextScroller::update(float elapsed_time)
      )&& !(controller->pressed(Controller::UP))) // prevent skipping if jump with up is enabled
     scroll += SCROLL;
   if(controller->pressed(Controller::PAUSE_MENU)) {
-    g_screen_manager->exit_screen(new FadeOut(0.5));
+    g_screen_manager->exit_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.5)));
   }
 
   scroll += speed * elapsed_time;
@@ -134,7 +134,7 @@ TextScroller::draw(DrawingContext& context)
 
   if(y < 0 && !fading ) {
     fading = true;
-    g_screen_manager->exit_screen(new FadeOut(0.5));
+    g_screen_manager->exit_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.5)));
   }
 }
 
index d1fe91e..be81688 100644 (file)
@@ -22,6 +22,7 @@
 #include "scripting/serialize.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "supertux/globals.hpp"
+#include "supertux/screen_fade.hpp"
 #include "supertux/screen_manager.hpp"
 #include "supertux/player_status.hpp"
 #include "supertux/world.hpp"
@@ -152,7 +153,7 @@ World::run()
   } catch(std::exception& ) {
     // fallback: try to load worldmap worldmap.stwm
     using namespace worldmap;
-    g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
+    g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(basedir + "worldmap.stwm", get_player_status())));
   }
 }
 
index f21f854..c21fc07 100644 (file)
@@ -240,7 +240,7 @@ void
 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
 {
   g_screen_manager->exit_screen();
-  g_screen_manager->push_screen(new WorldMap(filename, player_status, force_spawnpoint));
+  g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(filename, player_status, force_spawnpoint)));
 }
 
 void
@@ -697,8 +697,8 @@ WorldMap::update(float delta)
           // update state and savegame
           save_state();
 
-          g_screen_manager->push_screen(new GameSession(levelfile, player_status, &level->statistics),
-                                   new ShrinkFade(shrinkpos, 1.0f));
+          g_screen_manager->push_screen(std::unique_ptr<Screen>(new GameSession(levelfile, player_status, &level->statistics)),
+                                        std::unique_ptr<ScreenFade>(new ShrinkFade(shrinkpos, 1.0f)));
           in_level = true;
         } catch(std::exception& e) {
           log_fatal << "Couldn't load level: " << e.what() << std::endl;