From: Timo Kokkonen Date: Thu, 8 Apr 2010 16:46:44 +0000 (+0300) Subject: main.c: Use quadtrees to draw only visible planets X-Git-Url: http://git.itanic.dy.fi/?p=sdl-planets;a=commitdiff_plain;h=b9f1e8cf06dc6ff2569551aa0b427d8468f7a802 main.c: Use quadtrees to draw only visible planets Signed-off-by: Timo Kokkonen --- diff --git a/main.c b/main.c index c383af7..be927c7 100644 --- a/main.c +++ b/main.c @@ -184,8 +184,11 @@ 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 planet *planet, *pl1, *pl2, *planet_root; struct camera camera; + struct planet_search_iterator itr; + struct vector vect; + int planets; int framecount = 0, last_fps_time = 0; int last_framecount = 0, last_step_count = 0; @@ -193,6 +196,7 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass, unsigned long step_count = 0; double last_fps = 0, last_sps = 0, fade_amount; double step_time = 0, true_time = 0; + int visible_planets; init_camera(&camera); @@ -201,6 +205,11 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass, status.screen = screen; status.tracers_enabled = 0; + itr.screen = screen; + itr.cam = &camera; + itr.qt_iterator.direction = planet_search_rectangular; + itr.qt_iterator.callback = planet_draw_iterator; + planet = malloc(sizeof(*planet)); init_planet(planet); create_planets(planet, num_of_planets, total_mass, range); @@ -222,7 +231,8 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass, pl2 = ptmp; } - move_planet(pl1, step_time); + planet_root = move_planet(pl1, step_time); + planets++; } move_camera(&camera, true_time); @@ -256,10 +266,16 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass, clear_buf(screen); } - list_for_each_entry(pl1, &planet->list, list) { - draw_planet(screen, pl1, &camera); - planets++; - } + itr.limit[0] = camera.pos; + vect.x = screen->w / 2; + vect.y = screen->h / 2; + vector_scale(&vect, 1 / camera.zoom, &vect); + vector_add(&itr.limit[0], &vect, &itr.limit[1]); + vector_sub(&itr.limit[0], &vect, &itr.limit[0]); + + itr.qt_iterator.head = &planet_root->tree; + + visible_planets = walk_quadtree(&itr.qt_iterator); SDL_UnlockSurface(screen); @@ -281,11 +297,10 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass, last_fps_time = ticks; } - printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d" - ", scale %.2f, zoom %.2f, step %ld", + ", scale %.2f, zoom %.2f, step %ld, visible %d", last_fps, last_sps, planets, status.time_scale, - camera.zoom, step_count); + camera.zoom, step_count, visible_planets); fflush(stdout);