JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
sprites now have a flag field (MOVE/DRAW/COLLIDE).
authorJoshua Grams <josh@qualdan.com>
Fri, 24 Mar 2006 00:46:58 +0000 (00:46 +0000)
committerJoshua Grams <josh@qualdan.com>
Fri, 24 Mar 2006 00:46:58 +0000 (00:46 +0000)
rocks which have been blasted do not collide.

main.c
rocks.c
sprite.c
sprite.h

diff --git a/main.c b/main.c
index a068033..96c3968 100644 (file)
--- a/main.c
+++ b/main.c
@@ -66,7 +66,7 @@ struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
 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;
@@ -531,9 +531,9 @@ void
 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;
        } else {
                bounce(a, b);
        }
        } else {
                bounce(a, b);
        }
@@ -553,7 +553,7 @@ gameloop() {
                                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;
@@ -668,6 +668,7 @@ gameloop() {
                                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;
diff --git a/rocks.c b/rocks.c
index 2ab0fa1..7285d3b 100644 (file)
--- 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;
                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));
@@ -206,7 +207,7 @@ blast_rocks(float x, float y, float radius)
        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;
 
@@ -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;
                        n *= 15;
                        r->dx += 54.0*dx/n;
                        r->dy += 54.0*dy/n;
+                       r->flags &= ~COLLIDE_FLAG;
                }
        }
 }
                }
        }
 }
index 1609368..db5ac8d 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -111,7 +111,6 @@ square(int x, int y, int set)
 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);
 }
 
@@ -123,18 +122,19 @@ reset_sprites(void)
        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;
+                       s->flags = 0;
                }
 }
 
 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;
+       }
 }
 
 void
 }
 
 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) {
        // 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;
+               s->flags = 0;
        } else insert_sprite(square(s->x, s->y, 1-set), s);
 }
 
        } 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;
 
 {
        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; }
 
 
        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) {
 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;
 }
        }
        return 0;
 }
index f9070b4..2e7ad3b 100644 (file)
--- a/sprite.h
+++ b/sprite.h
@@ -15,6 +15,7 @@ typedef struct sprite Sprite;
 
 struct sprite {
        int8_t type;
 
 struct sprite {
        int8_t type;
+       int8_t flags;
        Sprite *next;
        float x, y;
        float dx, dy;
        Sprite *next;
        float x, y;
        float dx, dy;
@@ -25,6 +26,13 @@ struct sprite {
        uint32_t area;
 };
 
        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);
 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;
 struct ship {
        // core sprite fields
        int8_t sprite_type;
+       int8_t flags;
        struct ship *next;
        float x, y;
        float dx, dy;
        struct ship *next;
        float x, y;
        float dx, dy;
@@ -64,6 +73,7 @@ struct ship {
 struct rock {
        // core sprite fields
        int8_t sprite_type;
 struct rock {
        // core sprite fields
        int8_t sprite_type;
+       int8_t flags;
        struct rock *next;
        float x, y;
        float dx, dy;
        struct rock *next;
        float x, y;
        float dx, dy;
@@ -99,9 +109,10 @@ static inline void
 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);
+       }
 }
 
 #endif // VOR_SPRITE_H
 }
 
 #endif // VOR_SPRITE_H