From ff871a6115b5f9b1b81c09891942eccbcf20679a Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Fri, 2 Apr 2010 15:54:27 +0300 Subject: [PATCH] Main loop: Collect simulation status variables into a structure This eases passing of the variables between function since there is no need to pass that many individual variables. Signed-off-by: Timo Kokkonen --- main.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index bfdd295..7f5e803 100644 --- 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); -- 2.45.0