]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Separate circle drawing into its own function
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 13 Apr 2010 17:18:11 +0000 (20:18 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 13 Apr 2010 17:18:11 +0000 (20:18 +0300)
planet.c

index 0d2c6d57a2b43d21a13fb09c740a1348b08f58fa..e62bf00c5818273adfd8dd8d9628585a0ba556e9 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -124,58 +124,77 @@ void create_planets(struct planet *p, int num, double total_mass, double radius)
        }
 }
 
+void draw_circle(SDL_Surface *screen, const struct vector *pos,
+                const double radius,
+                char r, char g, char b, int transparent)
+{
+       int x, x_start, y, x_end;
+       float r2 = radius * radius;
+
+       y = MAX(pos->y - radius, 0);
+
+       if (radius * 2 <= 1) {
+               if (pos->x >= 0 && pos->x < screen->w &&
+                   pos->y >= 0 && pos->y < screen->h)
+                       putpixel(screen, (int)pos->x, (int)pos->y,
+                                r, g, b);
+               return;
+       }
+
+       for (; y < MIN(pos->y + radius, screen->h); y++) {
+               int offset;
+               unsigned char *buf = screen->pixels;
+               float y2 = (y - pos->y);
+
+               y2 = sqrt(r2 - y2 * y2);
+               x_start = pos->x - y2;
+               x_end = pos->x + y2;
+               x_start = MAX(0, x_start);
+               x_end = MIN(x_end, screen->w);
+
+               offset = y * screen->pitch + x_start * 4;
+               if (transparent) {
+                       for (x = x_start; x < x_end; x++) {
+                               buf[offset++] += b;
+                               buf[offset++] += g;
+                               buf[offset++] += r;
+                               offset++;
+                       }
+               } else {
+                       for (x = x_start; x < x_end; x++) {
+                               buf[offset++] = b;
+                               buf[offset++] = g;
+                               buf[offset++] = r;
+                               offset++;
+                       }
+               }
+       }
+}
+
 void draw_planet(SDL_Surface *screen, const struct planet *p,
                 const struct camera *cam)
 {
        struct vector pos;
        float radius = p->radius * cam->zoom;
-       float r2 = radius * radius;
-       int x, x_start, y, x_end;
+       int i;
 
        vector_sub(&p->pos, &cam->pos, &pos);
        vector_scale(&pos, cam->zoom, &pos);
        pos.x += screen->w / 2;
        pos.y += screen->h / 2;
 
-       y = MAX(pos.y - radius, 0);
 
-       for (x = 0; x < 4; x++) {
-               if (!p->tree.child[x])
+       for (i = 0; i < 4; i++) {
+               if (!p->tree.child[i])
                        continue;
 
-               struct planet *q = tree_to_planet(p->tree.child[x]);
+               struct planet *q = tree_to_planet(p->tree.child[i]);
 
                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)
-                       putpixel(screen, (int)pos.x, (int)pos.y,
-                                p->r, p->g, p->b);
-               return;
-       }
-
-       for (; y < MIN(pos.y + radius, screen->h); y++) {
-               int offset;
-               unsigned char *buf = screen->pixels;
-               float y2 = (y - pos.y);
-
-               y2 = sqrt(r2 - y2 * y2);
-               x_start = pos.x - y2;
-               x_end = pos.x + y2;
-               x_start = MAX(0, x_start);
-               x_end = MIN(x_end, screen->w);
-
-               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;
-                       offset++;
-               }
-       }
+       draw_circle(screen, &pos, radius, p->r, p->g, p->b, 0);
 }
 
 int gravitize_planet_with_planet(struct planet *a, struct planet *b,