]> git.itanic.dy.fi Git - sdl-planets/commitdiff
main.c: Make it possible to scale the time step at run time
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 20 Mar 2010 20:36:35 +0000 (22:36 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 20 Mar 2010 20:36:35 +0000 (22:36 +0200)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index e88ad1b00f1b115007018325db15533e67cc98da..6863b18c1970131993e6aaf1e593117f41deeb55 100644 (file)
--- a/main.c
+++ b/main.c
@@ -23,10 +23,14 @@ static void clear_buf(SDL_Surface *screen)
                buf[i] = 0;
 }
 
-static int poll_events(SDL_Surface **screen, struct camera *cam)
+static int poll_events(SDL_Surface **screen, struct camera *cam,
+                      double *time_scale, double time)
 {
+       static double time_scale_rate = 1;
        SDL_Event event;
 
+       *time_scale *= pow(time_scale_rate, time);
+
        while (SDL_PollEvent(&event)) {
                switch (event.type) {
                case SDL_KEYDOWN:
@@ -54,6 +58,12 @@ static int poll_events(SDL_Surface **screen, struct camera *cam)
                        case SDLK_w:
                                cam->zoom_rate = 1 / CAM_ZOOM_RATE;
                                break;
+                       case SDLK_s:
+                               time_scale_rate = 1.5;
+                               break;
+                       case SDLK_a:
+                               time_scale_rate = 1 / 1.5;
+                               break;
                        default:
                                break;
                        }
@@ -80,6 +90,12 @@ static int poll_events(SDL_Surface **screen, struct camera *cam)
                        case SDLK_w:
                                cam->zoom_rate = 1;
                                break;
+                       case SDLK_s:
+                               time_scale_rate = 1;
+                               break;
+                       case SDLK_a:
+                               time_scale_rate = 1;
+                               break;
                        default:
                                break;
                        }
@@ -104,7 +120,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
        int planets;
        int old_ticks, ticks, framecount = 0, last_fps_time = 0;
        int last_framecount = 0;
-       double time = 0, last_fps = 0;
+       double time = 0, last_fps = 0, time_scale = 1;
 
        init_camera(&camera);
 
@@ -120,7 +136,8 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        pl2 = list_to_planet(pl1->list.next);
                        list_for_each_entry_from(pl2, &planet->list, list) {
                                struct planet *ptmp;
-                               if (!gravitize_planets(pl1, pl2, time))
+                               if (!gravitize_planets(pl1, pl2,
+                                                      time * time_scale))
                                        continue;
 
                                ptmp = list_to_planet(pl2->list.prev);
@@ -128,7 +145,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                                pl2 = ptmp;
                        }
 
-                       move_planet(pl1, time);
+                       move_planet(pl1, time * time_scale);
                }
 
                SDL_LockSurface(screen);
@@ -146,7 +163,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
 
                move_camera(&camera, time);
 
-               if (poll_events(&screen, &camera))
+               if (poll_events(&screen, &camera, &time_scale, time))
                        return;
 
                old_ticks = ticks;
@@ -161,8 +178,10 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        last_framecount = framecount;
                        last_fps_time = ticks;
                }
-               printf("  \rFrames/s: %.2f, Frame: %d, planets: %d",
-                      last_fps, framecount++, planets);
+               printf("  \rFrames/s: %.2f, Frame: %d, planets: %d"
+                      ", scale %.2f, zoom %.2f",
+                      last_fps, framecount++, planets, time_scale,
+                      camera.zoom);
        }
 }