]> git.itanic.dy.fi Git - sdl-planets/commitdiff
main.c: Use quadtrees to draw only visible planets
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 8 Apr 2010 16:46:44 +0000 (19:46 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 8 Apr 2010 16:46:44 +0000 (19:46 +0300)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index c383af71e05f7fe7ad52c059000d0c4a3cd20e62..be927c7ae1518cea769236b6f5f93c19fcae4f28 100644 (file)
--- 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;
                 double range)
 {
        struct sim_status status;
-       struct planet *planet, *pl1, *pl2;
+       struct planet *planet, *pl1, *pl2, *planet_root;
        struct camera camera;
        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;
        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;
        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);
 
 
        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;
 
        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);
        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;
                        }
 
                                pl2 = ptmp;
                        }
 
-                       move_planet(pl1, step_time);
+                       planet_root = move_planet(pl1, step_time);
+                       planets++;
                }
 
                move_camera(&camera, true_time);
                }
 
                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);
                }
 
                        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);
 
 
                SDL_UnlockSurface(screen);
 
@@ -281,11 +297,10 @@ static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
                        last_fps_time = ticks;
                }
 
                        last_fps_time = ticks;
                }
 
-
                printf("  \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
                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,
                       last_fps, last_sps, planets, status.time_scale,
-                      camera.zoom, step_count);
+                      camera.zoom, step_count, visible_planets);
                fflush(stdout);
 
 
                fflush(stdout);