]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Draw lines
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 12 Apr 2010 18:16:20 +0000 (21:16 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Mon, 12 Apr 2010 18:16:20 +0000 (21:16 +0300)
planet.c
planet.h

index 33f7cdecf439c3836afcd51ec300b63302b200c4..02249fe1113857f808b34d929c782569572b0b2a 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -19,6 +19,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);
@@ -95,7 +124,7 @@ void create_planets(struct planet *p, int num, double total_mass, double radius)
        }
 }
 
-void draw_planet(SDL_Surface *screen, struct planet *p,
+void draw_planet(SDL_Surface *screen, const struct planet *p,
                 const struct camera *cam)
 {
        struct vector pos;
@@ -110,6 +139,16 @@ void draw_planet(SDL_Surface *screen, struct planet *p,
 
        y = MAX(pos.y - radius, 0);
 
+       for (x = 0; x < 4; x++) {
+               if (!p->tree.child[x])
+                       continue;
+
+               struct planet *q = tree_to_planet(p->tree.child[x]);
+
+               draw_line(screen, &p->pos, &q->pos,
+                         p->r, p->g, p->b, cam);
+       }
+
        if (radius * 2 <= 1) {
                if (pos.x >= 0 && pos.x < screen->w &&
                    pos.y >= 0 && pos.y < screen->h)
@@ -131,9 +170,9 @@ void draw_planet(SDL_Surface *screen, struct planet *p,
 
                offset = y * screen->pitch + x_start * 4;
                for (x = x_start; x < x_end; x++) {
-                       buf[offset++] = p->b;
-                       buf[offset++] = p->g;
-                       buf[offset++] = p->r;
+                       buf[offset++] += p->b;
+                       buf[offset++] += p->g;
+                       buf[offset++] += p->r;
                        offset++;
                }
        }
@@ -396,6 +435,8 @@ struct planet *prune_planet_tree(struct planet *p)
                return tree_to_planet(tree_parent);
        }
 
+       planet_ops.recalculate_stats(&p->tree);
+
        return tree_to_planet(quadtree_find_parent(&p->tree));
 }
 
index bc18fcfbd0072cfee9288e417ac1f2878c86a3b3..b493b3a6871a0bdf6885eb2d928dce9c78abd9db 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -43,7 +43,8 @@ extern int gravitations, optimizations;
 void init_planet(struct planet *p);
 void create_planets(struct planet *p, int num, double total_mass,
                    double radius);
-void draw_planet(SDL_Surface *screen, struct planet *p, const struct camera *);
+void draw_planet(SDL_Surface *screen, const struct planet *p,
+                const struct camera *);
 void gravitize_planet_tree(struct planet *p, double time);
 struct planet *merge_planets(struct planet *a, struct planet *b);
 struct planet *move_planet(struct planet *p, const double time);