rocks which have been blasted do not collide.
char topline[1024];
char *initerror = "";
char topline[1024];
char *initerror = "";
-struct ship ship = { SHIP, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
+struct ship ship = { SHIP, ALL_FLAGS, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
float screendx = SCREENDXMIN, screendy = 0.0;
float xscroll, yscroll;
float screendx = SCREENDXMIN, screendy = 0.0;
float xscroll, yscroll;
do_collision(Sprite *a, Sprite *b)
{
if(a->type == SHIP) {
do_collision(Sprite *a, Sprite *b)
{
if(a->type == SHIP) {
- a->type = -SHIP; bang = true;
+ a->flags = MOVE_FLAG; bang = true;
} else if (b->type == SHIP) {
} else if (b->type == SHIP) {
- b->type = -SHIP; bang = true;
+ b->flags = MOVE_FLAG; bang = true;
switch(state) {
case DEAD_PAUSE:
// Create a new ship and start all over again
switch(state) {
case DEAD_PAUSE:
// Create a new ship and start all over again
- ship.sprite_type = SHIP;
+ ship.flags = ALL_FLAGS;
state = GAMEPLAY;
play_tune(TUNE_GAMEPLAY);
break;
state = GAMEPLAY;
play_tune(TUNE_GAMEPLAY);
break;
ship.x = XSIZE/2.2; ship.y = YSIZE/2;
ship.dx = screendx; ship.dy = screendy;
ship.lives = 4;
ship.x = XSIZE/2.2; ship.y = YSIZE/2;
ship.dx = screendx; ship.dy = screendy;
ship.lives = 4;
+ ship.flags = ALL_FLAGS;
add_sprite(SPRITE(&ship));
score = 0;
add_sprite(SPRITE(&ship));
score = 0;
snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
load_sprite(SPRITE(&prototypes[i]), a);
prototypes[i].sprite_type = ROCK;
snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
load_sprite(SPRITE(&prototypes[i]), a);
prototypes[i].sprite_type = ROCK;
+ prototypes[i].flags = ALL_FLAGS;
}
memset(rocks, 0, MAXROCKS*sizeof(struct rock));
}
memset(rocks, 0, MAXROCKS*sizeof(struct rock));
float dx, dy, n;
for(i=0; i<MAXROCKS; i++) {
float dx, dy, n;
for(i=0; i<MAXROCKS; i++) {
- if(rocks[i].sprite_type == NONE) continue;
+ if(!rocks[i].flags) continue;
r = SPRITE(&rocks[i]);
if(r->x <= 0) continue;
r = SPRITE(&rocks[i]);
if(r->x <= 0) continue;
n *= 15;
r->dx += 54.0*dx/n;
r->dy += 54.0*dy/n;
n *= 15;
r->dx += 54.0*dx/n;
r->dy += 54.0*dy/n;
+ r->flags &= ~COLLIDE_FLAG;
void
add_sprite(Sprite *s)
{
void
add_sprite(Sprite *s)
{
- if(s->type < 0) s->type = -s->type;
insert_sprite(square(s->x, s->y, set), s);
}
insert_sprite(square(s->x, s->y, set), s);
}
for(i=0; i<gw*gh; i++)
while(sprites[set][i]) {
Sprite *s = remove_sprite(&sprites[set][i]);
for(i=0; i<gw*gh; i++)
while(sprites[set][i]) {
Sprite *s = remove_sprite(&sprites[set][i]);
- if(s->type < 0) s->type = -s->type;
insert_sprite(&free_sprites[s->type], s);
insert_sprite(&free_sprites[s->type], s);
- if(s->type > 0) s->type = -s->type;
}
}
void
move_sprite(Sprite *s)
{
}
}
void
move_sprite(Sprite *s)
{
- // move it.
- s->x += (s->dx - screendx)*t_frame;
- s->y += (s->dy - screendy)*t_frame;
+ if(s->flags & DRAW_FLAG) {
+ // move it.
+ s->x += (s->dx - screendx)*t_frame;
+ s->y += (s->dy - screendy)*t_frame;
+ }
// clip it, or sort it into the other set of sprites.
if(s->x + s->w < 0 || s->x >= XSIZE
|| s->y + s->h < 0 || s->y >= YSIZE) {
// clip it, or sort it into the other set of sprites.
if(s->x + s->w < 0 || s->x >= XSIZE
|| s->y + s->h < 0 || s->y >= YSIZE) {
- if(s->type < 0) s->type = -s->type;
insert_sprite(&free_sprites[s->type], s);
insert_sprite(&free_sprites[s->type], s);
- if(s->type > 0) s->type = -s->type;
} else insert_sprite(square(s->x, s->y, 1-set), s);
}
} else insert_sprite(square(s->x, s->y, 1-set), s);
}
- if(a->type < 0 || b->type < 0) return false;
+ if(!COLLIDES(a) || !COLLIDES(b)) return false;
if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
pixel_hit_in_square(Sprite *r, float x, float y)
{
for(; r; r=r->next) {
pixel_hit_in_square(Sprite *r, float x, float y)
{
for(; r; r=r->next) {
- if(r->type >= 0 && pixel_collide(r, x, y)) return 1;
+ if(COLLIDES(r) && pixel_collide(r, x, y)) return 1;
struct sprite {
int8_t type;
struct sprite {
int8_t type;
Sprite *next;
float x, y;
float dx, dy;
Sprite *next;
float x, y;
float dx, dy;
+#define MOVE_FLAG 1
+#define DRAW_FLAG 2
+#define COLLIDE_FLAG 4
+#define ALL_FLAGS (~0)
+
+#define COLLIDES(sprite) ((sprite)->flags & COLLIDE_FLAG)
+
Sprite *free_sprites[N_TYPES]; // lists of free sprites, by type.
void do_collision(Sprite *a, Sprite *b);
Sprite *free_sprites[N_TYPES]; // lists of free sprites, by type.
void do_collision(Sprite *a, Sprite *b);
struct ship {
// core sprite fields
int8_t sprite_type;
struct ship {
// core sprite fields
int8_t sprite_type;
struct ship *next;
float x, y;
float dx, dy;
struct ship *next;
float x, y;
float dx, dy;
struct rock {
// core sprite fields
int8_t sprite_type;
struct rock {
// core sprite fields
int8_t sprite_type;
struct rock *next;
float x, y;
float dx, dy;
struct rock *next;
float x, y;
float dx, dy;
draw_sprite(Sprite *s)
{
SDL_Rect dest;
draw_sprite(Sprite *s)
{
SDL_Rect dest;
- if(s->type < 0) return;
- dest.x = s->x; dest.y = s->y;
- SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
+ if(s->flags & DRAW_FLAG) {
+ dest.x = s->x; dest.y = s->y;
+ SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
+ }