]> git.itanic.dy.fi Git - sdl-planets/commitdiff
planet.c: Add support for drawing the planet tree structure
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 18 Apr 2010 08:49:25 +0000 (11:49 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 18 Apr 2010 08:49:25 +0000 (11:49 +0300)
A line will be drawn from a planet parent to the child node. Local
variable is used to enable line drawing at compile time.

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

index 88577ee8541e28a043794debe8a1c91ef4e04167..330f82af7b91dae0099330f4e3dfc9110d7632d6 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -6,6 +6,8 @@
 
 struct quadtree_ops planet_ops;
 
+static int draw_lines = 0;
+
 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)
@@ -18,6 +20,35 @@ static void putpixel(struct SDL_Surface *screen, const int x, const int y,
        buf[offset]   = r;
 }
 
+static void draw_line(SDL_Surface *screen, const struct vector *p1,
+                     const struct vector *p2,
+                     const unsigned char r, const unsigned char g,
+                     const unsigned char b, const struct camera *cam)
+{
+       struct vector dir, v;
+       float len;
+       int i;
+
+       vector_sub(p2, p1, &dir);
+
+       len = vector_abs(&dir);
+       vector_scale(&dir, 1 / len, &dir);
+       len *= cam->zoom;
+
+       vector_sub(p1, &cam->pos, &v);
+       vector_scale(&v, cam->zoom, &v);
+
+       v.x += screen->w / 2;
+       v.y += screen->h / 2;
+
+       for (i = 0; i < len ; i++) {
+               if (v.x >= 0 && v.x < screen->w &&
+                   v.y >= 0 && v.y < screen->h)
+                       putpixel(screen, v.x, v.y, r, g, b);
+               vector_add(&v, &dir, &v);
+       }
+}
+
 static void reshape_planet(struct planet *p)
 {
        p->radius = pow(p->mass / 100, 1 / 3.0);
@@ -153,6 +184,18 @@ void draw_planet(SDL_Surface *screen, struct planet *p,
        pos.x += screen->w / 2;
        pos.y += screen->h / 2;
 
+       if (draw_lines) {
+               for (i = 0; i < 4; i++) {
+                       if (!p->tree.child[i])
+                               continue;
+
+                       struct planet *q = tree_to_planet(p->tree.child[i]);
+
+                       draw_line(screen, &p->pos, &q->pos,
+                                 p->r, p->g, p->b, cam);
+               }
+       }
+
        draw_circle(screen, &pos, radius, p->r, p->g, p->b, 0);
 }