#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,
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;
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)