From: Timo Kokkonen Date: Wed, 31 Mar 2010 16:54:29 +0000 (+0300) Subject: main loop: Separate simulation time from true time X-Git-Url: http://git.itanic.dy.fi/?p=sdl-planets;a=commitdiff_plain;h=457c225f5ccaa25d88c8bf9f6ac617721987809f main loop: Separate simulation time from true time 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 --- diff --git a/main.c b/main.c index c8d00a3..21841e4 100644 --- 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; - 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); @@ -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, - time * time_scale)) + step_time)) 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; } - 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(); - 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++; /*