crystal-tower/src/stars.c

73 lines
1.2 KiB
C

#include "stars.h"
#include "conf.h"
#include "raygint/display.h"
#include <stdlib.h>
struct Star {
int x;
float y;
float spd;
int stretch;
};
static struct Star stars[STARS_COUNT];
static void star_rand(struct Star *);
static void star_update(struct Star *);
static void star_draw(struct Star *);
void
stars_init(void)
{
int i = STARS_COUNT;
while (i-- > 0) {
stars[i].y = (float)(rand() % DHEIGHT);
star_rand(&stars[i]);
}
}
void
stars_update(void)
{
int i = STARS_COUNT;
while (i-- > 0)
star_update(&stars[i]);
}
void
stars_draw(void)
{
int i = STARS_COUNT;
while (i-- > 0)
star_draw(&stars[i]);
}
static void
star_rand(struct Star *s)
{
s->x = STARS_MIN_X + rand() % (STARS_MAX_X - STARS_MIN_X);
s->spd = STARS_BASE_SPEED +
(float)(rand() & 0xff) / 0xff * STARS_SPEED_VARIATION;
s->stretch = s->spd * STARS_STRETCH * STARS_SIZE - 1.0f;
}
static void
star_update(struct Star *s)
{
s->y -= s->spd;
if (s->y < -STARS_SIZE - STARS_MAX_SPEED * STARS_STRETCH) {
s->y = DHEIGHT - STARS_SIZE;
star_rand(s);
}
}
static void
star_draw(struct Star *s)
{
const int x = s->x;
const int y = s->y;
const int x2 = x + STARS_SIZE - 1;
const int y2 = y + s->stretch;
drect(x, y, x2, y2, STARS_COLOR);
}