From ee4978ac9a13a7956bb7799056a2c97196af6cd5 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Fri, 8 Aug 2014 04:56:16 +0200 Subject: [PATCH] Use std::unique_ptr<> in ScreenManager --- src/scripting/functions.cpp | 12 +++---- src/supertux/game_session.cpp | 11 ++++--- src/supertux/levelintro.cpp | 2 +- src/supertux/main.cpp | 14 ++++---- src/supertux/menu/contrib_world_menu.cpp | 6 ++-- src/supertux/menu/main_menu.cpp | 7 ++-- src/supertux/menu/worldmap_menu.cpp | 1 + src/supertux/screen_manager.cpp | 55 +++++++++++++------------------- src/supertux/screen_manager.hpp | 10 +++--- src/supertux/textscroller.cpp | 4 +-- src/supertux/world.cpp | 3 +- src/worldmap/worldmap.cpp | 6 ++-- 12 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index ba0fc3fb5..f5a07a9b9 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -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(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(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()); } 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(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(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(new GameSession(filename, GameSession::current()->get_player_status()))); } void import(HSQUIRRELVM vm, const std::string& filename) diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 2b57b040c..f6bd71afd 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -32,13 +32,14 @@ #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(new LevelIntro(level.get(), best_level_statistics))); } } diff --git a/src/supertux/levelintro.cpp b/src/supertux/levelintro.cpp index 8db125177..bd8f34d7a 100644 --- a/src/supertux/levelintro.cpp +++ b/src/supertux/levelintro.cpp @@ -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(new FadeOut(0.1))); } player_sprite_py += player_sprite_vy * elapsed_time; diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index 00a58a979..40fab4c96 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -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( + new worldmap::WorldMap( + FileSystem::basename(g_config->start_level), default_playerstatus.get()))); } else { std::unique_ptr 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(new TitleScreen(default_playerstatus.get()))); } g_screen_manager->run(context); diff --git a/src/supertux/menu/contrib_world_menu.cpp b/src/supertux/menu/contrib_world_menu.cpp index 5d22b8e05..b4ef4a5e2 100644 --- a/src/supertux/menu/contrib_world_menu.cpp +++ b/src/supertux/menu/contrib_world_menu.cpp @@ -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( + new GameSession(m_current_world.get_level_filename(index), m_current_world.get_player_status()))); } } } diff --git a/src/supertux/menu/main_menu.cpp b/src/supertux/menu/main_menu.cpp index 5849be7f9..21b1fa044 100644 --- a/src/supertux/menu/main_menu.cpp +++ b/src/supertux/menu/main_menu.cpp @@ -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(new TextScroller("credits.txt")), + std::unique_ptr(new FadeOut(0.5))); break; case MNID_QUITMAINMENU: - g_screen_manager->quit(new FadeOut(0.25)); + g_screen_manager->quit(std::unique_ptr(new FadeOut(0.25))); sound_manager->stop_music(0.25); break; } diff --git a/src/supertux/menu/worldmap_menu.cpp b/src/supertux/menu/worldmap_menu.cpp index 29b877700..c54f0c839 100644 --- a/src/supertux/menu/worldmap_menu.cpp +++ b/src/supertux/menu/worldmap_menu.cpp @@ -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" diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index 724adc2c9..49468c21b 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -67,47 +67,39 @@ ScreenManager::~ScreenManager() using namespace scripting; delete TimeScheduler::instance; TimeScheduler::instance = NULL; - - for(std::vector::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, std::unique_ptr 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 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 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 screen_fade) { - for(std::vector::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(); diff --git a/src/supertux/screen_manager.hpp b/src/supertux/screen_manager.hpp index 3794da623..b0ae9d205 100644 --- a/src/supertux/screen_manager.hpp +++ b/src/supertux/screen_manager.hpp @@ -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 fade = {}); + void quit(std::unique_ptr 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, std::unique_ptr fade = {}); + void set_screen_fade(std::unique_ptr fade); /// threads that wait for a screenswitch scripting::ThreadQueue waiting_threads; @@ -77,7 +77,7 @@ private: std::unique_ptr current_screen; std::unique_ptr console; std::unique_ptr screen_fade; - std::vector screen_stack; + std::vector > screen_stack; bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */ }; diff --git a/src/supertux/textscroller.cpp b/src/supertux/textscroller.cpp index f5e7863a2..c99d361f3 100644 --- a/src/supertux/textscroller.cpp +++ b/src/supertux/textscroller.cpp @@ -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(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(new FadeOut(0.5))); } } diff --git a/src/supertux/world.cpp b/src/supertux/world.cpp index d1fe91e7f..be816883c 100644 --- a/src/supertux/world.cpp +++ b/src/supertux/world.cpp @@ -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(new WorldMap(basedir + "worldmap.stwm", get_player_status()))); } } diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index f21f854cc..c21fc0777 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -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(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(new GameSession(levelfile, player_status, &level->statistics)), + std::unique_ptr(new ShrinkFade(shrinkpos, 1.0f))); in_level = true; } catch(std::exception& e) { log_fatal << "Couldn't load level: " << e.what() << std::endl; -- 2.11.0