Added flame fish.
[supertux.git] / src / gameloop.cpp
index a1ab946..97cecb9 100644 (file)
@@ -20,6 +20,7 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <iostream>
+#include <sstream>
 #include <cassert>
 #include <cstdio>
 #include <cstdlib>
@@ -36,7 +37,7 @@
 #include <ctype.h>
 #endif
 
-#include "app/defines.h"
+#include "defines.h"
 #include "app/globals.h"
 #include "gameloop.h"
 #include "video/screen.h"
@@ -74,10 +75,11 @@ GameSession::GameSession(const std::string& levelname_, int mode, bool flip_leve
 
   fps_timer.init(true);            
   frame_timer.init(true);
+  frame_rate.set_fps(100);
 
   context = new DrawingContext();
 
-  if(debug_mode)
+  if(flip_levels_mode)
     flip_level = true;
 
   restart_level();
@@ -189,8 +191,8 @@ void
 GameSession::start_timers()
 {
   time_left.start(level->time_left*1000);
-  st_pause_ticks_init();
-  update_time = st_get_ticks();
+  Ticks::pause_init();
+  frame_rate.start();
 }
 
 void
@@ -219,7 +221,7 @@ GameSession::on_escape_press()
       tux.key_event((SDLKey)keymap.fire, UP);
 
       Menu::set_current(game_menu);
-      st_pause_ticks_start();
+      Ticks::pause_start();
     }
 }
 
@@ -250,7 +252,7 @@ GameSession::process_events()
             {
               Menu::current()->event(event);
              if(!Menu::current())
-             st_pause_ticks_stop();
+             Ticks::pause_stop();
             }
 
           switch(event.type)
@@ -283,7 +285,7 @@ GameSession::process_events()
   else // normal mode
     {
       if(!Menu::current() && !game_pause)
-        st_pause_ticks_stop();
+        Ticks::pause_stop();
 
       SDL_Event event;
       while (SDL_PollEvent(&event))
@@ -293,7 +295,7 @@ GameSession::process_events()
             {
               Menu::current()->event(event);
               if(!Menu::current())
-                st_pause_ticks_stop();
+                Ticks::pause_stop();
             }
           else
             {
@@ -350,12 +352,12 @@ GameSession::process_events()
                             if(game_pause)
                               {
                                 game_pause = false;
-                                st_pause_ticks_stop();
+                                Ticks::pause_stop();
                               }
                             else
                               {
                                 game_pause = true;
-                                st_pause_ticks_start();
+                                Ticks::pause_start();
                               }
                           }
                         break;
@@ -573,10 +575,10 @@ GameSession::process_menu()
           switch (game_menu->check())
             {
             case MNID_CONTINUE:
-              st_pause_ticks_stop();
+              Ticks::pause_stop();
               break;
             case MNID_ABORTLEVEL:
-              st_pause_ticks_stop();
+              Ticks::pause_stop();
               exit_status = ES_LEVEL_ABORT;
               break;
             }
@@ -600,7 +602,7 @@ GameSession::run()
   
   int fps_cnt = 0;
 
-  update_time = last_update_time = st_get_ticks();
+  frame_rate.start();
 
   // Eat unneeded events
   SDL_Event event;
@@ -611,7 +613,7 @@ GameSession::run()
   while (exit_status == ES_NONE)
     {
       /* Calculate the movement-factor */
-      double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
+      double frame_ratio = frame_rate.get();
 
       if(!frame_timer.check())
         {
@@ -652,18 +654,7 @@ GameSession::run()
           continue;
         }
 
-      /* Set the time of the last update and the time of the current update */
-      last_update_time = update_time;
-      update_time      = st_get_ticks();
-
-      /* Pause till next frame, if the machine running the game is too fast: */
-      /* FIXME: Works great for in OpenGl mode, where the CPU doesn't have to do that much. But
-         the results in SDL mode aren't perfect (thought the 100 FPS are reached), even on an AMD2500+. */
-      if(last_update_time >= update_time - 12) 
-        {
-          SDL_Delay(10);
-          update_time = st_get_ticks();
-        }
+      frame_rate.update();
 
       /* Handle time: */
       if (!time_left.check() && currentsector->player->dying == DYING_NOT
@@ -809,12 +800,14 @@ GameSession::drawresultscreen(void)
 
 std::string slotinfo(int slot)
 {
-  char tmp[1024];
-  char slotfile[1024];
+  std::string tmp;
+  std::string slotfile;
   std::string title;
-  sprintf(slotfile,"%s/slot%d.stsg",st_save_dir,slot);
+  std::stringstream stream;
+  stream << slot;
+  slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
 
-  lisp_object_t* savegame = lisp_read_from_file(slotfile);
+  lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
   if (savegame)
     {
       LispReader reader(lisp_cdr(savegame));
@@ -822,15 +815,15 @@ std::string slotinfo(int slot)
       lisp_free(savegame);
     }
 
-  if (access(slotfile, F_OK) == 0)
+  if (access(slotfile.c_str(), F_OK) == 0)
     {
       if (!title.empty())
-        snprintf(tmp,1024,"Slot %d - %s",slot, title.c_str());
+        tmp = "Slot " + stream.str() + " - " + title;
       else
-        snprintf(tmp, 1024,_("Slot %d - Savegame"),slot);
+        tmp = "Slot " + stream.str() + " - Savegame";
     }
   else
-    sprintf(tmp,_("Slot %d - Free"),slot);
+    tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
 
   return tmp;
 }
@@ -841,26 +834,33 @@ bool process_load_game_menu()
 
   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
     {
-      char slotfile[1024];
-      snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot);
+      std::stringstream stream;
+      stream << slot;
+      std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
 
-      if (access(slotfile, F_OK) != 0)
+      if (access(slotfile.c_str(), F_OK) != 0)
         {
           draw_intro();
         }
 
       // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
       fadeout(256);
+
+      DrawingContext context;
+      context.draw_text_center(white_text, "Loading...",
+                               Vector(0, screen->h/2), LAYER_FOREGROUND1);
+      context.do_drawing();
+
       WorldMapNS::WorldMap worldmap;
-     
+
       // Load the game or at least set the savegame_file variable
       worldmap.loadgame(slotfile);
 
       worldmap.display();
-      
+
       Menu::set_current(main_menu);
 
-      st_pause_ticks_stop();
+      Ticks::pause_stop();
       return true;
     }
   else