Skip to content

Commit

Permalink
Use std::unique_ptr<> in ScreenManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 8, 2014
1 parent f7be36d commit ee4978a
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 66 deletions.
12 changes: 6 additions & 6 deletions src/scripting/functions.cpp
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -104,15 +104,15 @@ 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)
{
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)
Expand Down
11 changes: 6 additions & 5 deletions src/supertux/game_session.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/supertux/levelintro.cpp
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions src/supertux/main.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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()));
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/supertux/menu/contrib_world_menu.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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())));
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/supertux/menu/main_menu.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/supertux/menu/worldmap_menu.cpp
Expand Up @@ -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"

Expand Down
55 changes: 23 additions & 32 deletions src/supertux/screen_manager.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/supertux/screen_manager.hpp
Expand Up @@ -39,8 +39,8 @@ class ScreenManager
~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;
Expand All @@ -51,8 +51,8 @@ class ScreenManager
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;
Expand All @@ -77,7 +77,7 @@ class ScreenManager
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 */
};

Expand Down
4 changes: 2 additions & 2 deletions src/supertux/textscroller.cpp
Expand Up @@ -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;
Expand All @@ -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)));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/supertux/world.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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())));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/worldmap/worldmap.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ee4978a

Please sign in to comment.