From 0978da8f14d855eecae9882b559d6e8b0533e78e Mon Sep 17 00:00:00 2001 From: Joshua Grams Date: Fri, 24 Mar 2006 00:46:58 +0000 Subject: [PATCH] sprites now have a flag field (MOVE/DRAW/COLLIDE). rocks which have been blasted do not collide. --- main.c | 9 +++++---- rocks.c | 4 +++- sprite.c | 19 +++++++++---------- sprite.h | 17 ++++++++++++++--- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/main.c b/main.c index a068033..96c3968 100644 --- a/main.c +++ b/main.c @@ -66,7 +66,7 @@ struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot; 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; @@ -531,9 +531,9 @@ void 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) { - b->type = -SHIP; bang = true; + b->flags = MOVE_FLAG; bang = true; } else { bounce(a, b); } @@ -553,7 +553,7 @@ gameloop() { 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; @@ -668,6 +668,7 @@ gameloop() { 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; diff --git a/rocks.c b/rocks.c index 2ab0fa1..7285d3b 100644 --- a/rocks.c +++ b/rocks.c @@ -46,6 +46,7 @@ load_rocks(void) 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)); @@ -206,7 +207,7 @@ blast_rocks(float x, float y, float radius) float dx, dy, n; for(i=0; ix <= 0) continue; @@ -218,6 +219,7 @@ blast_rocks(float x, float y, float radius) n *= 15; r->dx += 54.0*dx/n; r->dy += 54.0*dy/n; + r->flags &= ~COLLIDE_FLAG; } } } diff --git a/sprite.c b/sprite.c index 1609368..db5ac8d 100644 --- a/sprite.c +++ b/sprite.c @@ -111,7 +111,6 @@ square(int x, int y, int set) void add_sprite(Sprite *s) { - if(s->type < 0) s->type = -s->type; insert_sprite(square(s->x, s->y, set), s); } @@ -123,18 +122,19 @@ reset_sprites(void) for(i=0; itype < 0) s->type = -s->type; insert_sprite(&free_sprites[s->type], s); - if(s->type > 0) s->type = -s->type; + s->flags = 0; } } 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; + } } void @@ -143,9 +143,8 @@ sort_sprite(Sprite *s) // 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); - if(s->type > 0) s->type = -s->type; + s->flags = 0; } else insert_sprite(square(s->x, s->y, 1-set), s); } @@ -214,7 +213,7 @@ collide(Sprite *a, Sprite *b) { int dx, dy, xov, yov; - 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; } @@ -306,7 +305,7 @@ int 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; } return 0; } diff --git a/sprite.h b/sprite.h index f9070b4..2e7ad3b 100644 --- a/sprite.h +++ b/sprite.h @@ -15,6 +15,7 @@ typedef struct sprite Sprite; struct sprite { int8_t type; + int8_t flags; Sprite *next; float x, y; float dx, dy; @@ -25,6 +26,13 @@ struct sprite { uint32_t area; }; +#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); @@ -48,6 +56,7 @@ void bounce(Sprite *a, Sprite *b); struct ship { // core sprite fields int8_t sprite_type; + int8_t flags; struct ship *next; float x, y; float dx, dy; @@ -64,6 +73,7 @@ struct ship { struct rock { // core sprite fields int8_t sprite_type; + int8_t flags; struct rock *next; float x, y; float dx, dy; @@ -99,9 +109,10 @@ static inline void 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); + } } #endif // VOR_SPRITE_H -- 1.7.10.4