]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Prepare for having planets in linked list
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 6 Mar 2010 20:48:25 +0000 (22:48 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 6 Mar 2010 20:48:25 +0000 (22:48 +0200)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
planet.c
planet.h
utils.h

index 8e68ecaff3afb9e7ab393296e739e88446fc8824..df16acb7310c7bc7ab9f1296bf07d3e312b561ee 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -25,6 +25,8 @@ void init_planet(const SDL_Surface *screen, struct planet *p)
        p->r = get_random() % 256;
        p->g = get_random() % 256;
        p->b = get_random() % 256;
+
+       LIST_HEAD_INIT(p->list);
 }
 
 void draw_planet(SDL_Surface *screen, struct planet *p)
index b6c9610a7818788ef68934645f8cd8ff1551796f..79f918a6656eac1605ee6cb91d5670771d5e8969 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -4,15 +4,20 @@
 #include <SDL.h>
 
 #include "vector.h"
+#include "list.h"
+#include "utils.h"
 
 struct planet {
        struct vector speed;
        struct vector pos;
+       struct list_head list;
        double mass;
        float size;
        unsigned char r, g, b;
 };
 
+#define list_to_planet(planet) container_of((planet), struct planet, list)
+
 void init_planet(const SDL_Surface *screen, struct planet *p);
 void draw_planet(SDL_Surface *screen, struct planet *p);
 void gravitize_planets(struct planet *a, struct planet *b, const double time);
diff --git a/utils.h b/utils.h
index b377038edd94a022114e0d84e40b9ed1c81a885a..bceaa27290a031ef0edbac49dd50392a436ae57f 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -4,4 +4,15 @@
 #define MAX(a,b) ((a) > (b) ? (a) : (b))
 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr:       the pointer to the member.
+ * @type:      the type of the container struct this is embedded in.
+ * @member:    the name of the member within the struct.
+ *
+ */
+#define container_of(ptr, type, member) ({                     \
+       const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
+       (type *)( (char *)__mptr - offsetof(type,member) );})
+
 #endif