Turned MenuStorage into a proper class
authorIngo Ruhnke <grumbel@gmail.com>
Fri, 8 Aug 2014 01:20:03 +0000 (03:20 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Fri, 8 Aug 2014 03:53:51 +0000 (05:53 +0200)
src/control/joystick_manager.cpp
src/control/keyboard_manager.cpp
src/supertux/menu/game_menu.cpp
src/supertux/menu/main_menu.cpp
src/supertux/menu/menu_storage.cpp
src/supertux/menu/menu_storage.hpp
src/supertux/menu/options_menu.cpp
src/supertux/menu/worldmap_menu.cpp
src/supertux/screen_manager.cpp
src/supertux/screen_manager.hpp

index 8fb6655..b4a82eb 100644 (file)
@@ -143,7 +143,7 @@ JoystickManager::process_hat_event(const SDL_JoyHatEvent& jhat)
     if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
       bind_joyhat(jhat.which, SDL_HAT_RIGHT, Controller::Control(wait_for_joystick));
 
-    MenuStorage::get_joystick_options_menu()->update();
+    MenuStorage::instance().get_joystick_options_menu()->update();
     wait_for_joystick = -1;
   }
   else
@@ -191,7 +191,7 @@ JoystickManager::process_axis_event(const SDL_JoyAxisEvent& jaxis)
       else
         bind_joyaxis(jaxis.which, jaxis.axis + 1, Controller::Control(wait_for_joystick));
 
-      MenuStorage::get_joystick_options_menu()->update();
+      MenuStorage::instance().get_joystick_options_menu()->update();
       wait_for_joystick = -1;
     }
   }
@@ -232,7 +232,7 @@ JoystickManager::process_button_event(const SDL_JoyButtonEvent& jbutton)
     if(jbutton.state == SDL_PRESSED)
     {
       bind_joybutton(jbutton.which, jbutton.button, (Controller::Control)wait_for_joystick);
-      MenuStorage::get_joystick_options_menu()->update();
+      MenuStorage::instance().get_joystick_options_menu()->update();
       parent->reset();
       wait_for_joystick = -1;
     }
index 5b3d0c2..d53ed39 100644 (file)
@@ -168,7 +168,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
       bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
     }
     m_parent->reset();
-    MenuStorage::get_key_options_menu()->update();
+    MenuStorage::instance().get_key_options_menu()->update();
     wait_for_key = -1;
     return;
   }
@@ -178,7 +178,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
     if (event.keysym.sym == SDLK_ESCAPE) 
     {
       m_parent->reset();
-      MenuStorage::get_joystick_options_menu()->update();
+      MenuStorage::instance().get_joystick_options_menu()->update();
       m_parent->joystick_manager->wait_for_joystick = -1;
     }
     return;
index 7b1bf6d..1bdc59b 100644 (file)
@@ -26,7 +26,7 @@ GameMenu::GameMenu(const Level& level)
   add_label(level.name);
   add_hl();
   add_entry(MNID_CONTINUE, _("Continue"));
-  add_submenu(_("Options"), MenuStorage::get_options_menu());
+  add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
   add_hl();
   add_entry(MNID_ABORTLEVEL, _("Abort Level"));
 }
index a0dd678..5849be7 100644 (file)
@@ -39,7 +39,7 @@ MainMenu::MainMenu() :
   add_entry(MNID_STARTGAME, _("Start Game"));
   add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
   add_entry(MNID_ADDONS, _("Add-ons"));
-  add_submenu(_("Options"), MenuStorage::get_options_menu());
+  add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
   add_entry(MNID_CREDITS, _("Credits"));
   add_entry(MNID_QUITMAINMENU, _("Quit"));
 }
index 7350849..d2c6f17 100644 (file)
 #include "supertux/menu/keyboard_menu.hpp"
 #include "supertux/globals.hpp"
 
-OptionsMenu*  MenuStorage::options_menu = 0;
-ProfileMenu*  MenuStorage::profile_menu = 0;
-KeyboardMenu* MenuStorage::key_options_menu = 0;
-JoystickMenu* MenuStorage::joystick_options_menu = 0;
+MenuStorage* MenuStorage::s_instance = 0;
+
+MenuStorage&
+MenuStorage::instance()
+{
+  assert(s_instance);
+  return *s_instance;
+}
+
+MenuStorage::MenuStorage()
+{
+  assert(!s_instance);
+  s_instance = this;
+}
+
+MenuStorage::~MenuStorage()
+{
+  s_instance = nullptr;
+}
 
 OptionsMenu*
 MenuStorage::get_options_menu()
 {
-  options_menu = new OptionsMenu();
-  return options_menu;
+  if (!m_options_menu)
+  {
+    m_options_menu.reset(new OptionsMenu());
+  }
+
+  return m_options_menu.get();
 }
 
 ProfileMenu*
 MenuStorage::get_profile_menu()
 {
-  profile_menu = new ProfileMenu();
-  return profile_menu;
+  if (!m_profile_menu)
+  {
+    m_profile_menu.reset(new ProfileMenu());
+  }
+
+  return m_profile_menu.get();
 }
 
 KeyboardMenu*
 MenuStorage::get_key_options_menu()
 {
-  if (!key_options_menu)
-  { // FIXME: this in never freed
-    key_options_menu = new KeyboardMenu(g_input_manager);
+  if (!m_key_options_menu)
+  {
+    m_key_options_menu.reset(new KeyboardMenu(g_input_manager));
   }
 
-  return key_options_menu;
+  return m_key_options_menu.get();
 }
 
 JoystickMenu*
 MenuStorage::get_joystick_options_menu()
 {
-  if (!joystick_options_menu)
-  { // FIXME: this in never freed
-    joystick_options_menu = new JoystickMenu(g_input_manager);
+  if (!m_joystick_options_menu)
+  {
+    m_joystick_options_menu.reset(new JoystickMenu(g_input_manager));
   }
 
-  return joystick_options_menu;
+  return m_joystick_options_menu.get();
 }
 
 /* EOF */
index 8ac6bf2..b4b78b7 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP
 #define HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP
 
+#include <memory>
+
 class JoystickMenu;
 class KeyboardMenu;
 class Menu;
@@ -25,19 +27,25 @@ class ProfileMenu;
 
 class MenuStorage
 {
+private:
+  static MenuStorage* s_instance;
+public:
+  static MenuStorage& instance();
+
 public:
   MenuStorage();
+  ~MenuStorage();
 
-  static OptionsMenu*  get_options_menu();
-  static ProfileMenu*  get_profile_menu();
-  static KeyboardMenu* get_key_options_menu();
-  static JoystickMenu* get_joystick_options_menu();
+  OptionsMenu*  get_options_menu();
+  ProfileMenu*  get_profile_menu();
+  KeyboardMenu* get_key_options_menu();
+  JoystickMenu* get_joystick_options_menu();
 
 private:
-  static OptionsMenu*  options_menu;
-  static ProfileMenu*  profile_menu;
-  static KeyboardMenu* key_options_menu;
-  static JoystickMenu* joystick_options_menu;
+  std::unique_ptr<OptionsMenu>  m_options_menu;
+  std::unique_ptr<ProfileMenu>  m_profile_menu;
+  std::unique_ptr<KeyboardMenu> m_key_options_menu;
+  std::unique_ptr<JoystickMenu> m_joystick_options_menu;
 
 private:
   MenuStorage(const MenuStorage&);
index 63d2472..b40eafc 100644 (file)
@@ -55,7 +55,7 @@ OptionsMenu::OptionsMenu() :
   add_submenu(_("Select Language"), language_menu.get())
     ->set_help(_("Select a different language to display text in"));
 
-  add_submenu(_("Select Profile"), MenuStorage::get_profile_menu())
+  add_submenu(_("Select Profile"), MenuStorage::instance().get_profile_menu())
     ->set_help(_("Select a profile to play with"));
 
   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
@@ -192,10 +192,10 @@ OptionsMenu::OptionsMenu() :
     add_inactive(MNID_MUSIC, _("Music (disabled)"));
   }
   
-  add_submenu(_("Setup Keyboard"), MenuStorage::get_key_options_menu())
+  add_submenu(_("Setup Keyboard"), MenuStorage::instance().get_key_options_menu())
     ->set_help(_("Configure key-action mappings"));
 
-  add_submenu(_("Setup Joystick"), MenuStorage::get_joystick_options_menu())
+  add_submenu(_("Setup Joystick"), MenuStorage::instance().get_joystick_options_menu())
     ->set_help(_("Configure joystick control-action mappings"));
   add_hl();
   add_back(_("Back"));
index 4a751e4..2334edb 100644 (file)
@@ -25,7 +25,7 @@ WorldmapMenu::WorldmapMenu()
   add_label(_("Pause"));
   add_hl();
   add_entry(MNID_RETURNWORLDMAP, _("Continue"));
-  add_submenu(_("Options"), MenuStorage::get_options_menu());
+  add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
   add_hl();
   add_entry(MNID_QUITWORLDMAP, _("Quit World"));
 }
index 8232329..724adc2 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), 
index 0a8bf32..3794da6 100644 (file)
@@ -25,6 +25,7 @@
 class Console;
 class DrawingContext;
 class MenuManager;
+class MenuStorage;
 class Screen;
 class ScreenFade;
 
@@ -64,6 +65,7 @@ private:
   void handle_screen_switch();
 
 private:
+  std::unique_ptr<MenuStorage> m_menu_storage;
   std::unique_ptr<MenuManager> m_menu_manager;
   bool running;
   float speed;