Patch by Matt McCutchen to fix active waiting in main loop. (Bug 480)
authorwolfgangb <wolfgangb@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Wed, 26 Aug 2009 21:39:05 +0000 (21:39 +0000)
committerwolfgangb <wolfgangb@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Wed, 26 Aug 2009 21:39:05 +0000 (21:39 +0000)
git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@5908 837edb03-e0f3-0310-88ca-d4d4e8b29345

src/mainloop.cpp

index 0807a58..444d3d2 100644 (file)
@@ -295,29 +295,32 @@ MainLoop::run(DrawingContext &context)
       elapsed_ticks = 0;
     }
 
+    if(elapsed_ticks < ticks_per_frame)
+      {
+        Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
+        SDL_Delay(delay_ticks);
+        last_ticks += delay_ticks;
+        elapsed_ticks += delay_ticks;
+      }
+
     int frames = 0;
 
-    if (elapsed_ticks > ticks_per_frame
+    while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP
       {
-        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;
-
-            process_events();
-            update_gamelogic(timestep);
-            frames += 1;
-          }
-
-        draw(context);
+        elapsed_ticks -= ticks_per_frame;
+        float timestep = 1.0 / LOGICAL_FPS;
+        real_time += timestep;
+        timestep *= speed;
+        game_time += timestep;
+
+        process_events();
+        update_gamelogic(timestep);
+        frames += 1;
       }
 
-    sound_manager->update();
+    draw(context);
 
-    SDL_Delay(0);
+    sound_manager->update();
   }
 }