// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+// 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
m_menu_storage(new MenuStorage),
m_menu_manager(new MenuManager),
m_speed(1.0),
- m_action(NO_ACTION),
+ m_actions(),
m_fps(0),
- m_next_screen(),
m_screen_fade(),
m_screen_stack(),
m_screenshot_requested(false)
log_debug << "ScreenManager::push_screen(): " << screen.get() << std::endl;
assert(screen);
- if (m_action == PUSH_ACTION)
- {
- assert(m_next_screen);
- // this happens for example when a Screen::setup() calls
- // push_screen() itself, i.e. GameSessions/LevelIntro, in that
- // case just commit the last action directly to the stack
- m_screen_stack.push_back(std::move(m_next_screen));
- m_action = NO_ACTION;
- }
-
- assert(m_action == NO_ACTION || m_action == POP_ACTION);
-
- m_next_screen = std::move(screen);
m_screen_fade = std::move(screen_fade);
-
- if (m_action == POP_ACTION)
- {
- m_action = REPLACE_ACTION;
- }
- else
- {
- m_action = PUSH_ACTION;
- }
- m_speed = 1.0f;
+ m_actions.push_back(Action(Action::PUSH_ACTION, std::move(screen)));
}
void
ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
{
log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
- assert(m_action == NO_ACTION);
- m_next_screen.reset();
m_screen_fade = std::move(screen_fade);
- m_action = POP_ACTION;
+ m_actions.push_back(Action(Action::POP_ACTION));
}
void
ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
{
m_screen_fade = std::move(screen_fade);
- m_action = QUIT_ACTION;
+ m_actions.push_back(Action(Action::QUIT_ACTION));
}
void
void
ScreenManager::handle_screen_switch()
{
- if (m_action != NO_ACTION && !has_pending_fadeout())
+ if (has_pending_fadeout())
{
- if (m_action == POP_ACTION)
- {
- assert(!m_screen_stack.empty());
- m_action = NO_ACTION;
+ // wait till the fadeout is completed before switching screens
+ }
+ else
+ {
+ m_screen_fade.reset();
- m_screen_stack.back()->leave();
- m_screen_stack.pop_back();
+ // keep track of the current screen, as only that needs a call to Screen::leave()
+ Screen* current_screen = m_screen_stack.empty() ? nullptr : m_screen_stack.back().get();
- if (!m_screen_stack.empty())
- {
- m_screen_stack.back()->setup();
- }
- }
- else if (m_action == PUSH_ACTION)
+ // Screen::setup() might push more screens, so loop till everything is done
+ while (!m_actions.empty())
{
- assert(m_next_screen);
- m_action = NO_ACTION;
+ // move actions to a new vector since setup() might modify it
+ auto actions = std::move(m_actions);
- if (!m_screen_stack.empty())
+ for(auto it = actions.begin(); it != actions.end(); ++it)
{
- m_screen_stack.back()->leave();
- }
-
- m_screen_stack.push_back(std::move(m_next_screen));
- m_screen_stack.back()->setup();
- }
- else if (m_action == REPLACE_ACTION)
- {
- assert(!m_screen_stack.empty());
- assert(!m_next_screen);
- m_action = NO_ACTION;
-
- m_screen_stack.back()->leave();
- m_screen_stack.pop_back();
+ auto& action = *it;
- m_screen_stack.push_back(std::move(m_next_screen));
- m_screen_stack.back()->setup();
- }
- else if (m_action == QUIT_ACTION)
- {
- m_screen_stack.clear();
- }
+ switch (action.type)
+ {
+ case Action::POP_ACTION:
+ assert(!m_screen_stack.empty());
+ if (current_screen == m_screen_stack.back().get())
+ {
+ m_screen_stack.back()->leave();
+ }
+ m_screen_stack.pop_back();
+ break;
- m_speed = 1.0;
+ case Action::PUSH_ACTION:
+ assert(action.screen);
+
+ if (!m_screen_stack.empty())
+ {
+ if (current_screen == m_screen_stack.back().get())
+ {
+ m_screen_stack.back()->leave();
+ }
+ }
+ m_screen_stack.push_back(std::move(action.screen));
+ break;
- m_screen_fade.reset();
+ case Action::QUIT_ACTION:
+ m_screen_stack.clear();
+ break;
+ }
+ }
- m_waiting_threads.wakeup();
+ if (!m_screen_stack.empty())
+ {
+ m_screen_stack.back()->setup();
+ m_speed = 1.0;
+ m_waiting_threads.wakeup();
+ }
+ }
}
}
Uint32 last_ticks = 0;
Uint32 elapsed_ticks = 0;
- assert(m_action == PUSH_ACTION);
- m_screen_stack.push_back(std::move(m_next_screen));
- m_action = NO_ACTION;
+ handle_screen_switch();
while (!m_screen_stack.empty())
{
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+// 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
#include <cstddef>
#include "scripting/thread_queue.hpp"
+#include "supertux/screen.hpp"
class Console;
class DrawingContext;
std::unique_ptr<MenuManager> m_menu_manager;
float m_speed;
- enum Action { NO_ACTION, PUSH_ACTION, POP_ACTION, REPLACE_ACTION, QUIT_ACTION };
- Action m_action;
+ struct Action
+ {
+ enum Type { PUSH_ACTION, POP_ACTION, QUIT_ACTION };
+ Type type;
+ std::unique_ptr<Screen> screen;
+
+ Action(Type type_,
+ std::unique_ptr<Screen> screen_ = {}) :
+ type(type_),
+ screen(std::move(screen_))
+ {}
+ };
+
+ std::vector<Action> m_actions;
+
/// measured fps
float m_fps;
- std::unique_ptr<Screen> m_next_screen;
std::unique_ptr<ScreenFade> m_screen_fade;
std::vector<std::unique_ptr<Screen> > m_screen_stack;
bool m_screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */