]> git.itanic.dy.fi Git - sdl-planets/commitdiff
main loop: Limit to max 60FPS
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 31 Mar 2010 16:44:43 +0000 (19:44 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 31 Mar 2010 16:44:43 +0000 (19:44 +0300)
Do not even try to draw more than 60FPS. If we have time left, do some
extra simulation steps.

As an extra bonus this patch fixes the timer usage which was broken
since we moved to higher clock resolution.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index 84b180e6e2b535ec4d7eb313dc2889f8bca03286..c8d00a369188255fb04454be1b901a4bacabd971 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,6 +6,8 @@
 #include "random.h"
 #include "planet.h"
 
+#define MAX_FPS 60
+
 static void fade_buf(SDL_Surface *screen, double amount)
 {
        int i;
@@ -143,9 +145,10 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
        struct camera camera;
        int planets;
        int framecount = 0, last_fps_time = 0;
-       int last_framecount = 0;
-       unsigned long long old_ticks, ticks;
-       double time = 0, last_fps = 0, time_scale = 1;
+       int last_framecount = 0, last_step_count = 0;
+       unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
+       unsigned long step_count = 0;
+       double time = 0, last_fps = 0, time_scale = 1, last_sps = 0;
 
        init_camera(&camera);
 
@@ -173,8 +176,27 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        move_planet(pl1, time * time_scale);
                }
 
-               SDL_LockSurface(screen);
+               move_camera(&camera, time);
+
+               if (poll_events(&screen, &camera, &time_scale, time))
+                       return;
 
+               old_ticks = ticks;
+               ticks = gettime();
+               time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
+               time = MIN(time, 0.02);
+               step_count++;
+
+               /*
+                * Do not draw to the screen more often than MAS_FPS
+                * per second
+                */
+               if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
+                       continue;
+
+               last_frame_tick = ticks;
+
+               SDL_LockSurface(screen);
                clear_buf(screen);
 
                list_for_each_entry(pl1, &planet->list, list) {
@@ -186,27 +208,28 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
 
                SDL_Flip(screen);
 
-               move_camera(&camera, time);
-
-               if (poll_events(&screen, &camera, &time_scale, time))
-                       return;
-
-               old_ticks = ticks;
-               ticks = gettime();
-               time = (ticks - old_ticks) / 1000.0;
-               time = MIN(time, 0.02);
-
-               if (last_fps_time + 500 < ticks) {
+               if (last_fps_time + (500 * 1000) < ticks) {
                        last_framecount = framecount - last_framecount;
-                       last_fps = last_framecount * 1000 /
+                       last_fps = last_framecount * 1000 * 1000 /
                                (float)(ticks - last_fps_time);
                        last_framecount = framecount;
+
+                       last_step_count = step_count - last_step_count;
+                       last_sps = last_step_count * 1000 * 1000 /
+                               (float)(ticks - last_fps_time);
+                       last_step_count = step_count;
+
                        last_fps_time = ticks;
                }
-               printf("  \rFrames/s: %.2f, Frame: %d, planets: %d"
-                      ", scale %.2f, zoom %.2f",
-                      last_fps, framecount++, planets, time_scale,
-                      camera.zoom);
+
+
+               printf("  \rFrames/s: %.2f, planets: %d"
+                      ", scale %.2f, zoom %.2f, steps/s: %.2f, %ld",
+                      last_fps, planets, time_scale,
+                      camera.zoom, last_sps, step_count);
+
+
+               framecount++;
        }
 }