- Use obstacks for memory allocation for lispfiles and DrawingRequests,
[supertux.git] / src / mainloop.cpp
index 55a87d3..07d8536 100644 (file)
@@ -49,7 +49,7 @@ static const int MAX_FRAME_SKIP = 2;
 MainLoop* main_loop = NULL;
 
 MainLoop::MainLoop()
-  : speed(1.0), nextpop(false), nextpush(false)
+  : speed(1.0), nextpop(false), nextpush(false), fps(0)
 {
   using namespace Scripting;
   TimeScheduler::instance = new TimeScheduler();
@@ -115,8 +115,8 @@ 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_HUD);
-  context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_HUD);
+  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), ALIGN_LEFT, LAYER_HUD);
+  context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
 }
 
 void
@@ -173,6 +173,10 @@ MainLoop::process_events()
       Menu::current()->event(event);
     if(event.type == SDL_QUIT)
       quit();
+    else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11) {
+      config->use_fullscreen = !config->use_fullscreen;
+      init_video();
+    }
   }
 }
 
@@ -216,7 +220,6 @@ MainLoop::run()
 
   Uint32 last_ticks = 0;
   Uint32 elapsed_ticks = 0;
-  int skipped_frames = 0;
 
   running = true;
   while(running) {
@@ -229,34 +232,33 @@ MainLoop::run()
     elapsed_ticks += ticks - last_ticks;
     last_ticks = ticks;
 
-    /* time for a new logical frame? */
-    if(elapsed_ticks > TICKS_PER_FRAME) {
-      elapsed_ticks -= TICKS_PER_FRAME;
-      float timestep = 1.0 / LOGICAL_FPS;
-      real_time += timestep;
-      timestep *= speed;
-      game_time += timestep;
-
-      process_events();
-      update_gamelogic(timestep);
+    if (elapsed_ticks > TICKS_PER_FRAME*4) {
+      // when the game loads up or levels are switched the
+      // elapsed_ticks grows extremly large, so we just ignore those
+      // large time jumps
+      elapsed_ticks = 0;
     }
 
-    if(elapsed_ticks > TICKS_PER_FRAME) {
-      // too much time passed since last frame, the computer doesn't seem to be
-      // able to draw 64fps on screen, so skip 1 frame for now
-      skipped_frames++;
+    int frames = 0;
+
+    if (elapsed_ticks > TICKS_PER_FRAME) {
+      while(elapsed_ticks > TICKS_PER_FRAME && frames < MAX_FRAME_SKIP) {
+        elapsed_ticks -= TICKS_PER_FRAME;
+        float timestep = 1.0 / LOGICAL_FPS;
+        real_time += timestep;
+        timestep *= speed;
+        game_time += timestep;
 
-      // slow down the game if we skip too many frames
-      if(skipped_frames > MAX_FRAME_SKIP) {
-        elapsed_ticks = elapsed_ticks % TICKS_PER_FRAME;
-        draw(context);
-        skipped_frames = 0;
+        process_events();
+        update_gamelogic(timestep);
+        frames += 1;
       }
-    } else {
+
       draw(context);
-      skipped_frames = 0;
     }
 
     sound_manager->update();
+
+    SDL_Delay(0);
   }
 }