Pause the game when window loses focus, fixes #513
[supertux.git] / src / supertux / screen_manager.cpp
index 9e11eed..decc603 100644 (file)
 #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"
@@ -127,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);
@@ -137,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)
   {
@@ -155,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)
@@ -169,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())
@@ -184,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);
 
@@ -208,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;
 
@@ -223,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 ||
@@ -234,10 +242,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;
     }
   }
@@ -365,7 +379,7 @@ ScreenManager::run(DrawingContext &context)
       draw(context);
     }
 
-    sound_manager->update();
+    SoundManager::current()->update();
 
     handle_screen_switch();
   }