]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Draw rectangular planets
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 6 Mar 2010 18:41:49 +0000 (20:41 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 6 Mar 2010 18:42:49 +0000 (20:42 +0200)
These look bit silly now, but it's easier to draw rectangular planets
instead of round ones..

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

index 62e8a1da3cbebb36fa2ffd54ae76bb0075d2e1e1..4feb500fbfe8450b9bf8c2420251c95bfe1919f5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 SDL_CONFIG=$(shell sdl-config --cflags)
 SDL_LIBS=$(shell sdl-config --libs)
-CFLAGS=$(SDL_CONFIG) -Wall -O2 -g
+CFLAGS=$(SDL_CONFIG) -Wall -O0 -g
 
 LIBS=$(SDL_LIBS) -lm
 
index e69337db39f18b8f54c1499135a604b497b3a525..8e68ecaff3afb9e7ab393296e739e88446fc8824 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -1,5 +1,6 @@
 #include "random.h"
 #include "planet.h"
+#include "utils.h"
 
 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
                     const unsigned char r, const unsigned char g,
@@ -20,6 +21,7 @@ void init_planet(const SDL_Surface *screen, struct planet *p)
        p->pos.x = get_random() % screen->w;
        p->pos.y = get_random() % screen->h;
        p->mass = get_random() % 1000 + 100;
+       p->size = p->mass / 100;
        p->r = get_random() % 256;
        p->g = get_random() % 256;
        p->b = get_random() % 256;
@@ -27,11 +29,18 @@ void init_planet(const SDL_Surface *screen, struct planet *p)
 
 void draw_planet(SDL_Surface *screen, struct planet *p)
 {
-       if (p->pos.x < screen->w &&
-           p->pos.y < screen->h &&
-           p->pos.x >= 0 &&
-           p->pos.y >= 0)
-               putpixel(screen, p->pos.x, p->pos.y, p->r, p->g, p->b);
+       int size = p->size / 2;
+       int x, x_start, y, x_end, y_end;
+
+       x_start = MAX(p->pos.x - size, 0);
+       y = MAX(p->pos.y - size, 0);
+
+       x_end = MIN(p->pos.x + size, screen->w);
+       y_end = MIN(p->pos.y + size, screen->h);
+
+       for (; y < y_end; y++)
+               for (x = x_start; x < x_end; x++)
+                       putpixel(screen, x, y, p->r, p->g, p->b);
 }
 
 void gravitize_planets(struct planet *a, struct planet *b, const double time)
index 1918ee4d424f5a150851cd0340f111063ddacab6..b6c9610a7818788ef68934645f8cd8ff1551796f 100644 (file)
--- a/planet.h
+++ b/planet.h
@@ -9,6 +9,7 @@ struct planet {
        struct vector speed;
        struct vector pos;
        double mass;
+       float size;
        unsigned char r, g, b;
 };