]> git.itanic.dy.fi Git - sdl-planets/commitdiff
main loop: Separate simulation time from true time
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 31 Mar 2010 16:54:29 +0000 (19:54 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 31 Mar 2010 16:54:29 +0000 (19:54 +0300)
This allows simulation and other actions, such as camera movements, to
use different time steps. Now camera will move the same speed
regardless of the simulation time (eg. very low fps scenarios).

Furthermore, the maximum time step value is always respected and won't
be exceeded even though user adjusts the simulation speed to higher.

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

diff --git a/main.c b/main.c
index c8d00a369188255fb04454be1b901a4bacabd971..21841e481817494b65bad270f3933f38bc27ef18 100644 (file)
--- a/main.c
+++ b/main.c
@@ -148,7 +148,8 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
        int last_framecount = 0, last_step_count = 0;
        unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
        unsigned long step_count = 0;
        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;
+       double last_fps = 0, time_scale = 1, last_sps = 0;
+       double step_time = 0, true_time = 0;
 
        init_camera(&camera);
 
 
        init_camera(&camera);
 
@@ -165,7 +166,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        list_for_each_entry_from(pl2, &planet->list, list) {
                                struct planet *ptmp;
                                if (!gravitize_planets(pl1, pl2,
                        list_for_each_entry_from(pl2, &planet->list, list) {
                                struct planet *ptmp;
                                if (!gravitize_planets(pl1, pl2,
-                                                      time * time_scale))
+                                                      step_time))
                                        continue;
 
                                ptmp = list_to_planet(pl2->list.prev);
                                        continue;
 
                                ptmp = list_to_planet(pl2->list.prev);
@@ -173,18 +174,19 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                                pl2 = ptmp;
                        }
 
                                pl2 = ptmp;
                        }
 
-                       move_planet(pl1, time * time_scale);
+                       move_planet(pl1, step_time);
                }
 
                }
 
-               move_camera(&camera, time);
+               move_camera(&camera, true_time);
 
 
-               if (poll_events(&screen, &camera, &time_scale, time))
+               if (poll_events(&screen, &camera, &time_scale, true_time))
                        return;
 
                old_ticks = ticks;
                ticks = gettime();
                        return;
 
                old_ticks = ticks;
                ticks = gettime();
-               time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
-               time = MIN(time, 0.02);
+               true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
+               step_time = true_time * time_scale;
+               step_time = MIN(step_time, 0.02);
                step_count++;
 
                /*
                step_count++;
 
                /*