]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Main loop: Collect simulation status variables into a structure
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 2 Apr 2010 12:54:27 +0000 (15:54 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 2 Apr 2010 12:59:14 +0000 (15:59 +0300)
This eases passing of the variables between function since there is no
need to pass that many individual variables.

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

diff --git a/main.c b/main.c
index bfdd295459548a0c0400533f8dbf5a2ffc49ebe0..7f5e8038334b16f55053103f0dac1be32a4c332d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -45,13 +45,19 @@ static unsigned long gettime(void)
        return ret - start;
 }
 
-static int poll_events(SDL_Surface **screen, struct camera *cam,
-                      double *time_scale, double time)
+struct sim_status {
+       SDL_Surface *screen;
+       struct camera *cam;
+       double time_scale;
+};
+
+static int poll_events(struct sim_status *status, double time)
 {
        static double time_scale_rate = 1;
        SDL_Event event;
+       struct camera *cam = status->cam;
 
-       *time_scale *= pow(time_scale_rate, time);
+       status->time_scale *= pow(time_scale_rate, time);
 
        while (SDL_PollEvent(&event)) {
                switch (event.type) {
@@ -122,10 +128,10 @@ static int poll_events(SDL_Surface **screen, struct camera *cam,
                        }
                        break;
                case SDL_VIDEORESIZE:
-                       *screen = SDL_SetVideoMode(event.resize.w,
-                                                  event.resize.h,
-                                                  32,
-                                                  screen[0]->flags);
+                       status->screen = SDL_SetVideoMode(event.resize.w,
+                                                         event.resize.h,
+                                                         32,
+                                                         status->screen->flags);
                        break;
                case SDL_QUIT:
                        goto quit;
@@ -141,6 +147,7 @@ quit:
 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                 double range)
 {
+       struct sim_status status;
        struct planet *planet, *pl1, *pl2;
        struct camera camera;
        int planets;
@@ -148,11 +155,15 @@ 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 last_fps = 0, time_scale = 1, last_sps = 0;
+       double last_fps = 0, last_sps = 0;
        double step_time = 0, true_time = 0;
 
        init_camera(&camera);
 
+       status.cam = &camera;
+       status.time_scale = 1;
+       status.screen = screen;
+
        planet = malloc(sizeof(*planet));
        init_planet(planet);
        create_planets(planet, num_of_planets, total_mass, range);
@@ -179,13 +190,13 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
 
                move_camera(&camera, true_time);
 
-               if (poll_events(&screen, &camera, &time_scale, true_time))
+               if (poll_events(&status, true_time))
                        return;
 
                old_ticks = ticks;
                ticks = gettime();
                true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
-               step_time = true_time * time_scale;
+               step_time = true_time * status.time_scale;
                step_time = MIN(step_time, 0.02);
                step_count++;
 
@@ -227,7 +238,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
 
                printf("  \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
                       ", scale %.2f, zoom %.2f, step %d",
-                      last_fps, last_sps, planets, time_scale,
+                      last_fps, last_sps, planets, status.time_scale,
                       camera.zoom, step_count);
                fflush(stdout);