]> git.itanic.dy.fi Git - sdl-planets/commitdiff
work in proggress
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 5 Apr 2010 16:15:02 +0000 (19:15 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 5 Apr 2010 16:15:02 +0000 (19:15 +0300)
crash and burn

main.c
planet.c
planet.h
quadtree.c
quadtree.h

diff --git a/main.c b/main.c
index 2241d42b6e1f52ee4a3ec849e4e6f8b47b4a5448..cb8e3fbc1b9a6ad66b3e84c1695d42ca6e1b53f4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -184,8 +184,10 @@ 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;
+
        int planets;
        int framecount = 0, last_fps_time = 0;
        int last_framecount = 0, last_step_count = 0;
@@ -193,6 +195,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;
        double step_time = 0, true_time = 0;
+       int visible_planets;
 
        init_camera(&camera);
 
@@ -201,6 +204,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 +230,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 +265,10 @@ 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.x = cam->zoom
+               itr.qt_iterator.head = planet_root;
+
+               visible_planets = walk_quadtree(&itr.qt_iterator);
 
                SDL_UnlockSurface(screen);
 
@@ -279,11 +288,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);
 
 
index bdbde37dc3f587d3b4e6048e166c1eeb4c61a689..e9dadc0215fdc10958109254f2e371c411d155a4 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -244,3 +244,45 @@ int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
                return 2;
        return 3;
 }
+
+int planet_search_rectangular(struct quadtree *node,
+                             struct quadtree_iterator *itr)
+{
+       struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
+       struct planet *p = tree_to_planet(node);
+       int directions = 0, i;
+       int up[2], left[2], right[2], down[2];
+
+       for (i = 0; i < 2; i++) {
+               if (it->limit[i].x < p->pos.x)
+                       left[i] = 1;
+               else
+                       right[i] = 1;
+               if (it->limit[i].y < p->pos.y)
+                       up[i] = 1;
+               else
+                       down[i] = 1;
+       }
+
+       directions |= left[0] | left[1] | up [0] | up[1] ?
+               QUADTREE_UPLEFT : 0;
+       directions |= right[0] | right[1] | up [0] | up[1] ?
+               QUADTREE_UPRIGHT : 0;
+       directions |= left[0]  | left[1]  | down [0] | down[1] ?
+               QUADTREE_DOWNLEFT :0;
+       directions |= right[0] | right[1] | down [0] | down[1] ?
+               QUADTREE_DOWNRIGHT :0;
+       directions |= (directions == (QUADTREE_UPLEFT | QUADTREE_UPRIGHT |
+                                     QUADTREE_DOWNLEFT | QUADTREE_DOWNRIGHT)) ?
+               QUADTREE_SELF : 0;
+
+       return directions;
+}
+
+void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it)
+{
+       struct planet *p = tree_to_planet(node);
+       struct planet_search_iterator *i = qt_itr_to_planet_itr(it);
+
+       draw_planet(i->screen, p, i->cam);
+}
index 248615a83f122f41dd9b12e76b8222caad0527f9..0673c87bb6267ec84c39db7573835e95c18a60bc 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -19,9 +19,20 @@ struct planet {
        unsigned char r, g, b;
 };
 
+struct planet_search_iterator {
+       struct quadtree_iterator qt_iterator;
+       struct vector limit[2];
+       struct camera *cam;
+       SDL_Surface *screen;
+};
+
 #define list_to_planet(list_head) container_of((list_head), struct planet, list)
+
 #define tree_to_planet(qt) container_of((qt), struct planet, tree)
 
+#define qt_itr_to_planet_itr(qt)                                       \
+       container_of((qt), struct planet_search_iterator, qt_iterator)
+
 void init_planet(struct planet *p);
 void create_planets(struct planet *p, int num, double total_mass,
                    double radius);
@@ -32,5 +43,8 @@ struct planet *move_planet(struct planet *p, const double time);
 void print_planet(const struct planet *p);
 
 int planet_spatial_compare(struct quadtree *a, struct quadtree *b);
+int planet_search_rectangular(struct quadtree *node,
+                             struct quadtree_iterator *itr);
+void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it);
 
 #endif
index 327ee2092b44a2646c39b1b2189a28b1e1ea4768..987dbb686e61927ca59deecc8765de1fe34a1d98 100644 (file)
@@ -212,7 +212,7 @@ static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
 {
        int direction, count = 0;
 
-       direction = it->direction(head, it->ptr);
+       direction = it->direction(head, it);
 
        if ((direction & QUADTREE_UPLEFT) && head->child[0])
                count += _walk_tree(head->child[0], it);
index a97619b20d0ab8e6ecb33f1c78dc9099b8e28477..23b79cebd7595d51875618fadedc42129fd0d5b8 100644 (file)
@@ -25,8 +25,8 @@ struct quadtree_iterator {
        struct quadtree *head;
        void *ptr;
 
-       int (*direction)(struct quadtree *head, void *ptr);
-       void (*callback)(struct quadtree *head, void *ptr);
+       int (*direction)(struct quadtree *head, struct quadtree_iterator *it);
+       void (*callback)(struct quadtree *head, struct quadtree_iterator *it);
 };
 
 struct quadtree *quadtree_add(struct quadtree *parent,