Removed a global variable
[supertux.git] / src / mainloop.cpp
index 575005b..7e5a01b 100644 (file)
@@ -1,5 +1,5 @@
-//  $Id: worldmap.hpp 2800 2005-10-02 22:57:31Z matzebraun $
-// 
+//  $Id$
+//
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
@@ -12,7 +12,7 @@
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
@@ -32,6 +32,7 @@
 #include "script_manager.hpp"
 #include "screen.hpp"
 #include "timer.hpp"
+#include "player_status.hpp"
 
 // the engine will be run with a logical framerate of 64fps.
 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
@@ -43,7 +44,6 @@ MainLoop* main_loop = NULL;
 MainLoop::MainLoop()
   : speed(1.0)
 {
-  console.reset(new Console());
 }
 
 MainLoop::~MainLoop()
@@ -65,6 +65,10 @@ MainLoop::push_screen(Screen* screen)
 void
 MainLoop::exit_screen()
 {
+  if (screen_stack.size() < 1) {
+    quit();
+    return;
+  }
   next_screen.reset(screen_stack.back());
   nextpush = false;
   screen_stack.pop_back();
@@ -84,12 +88,22 @@ MainLoop::set_speed(float speed)
 }
 
 void
+MainLoop::draw_fps(DrawingContext& context, float fps_fps)
+{
+  char str[60];
+  snprintf(str, sizeof(str), "%3.1f", fps_fps);
+  const char* fpstext = "FPS";
+  context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
+  context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);                    
+}
+
+void
 MainLoop::run()
 {
   DrawingContext context; 
   
   unsigned int frame_count;
-  float fps_fps;
+  float fps_fps = 0;
   Uint32 fps_ticks = SDL_GetTicks();
   Uint32 fps_nextframe_ticks = SDL_GetTicks();
   Uint32 ticks;
@@ -130,8 +144,8 @@ MainLoop::run()
         /* just wait */
         // If we really have to wait long, then do an imprecise SDL_Delay()
         Uint32 diff = fps_nextframe_ticks - ticks;
-        if(diff > 15) {
-          SDL_Delay(diff - 10);
+        if(diff > 10) {
+          SDL_Delay(diff - 2);
         }
         ticks = SDL_GetTicks();
       }
@@ -142,7 +156,10 @@ MainLoop::run()
       current_screen->draw(context);
       if(Menu::current() != NULL)
           Menu::current()->draw(context);
-      console->draw(context);
+      Console::instance->draw(context);
+
+      if(config->show_fps)
+        draw_fps(context, fps_fps);
 
       context.do_drawing();
 
@@ -165,6 +182,7 @@ MainLoop::run()
     game_time += elapsed_time;
     ScriptManager::instance->update();
     current_screen->update(elapsed_time);
+    Console::instance->update(elapsed_time);
  
     main_controller->update();
     SDL_Event event;