From 972088a297dbd96204f26755e8f744a6b67f4812 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Fri, 8 Aug 2014 03:20:03 +0200 Subject: [PATCH] Turned MenuStorage into a proper class --- src/control/joystick_manager.cpp | 6 ++-- src/control/keyboard_manager.cpp | 4 +-- src/supertux/menu/game_menu.cpp | 2 +- src/supertux/menu/main_menu.cpp | 2 +- src/supertux/menu/menu_storage.cpp | 55 ++++++++++++++++++++++++++----------- src/supertux/menu/menu_storage.hpp | 24 ++++++++++------ src/supertux/menu/options_menu.cpp | 6 ++-- src/supertux/menu/worldmap_menu.cpp | 2 +- src/supertux/screen_manager.cpp | 6 ++-- src/supertux/screen_manager.hpp | 2 ++ 10 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/control/joystick_manager.cpp b/src/control/joystick_manager.cpp index 8fb665523..b4a82eb73 100644 --- a/src/control/joystick_manager.cpp +++ b/src/control/joystick_manager.cpp @@ -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; } diff --git a/src/control/keyboard_manager.cpp b/src/control/keyboard_manager.cpp index 5b3d0c242..d53ed3981 100644 --- a/src/control/keyboard_manager.cpp +++ b/src/control/keyboard_manager.cpp @@ -168,7 +168,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event) bind_key(event.keysym.sym, static_cast(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; diff --git a/src/supertux/menu/game_menu.cpp b/src/supertux/menu/game_menu.cpp index 7b1bf6d3e..1bdc59ba8 100644 --- a/src/supertux/menu/game_menu.cpp +++ b/src/supertux/menu/game_menu.cpp @@ -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")); } diff --git a/src/supertux/menu/main_menu.cpp b/src/supertux/menu/main_menu.cpp index a0dd678b3..5849be7f9 100644 --- a/src/supertux/menu/main_menu.cpp +++ b/src/supertux/menu/main_menu.cpp @@ -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")); } diff --git a/src/supertux/menu/menu_storage.cpp b/src/supertux/menu/menu_storage.cpp index 7350849ac..d2c6f1776 100644 --- a/src/supertux/menu/menu_storage.cpp +++ b/src/supertux/menu/menu_storage.cpp @@ -22,45 +22,68 @@ #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 */ diff --git a/src/supertux/menu/menu_storage.hpp b/src/supertux/menu/menu_storage.hpp index 8ac6bf2a3..b4b78b730 100644 --- a/src/supertux/menu/menu_storage.hpp +++ b/src/supertux/menu/menu_storage.hpp @@ -17,6 +17,8 @@ #ifndef HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP #define HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP +#include + 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 m_options_menu; + std::unique_ptr m_profile_menu; + std::unique_ptr m_key_options_menu; + std::unique_ptr m_joystick_options_menu; private: MenuStorage(const MenuStorage&); diff --git a/src/supertux/menu/options_menu.cpp b/src/supertux/menu/options_menu.cpp index 63d2472f0..b40eafc4e 100644 --- a/src/supertux/menu/options_menu.cpp +++ b/src/supertux/menu/options_menu.cpp @@ -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")); diff --git a/src/supertux/menu/worldmap_menu.cpp b/src/supertux/menu/worldmap_menu.cpp index 4a751e460..2334edbd7 100644 --- a/src/supertux/menu/worldmap_menu.cpp +++ b/src/supertux/menu/worldmap_menu.cpp @@ -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")); } diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index 8232329dc..724adc2c9 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -22,15 +22,16 @@ #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), diff --git a/src/supertux/screen_manager.hpp b/src/supertux/screen_manager.hpp index 0a8bf32f5..3794da623 100644 --- a/src/supertux/screen_manager.hpp +++ b/src/supertux/screen_manager.hpp @@ -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 m_menu_storage; std::unique_ptr m_menu_manager; bool running; float speed; -- 2.11.0