Pause the game when window loses focus, fixes #513
[supertux.git] / src / supertux / screen_manager.cpp
index a7f810b..decc603 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"
@@ -46,13 +49,9 @@ ScreenManager::ScreenManager() :
   m_waiting_threads(),
   m_menu_storage(new MenuStorage),
   m_menu_manager(new MenuManager),
-  m_running(),
   m_speed(1.0),
-  m_nextpop(false),
-  m_nextpush(false),
+  m_actions(),
   m_fps(0),
-  m_next_screen(),
-  m_current_screen(),
   m_screen_fade(),
   m_screen_stack(),
   m_screenshot_requested(false)
@@ -71,21 +70,20 @@ ScreenManager::~ScreenManager()
 void
 ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
 {
-  assert(!m_next_screen);
-  m_next_screen = std::move(screen);
+  log_debug << "ScreenManager::push_screen(): " << screen.get() << std::endl;
+  assert(screen);
+
   m_screen_fade = std::move(screen_fade);
-  m_nextpush = !m_nextpop;
-  m_nextpop = false;
-  m_speed = 1.0f;
+  m_actions.push_back(Action(Action::PUSH_ACTION, std::move(screen)));
 }
 
 void
-ScreenManager::exit_screen(std::unique_ptr<ScreenFade> screen_fade)
+ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
 {
-  m_next_screen.reset();
+  log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
+
   m_screen_fade = std::move(screen_fade);
-  m_nextpop = true;
-  m_nextpush = false;
+  m_actions.push_back(Action(Action::POP_ACTION));
 }
 
 void
@@ -97,8 +95,8 @@ ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
 void
 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
 {
-  m_screen_stack.clear();
-  exit_screen(std::move(screen_fade));
+  m_screen_fade = std::move(screen_fade);
+  m_actions.push_back(Action(Action::QUIT_ACTION));
 }
 
 void
@@ -113,12 +111,6 @@ ScreenManager::get_speed() const
   return m_speed;
 }
 
-bool
-ScreenManager::has_no_pending_fadeout() const
-{
-  return !m_screen_fade || m_screen_fade->done();
-}
-
 void
 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
 {
@@ -134,16 +126,19 @@ ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
 void
 ScreenManager::draw(DrawingContext& context)
 {
+  assert(!m_screen_stack.empty());
+
   static Uint32 fps_ticks = SDL_GetTicks();
-  static int frame_count = 0;
 
-  m_current_screen->draw(context);
+  m_screen_stack.back()->draw(context);
   m_menu_manager->draw(context);
+
   if (m_screen_fade)
   {
     m_screen_fade->draw(context);
   }
-  Console::instance->draw(context);
+
+  Console::current()->draw(context);
 
   if (g_config->show_fps)
   {
@@ -161,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)
@@ -175,25 +171,32 @@ 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);
-  m_current_screen->update(elapsed_time);
+
+  if (!m_screen_stack.empty())
+  {
+    m_screen_stack.back()->update(elapsed_time);
+  }
+
   m_menu_manager->process_input();
+
   if (m_screen_fade)
   {
     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);
 
@@ -207,10 +210,16 @@ 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()->toggle_pause();
+            }
+            break;
         }
         break;
 
@@ -222,7 +231,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 ||
@@ -233,48 +242,88 @@ 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;
     }
   }
 }
 
+bool
+ScreenManager::has_pending_fadeout() const
+{
+  return m_screen_fade && !m_screen_fade->done();
+}
+
 void
 ScreenManager::handle_screen_switch()
 {
-  while ((m_next_screen || m_nextpop) &&
-        has_no_pending_fadeout())
+  if (has_pending_fadeout())
   {
-    if (m_current_screen) {
-      m_current_screen->leave();
-    }
+    // wait till the fadeout is completed before switching screens
+  }
+  else
+  {
+    m_screen_fade.reset();
 
-    if (m_nextpop) {
-      if (m_screen_stack.empty()) {
-        m_running = false;
-        break;
-      }
-      m_next_screen = std::move(m_screen_stack.back());
-      m_screen_stack.pop_back();
-    }
-    if (m_nextpush && m_current_screen) {
-      m_screen_stack.push_back(std::move(m_current_screen));
-    }
+    // 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();
 
-    m_nextpush = false;
-    m_nextpop = false;
-    m_speed = 1.0;
-    m_current_screen = std::move(m_next_screen);
-    if (m_current_screen)
+    // Screen::setup() might push more screens, so loop till everything is done
+    while (!m_actions.empty())
     {
-      m_current_screen->setup();
-    }
-    m_screen_fade.reset();
+      // move actions to a new vector since setup() might modify it
+      auto actions = std::move(m_actions);
+
+      for(auto it = actions.begin(); it != actions.end(); ++it)
+      {
+        auto& action = *it;
+
+        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;
+
+          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;
+
+          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();
+      }
+    }
   }
 }
 
@@ -284,15 +333,10 @@ ScreenManager::run(DrawingContext &context)
   Uint32 last_ticks = 0;
   Uint32 elapsed_ticks = 0;
 
-  m_running = true;
-  while (m_running)
-  {
-    handle_screen_switch();
-    if (!m_running || !m_current_screen)
-    {
-      break;
-    }
+  handle_screen_switch();
 
+  while (!m_screen_stack.empty())
+  {
     Uint32 ticks = SDL_GetTicks();
     elapsed_ticks += ticks - last_ticks;
     last_ticks = ticks;
@@ -330,9 +374,14 @@ ScreenManager::run(DrawingContext &context)
       frames += 1;
     }
 
-    draw(context);
+    if (!m_screen_stack.empty())
+    {
+      draw(context);
+    }
+
+    SoundManager::current()->update();
 
-    sound_manager->update();
+    handle_screen_switch();
   }
 }