Only pause game on focus lose when game session is active
[supertux.git] / src / supertux / screen_manager.cpp
index b8d26ef..e1c3376 100644 (file)
@@ -1,5 +1,6 @@
 //  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 "control/input_manager.hpp"
 #include "gui/menu.hpp"
 #include "gui/menu_manager.hpp"
+#include "scripting/scripting.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "scripting/time_scheduler.hpp"
 #include "supertux/console.hpp"
 #include "supertux/constants.hpp"
 #include "supertux/gameconfig.hpp"
+#include "supertux/game_session.hpp"
 #include "supertux/globals.hpp"
 #include "supertux/main.hpp"
 #include "supertux/menu/menu_storage.hpp"
@@ -47,9 +50,8 @@ ScreenManager::ScreenManager() :
   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)
@@ -71,41 +73,17 @@ ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<Scree
   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
@@ -118,7 +96,7 @@ 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
@@ -151,7 +129,6 @@ ScreenManager::draw(DrawingContext& context)
   assert(!m_screen_stack.empty());
 
   static Uint32 fps_ticks = SDL_GetTicks();
-  static int frame_count = 0;
 
   m_screen_stack.back()->draw(context);
   m_menu_manager->draw(context);
@@ -161,7 +138,7 @@ ScreenManager::draw(DrawingContext& context)
     m_screen_fade->draw(context);
   }
 
-  Console::instance->draw(context);
+  Console::current()->draw(context);
 
   if (g_config->show_fps)
   {
@@ -179,6 +156,7 @@ ScreenManager::draw(DrawingContext& context)
   /* Calculate frames per second */
   if (g_config->show_fps)
   {
+    static int frame_count = 0;
     ++frame_count;
 
     if (SDL_GetTicks() - fps_ticks >= 500)
@@ -193,7 +171,7 @@ ScreenManager::draw(DrawingContext& context)
 void
 ScreenManager::update_gamelogic(float elapsed_time)
 {
-  scripting::update_debugger();
+  scripting::Scripting::current()->update_debugger();
   scripting::TimeScheduler::instance->update(game_time);
 
   if (!m_screen_stack.empty())
@@ -208,17 +186,17 @@ ScreenManager::update_gamelogic(float elapsed_time)
     m_screen_fade->update(elapsed_time);
   }
 
-  Console::instance->update(elapsed_time);
+  Console::current()->update(elapsed_time);
 }
 
 void
 ScreenManager::process_events()
 {
-  g_input_manager->update();
+  InputManager::current()->update();
   SDL_Event event;
   while (SDL_PollEvent(&event))
   {
-    g_input_manager->process_event(event);
+    InputManager::current()->process_event(event);
 
     m_menu_manager->event(event);
 
@@ -232,10 +210,18 @@ ScreenManager::process_events()
         switch(event.window.event)
         {
           case SDL_WINDOWEVENT_RESIZED:
-            Renderer::instance()->resize(event.window.data1,
-                                         event.window.data2);
+            VideoSystem::current()->resize(event.window.data1,
+                                           event.window.data2);
             m_menu_manager->on_window_resize();
             break;
+
+          case SDL_WINDOWEVENT_FOCUS_LOST:
+            if(GameSession::current() != NULL &&
+               GameSession::current()->is_active())
+            {
+              GameSession::current()->toggle_pause();
+            }
+            break;
         }
         break;
 
@@ -247,7 +233,7 @@ ScreenManager::process_events()
         else if (event.key.keysym.sym == SDLK_F11)
         {
           g_config->use_fullscreen = !g_config->use_fullscreen;
-          Renderer::instance()->apply_config();
+          VideoSystem::current()->apply_config();
           m_menu_manager->on_window_resize();
         }
         else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
@@ -258,10 +244,16 @@ ScreenManager::process_events()
         else if (event.key.keysym.sym == SDLK_F1 &&
                  event.key.keysym.mod & KMOD_CTRL)
         {
-          Console::instance->toggle();
+          Console::current()->toggle();
           g_config->console_enabled = true;
           g_config->save();
         }
+        else if (event.key.keysym.sym == SDLK_F2 &&
+                 event.key.keysym.mod & KMOD_CTRL)
+        {
+          g_config->developer_mode = !g_config->developer_mode;
+          log_info << "developer mode: " << g_config->developer_mode << std::endl;
+        }
         break;
     }
   }
@@ -276,56 +268,64 @@ ScreenManager::has_pending_fadeout() const
 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();
+      }
+    }
   }
 }
 
@@ -335,9 +335,7 @@ ScreenManager::run(DrawingContext &context)
   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())
   {
@@ -383,7 +381,7 @@ ScreenManager::run(DrawingContext &context)
       draw(context);
     }
 
-    sound_manager->update();
+    SoundManager::current()->update();
 
     handle_screen_switch();
   }