SPARSE=sparse
CHECKPATCH=/usr/src/linux/scripts/checkpatch.pl
-PLANET_OBJS=main.o random.o vector.o planet.o camera.o
+PLANET_OBJS=main.o random.o vector.o planet.o camera.o quadtree.o
PLANET_DEBUG_OBJS= $(patsubst %.o,%-debug.o,$(PLANET_OBJS))
sdl-planet: $(PLANET_OBJS)
p->b = get_random() % 256;
INIT_LIST_HEAD(&p->list);
+ init_quadtree(&p->tree);
}
/**
for (i = 0; i < num; i++) {
new_planet = malloc(sizeof(*new_planet));
init_planet(new_planet);
+
list_add(&new_planet->list, &p->list);
+ quadtree_add(&new_planet->tree, &p->tree,
+ planet_spatial_compare);
+
setup_planet(new_planet,
total_mass / num * 2 * get_random_double(),
total_mass,
printf("pos: (%f,%f), speed: (%f,%f), mass: %f, radius %f\n",
p->pos.x, p->pos.y, p->speed.x, p->speed.y, p->mass, p->radius);
}
+
+int planet_spatial_compare(struct quadtree *ta, struct quadtree *tb)
+{
+ struct planet *a, *b;
+ int up, left;
+ a = tree_to_planet(ta);
+ b = tree_to_planet(tb);
+
+ up = b->pos.y < a->pos.y;
+ left = b->pos.x < a->pos.x;
+
+ if (up && left)
+ return 0;
+ if (up && !left)
+ return 1;
+ if (left)
+ return 2;
+ return 3;
+}
#include "list.h"
#include "utils.h"
#include "camera.h"
+#include "quadtree.h"
struct planet {
struct vector speed;
struct vector pos;
struct list_head list;
+ struct quadtree tree;
double mass;
float radius;
unsigned char r, g, b;
};
#define list_to_planet(list_head) container_of((list_head), struct planet, list)
+#define tree_to_planet(qt) container_of((qt), struct planet, tree)
void init_planet(struct planet *p);
void create_planets(struct planet *p, int num, double total_mass,
void 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);
+
#endif