]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Introduce struct quadree_ops
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 11 Apr 2010 15:05:13 +0000 (18:05 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 13 Apr 2010 19:00:14 +0000 (22:00 +0300)
Store all related function pointers (so far only the comparison) in
one structure. This will make it easier to add function pointers that
are needed in various quadtree operations.

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

index de0e49fa2d2bdb3138ed46e9aec54e15fc85232d..6a3e59464f2b8cfcc2ddc74146cdb5da34d8c21b 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -4,6 +4,10 @@
 #include "planet.h"
 #include "utils.h"
 
+struct quadtree_ops planet_ops = {
+       .compare = planet_spatial_compare,
+};
+
 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
                     const unsigned char r, const unsigned char g,
                     const unsigned char b)
@@ -90,8 +94,7 @@ void create_planets(struct planet *p, int num, double total_mass, double radius)
                             total_mass,
                             radius);
 
-               quadtree_add(&p->tree, &new_planet->tree,
-                            planet_spatial_compare);
+               quadtree_add(&p->tree, &new_planet->tree, &planet_ops);
 
                sum += new_planet->mass;
        }
@@ -201,7 +204,7 @@ struct planet *merge_planets(struct planet *a, struct planet *b)
        _merge_planets(a, b);
 
        list_del(&b->list);
-       quadtree_del(&b->tree, planet_spatial_compare);
+       quadtree_del(&b->tree, &planet_ops);
 
        free(b);
        return a;
@@ -244,8 +247,8 @@ void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
 {
        struct quadtree *parent;
 
-       parent = quadtree_del(node, planet_spatial_compare);
-       quadtree_add(parent, node, planet_spatial_compare);
+       parent = quadtree_del(node, &planet_ops);
+       quadtree_add(parent, node, &planet_ops);
 }
 
 struct planet *move_planet(struct planet *p, const double time)
@@ -278,9 +281,9 @@ struct planet *move_planet(struct planet *p, const double time)
                        continue;
                }
 
-               tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
+               tree_parent = quadtree_del(&p->tree, &planet_ops);
                p->pos = new_pos;
-               quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
+               quadtree_add(tree_parent, &p->tree, &planet_ops);
                return tree_to_planet(tree_parent);
        }
 
index f188d8069ee38a5426403304c97e5e485eb9186a..ec683aaa0e8d4e872215e337f3348f5896a21e70 100644 (file)
@@ -109,10 +109,8 @@ static void validate_tree(const struct quadtree *node)
  * account at all.
  */
 
-struct quadtree *quadtree_add(struct quadtree *parent,
-                             struct quadtree *new,
-                             int (*compare)(struct quadtree *a,
-                                            struct quadtree *b))
+struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
+                             struct quadtree_ops *ops)
 {
        int ret;
        if (parent == new)
@@ -120,7 +118,7 @@ struct quadtree *quadtree_add(struct quadtree *parent,
 
        validate_tree(parent);
 
-       ret = compare(parent, new);
+       ret = ops->compare(parent, new);
 
        if (ret < 0 || ret >= 4) {
                printf("Invalid comparison result of %d\n", ret);
@@ -128,7 +126,7 @@ struct quadtree *quadtree_add(struct quadtree *parent,
        }
 
        if (parent->child[ret])
-               return quadtree_add(parent->child[ret], new, compare);
+               return quadtree_add(parent->child[ret], new, ops);
 
        parent->child[ret] = new;
        new->parent = parent;
@@ -148,8 +146,7 @@ struct quadtree *quadtree_add(struct quadtree *parent,
 static void
 _quadtree_reposition_reqursively(struct quadtree *root,
                                 struct quadtree *node,
-                                int (*compare)(struct quadtree *a,
-                                               struct quadtree *b))
+                                struct quadtree_ops *ops)
 {
        int i;
 
@@ -165,7 +162,7 @@ _quadtree_reposition_reqursively(struct quadtree *root,
                    node->child[i] == node->parent)
                        trap();
 
-               _quadtree_reposition_reqursively(root, node->child[i], compare);
+               _quadtree_reposition_reqursively(root, node->child[i], ops);
                node->child[i] = 0;
        }
 
@@ -174,7 +171,7 @@ _quadtree_reposition_reqursively(struct quadtree *root,
        node->children = 0;
 
        /* Then remove this node under the new root. */
-       quadtree_add(root, node, compare);
+       quadtree_add(root, node, ops);
 }
 
 /*
@@ -185,8 +182,7 @@ _quadtree_reposition_reqursively(struct quadtree *root,
  * value will be the original root of the tree.
  */
 struct quadtree *quadtree_del(struct quadtree *node,
-                             int (*compare)(struct quadtree *a,
-                                            struct quadtree *b))
+                             struct quadtree_ops *ops)
 {
        struct quadtree *parent = 0;
        int i;
@@ -207,7 +203,7 @@ struct quadtree *quadtree_del(struct quadtree *node,
                        }
                        _quadtree_reposition_reqursively(parent,
                                                         node->child[i],
-                                                        compare);
+                                                        ops);
                        node->child[i] = 0;
                }
 
@@ -245,7 +241,7 @@ struct quadtree *quadtree_del(struct quadtree *node,
                        continue;
 
                _quadtree_reposition_reqursively(node->parent, node->child[i],
-                                                compare);
+                                                ops);
                node->child[i] = 0;
        }
 
index 3d8f51835ddad4a2c6c549b856ab12b1fad2a3c2..917abd501b34dec4e6fea636c51590ddfef44bc4 100644 (file)
@@ -14,6 +14,10 @@ struct quadtree {
        long int depth;         /* The deepest subtree branch */
 };
 
+struct quadtree_ops {
+       int (*compare)(struct quadtree *a, struct quadtree *b);
+};
+
 static inline void init_quadtree(struct quadtree *t)
 {
        memset(t, 0, sizeof(*t));
@@ -33,14 +37,10 @@ struct quadtree_iterator {
        void (*callback)(struct quadtree *head, struct quadtree_iterator *it);
 };
 
-struct quadtree *quadtree_add(struct quadtree *parent,
-                             struct quadtree *new,
-                             int (*compare)(struct quadtree *a,
-                                            struct quadtree *b));
+struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
+                             struct quadtree_ops *ops);
 
-struct quadtree *quadtree_del(struct quadtree *node,
-                             int (*compare)(struct quadtree *a,
-                                            struct quadtree *b));
+struct quadtree *quadtree_del(struct quadtree *node, struct quadtree_ops *ops);
 
 int walk_quadtree(const struct quadtree_iterator *iterator);