JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Updated collide to take two sprites.
authorJoshua Grams <josh@qualdan.com>
Tue, 21 Mar 2006 17:33:25 +0000 (17:33 +0000)
committerJoshua Grams <josh@qualdan.com>
Tue, 21 Mar 2006 17:33:25 +0000 (17:33 +0000)
removed references to stdint.h (using inttypes.h where necessary).
Makefile: update header dependencies.

Makefile
dust.h
main.c
rocks.c
rocks.h
score.c
score.h
sprite.c
sprite.h

index b3d8333..77460c4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -63,7 +63,25 @@ program: vor
 
 $(my_objects): config.h
 
-main.o file.o: file.h
+SFont.o: SFont.h
+
+args.o: args.h
+
+dust.o: globals.h dust.h mt.h
+
+file.o: file.h common.h
+
+main.o: args.h common.h dust.h file.h globals.h mt.h rocks.h score.h sprite.h sound.h
+
+mt.o: mt.h
+
+rocks.o: rocks.h common.h file.h globals.h mt.h
+
+score.o: score.h common.h file.h
+
+sound.o: sound.h args.h common.h
+
+sprite.o: sprite.h common.h
 
 vor: $(objects)
        @echo linking $@ from $^
diff --git a/dust.h b/dust.h
index b8e0de1..984587b 100644 (file)
--- a/dust.h
+++ b/dust.h
@@ -1,8 +1,6 @@
 #ifndef VOR_DUST_H
 #define VOR_DUST_H
 
-#include <stdint.h>
-
 /*
  * Dust motes are harmless background items.  They are created when the
  * program is initialized, and are simply wrapped whenever they hit the
diff --git a/main.c b/main.c
index ae52bd7..d57aa3b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -21,8 +21,8 @@
 
 #include <argp.h>
 #include <math.h>
-#include <SDL/SDL.h>
-#include <SDL/SDL_image.h>
+#include <SDL.h>
+#include <SDL_image.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -368,13 +368,7 @@ init(void) {
        NULLERROR(surf_b_over = load_image("banners/over.png"));
 
        // Load the spaceship graphic.
-       ship.shape = malloc(sizeof(struct shape));
-       if(!ship.shape) {
-               fprintf(stderr, "can't allocate ship shape.\n");
-               exit(1);
-       }
-       NULLERROR(ship.image = load_image("sprites/ship.png"));
-       get_shape(ship.image, ship.shape);
+       load_sprite(SPRITE(&ship), "sprites/ship.png");
 
        // Load the life indicator (small ship) graphic.
        NULLERROR(surf_life = load_image("indicators/life.png"));
@@ -517,7 +511,7 @@ draw() {
        }
 
        if(state == GAMEPLAY) {
-               bang = hit_rocks(ship.x, ship.y, ship.shape);
+               bang = hit_rocks(SPRITE(&ship));
        }
 
        ms_frame = SDL_GetTicks() - ms_end;
diff --git a/rocks.c b/rocks.c
index 8cb7987..d01d794 100644 (file)
--- a/rocks.c
+++ b/rocks.c
@@ -10,8 +10,6 @@
 #include "mt.h"
 #include "rocks.h"
 
-SDL_Surface *load_image(char *filename);
-
 struct rock rocks[MAXROCKS], *free_rocks;
 
 struct rock **rock_buckets[2];
@@ -286,40 +284,41 @@ draw_rocks(void)
 }
 
 int
-hit_in_bucket(struct rock *r, float x, float y, struct shape *shape)
+hit_in_bucket(struct rock *r, Sprite *s)
 {
        for(; r; r=&r->next->rock) {
-               if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
+               if(collide(SPRITE(r), s)) return true;
        }
-       return 0;
+       return false;
 }
 
 int
-hit_rocks(float x, float y, struct shape *shape)
+hit_rocks(Sprite *s)
 {
-       int ix, iy;
+       struct base_sprite *sp = &s->sprite;
        int l, r, t, b;
        struct rock **bucket;
 
-       ix = x + grid_size; iy = y + grid_size;
-       l = ix / grid_size; r = (ix+shape->w)/grid_size;
-       t = iy / grid_size; b = (iy+shape->h)/grid_size;
+       l = (sp->x + grid_size) / grid_size;
+       r = (sp->x + sp->shape->w + grid_size) / grid_size;
+       t = (sp->y + grid_size) / grid_size;
+       b = (sp->y + sp->shape->h + grid_size) / grid_size;
        bucket = &rock_buckets[p][l + t*bw];
 
-       if(hit_in_bucket(*bucket, x, y, shape)) return true;
-       if(l > 0 && hit_in_bucket(*(bucket-1), x, y, shape)) return true;
-       if(t > 0 && hit_in_bucket(*(bucket-bw), x, y, shape)) return true;
-       if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), x, y, shape)) return true;
+       if(hit_in_bucket(*bucket, s)) return true;
+       if(l > 0 && hit_in_bucket(*(bucket-1), s)) return true;
+       if(t > 0 && hit_in_bucket(*(bucket-bw), s)) return true;
+       if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), s)) return true;
 
        if(r > l) {
-               if(hit_in_bucket(*(bucket+1), x, y, shape)) return true;
-               if(t > 0 && hit_in_bucket(*(bucket+1-bw), x, y, shape)) return true;
+               if(hit_in_bucket(*(bucket+1), s)) return true;
+               if(t > 0 && hit_in_bucket(*(bucket+1-bw), s)) return true;
        }
        if(b > t) {
-               if(hit_in_bucket(*(bucket+bw), x, y, shape)) return true;
-               if(l > 0 && hit_in_bucket(*(bucket-1+bw), x, y, shape)) return true;
+               if(hit_in_bucket(*(bucket+bw), s)) return true;
+               if(l > 0 && hit_in_bucket(*(bucket-1+bw), s)) return true;
        }
-       if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), x, y, shape)) return true;
+       if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), s)) return true;
        return false;
 }
 
diff --git a/rocks.h b/rocks.h
index 40f26bd..ab5df75 100644 (file)
--- a/rocks.h
+++ b/rocks.h
@@ -7,7 +7,7 @@ void new_rocks(void);
 void move_rocks(void);
 void draw_rocks(void);
 
-int hit_rocks(float x, float y, struct shape *shape);
+int hit_rocks(Sprite *s);
 int pixel_hit_rocks(float x, float y);
 
 void blast_rocks(float x, float y, float radius, int onlyslow);
diff --git a/score.c b/score.c
index 31025a9..5738e72 100644 (file)
--- a/score.c
+++ b/score.c
@@ -18,7 +18,6 @@
 
 #include <SDL.h>
 #include <SDL_keysym.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/score.h b/score.h
index 478afca..0778033 100644 (file)
--- a/score.h
+++ b/score.h
@@ -20,7 +20,7 @@
 #define VOR_SCORE_H
 
 #include <SDL.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdio.h>
 
 #define N_SCORES 8
index ca3e937..9a9d953 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -3,6 +3,22 @@
 #include "sprite.h"
 
 void
+load_sprite(Sprite *sprite, char *filename)
+{
+       struct base_sprite *spr = &sprite->sprite;
+       spr->image = load_image(filename);
+       if(!spr->image) return;
+       if(!spr->shape) {
+               spr->shape = malloc(sizeof(struct shape));
+               if(!spr->shape) {
+                       fprintf(stderr, "load_sprite(): can't allocate shape structure.\n");
+                       exit(1);
+               }
+               get_shape(spr->image, spr->shape);
+       }
+}
+
+void
 get_shape(SDL_Surface *img, struct shape *s)
 {
        int x, y;
@@ -19,7 +35,7 @@ get_shape(SDL_Surface *img, struct shape *s)
        s->mw = ((img->w+31)>>5);
        s->mask = malloc(4*s->mw*s->h);
        if(!s->mask) {
-               fprintf(stderr, "can't malloc bitmask");
+               fprintf(stderr, "get_shape(): can't allocate bitmask.\n");
                exit(1);
        }
 
@@ -40,7 +56,7 @@ get_shape(SDL_Surface *img, struct shape *s)
        SDL_UnlockSurface(img);
 }
 
-int
+static int
 line_collide(int xov, struct shape *r, uint32_t *rbits, struct shape *s, uint32_t *sbits)
 {
        int lshift, n, i, ret = 0;
@@ -70,7 +86,7 @@ line_collide(int xov, struct shape *r, uint32_t *rbits, struct shape *s, uint32_
        return ret;
 }
 
-int
+static int
 mask_collide(int xov, int yov, struct shape *r, struct shape *s)
 {
        int y, ry, sy;
@@ -95,18 +111,22 @@ mask_collide(int xov, int yov, struct shape *r, struct shape *s)
 }
 
 int
-collide(int xdiff, int ydiff, struct shape *r, struct shape *s)
+collide(Sprite *r, Sprite *s)
 {
+       struct shape *rs = r->sprite.shape;
+       struct shape *ss = s->sprite.shape;
+       int dx = s->sprite.x - r->sprite.x;
+       int dy = s->sprite.y - r->sprite.y;
        int xov, yov;
 
-       if(xdiff >= 0) xov = max(min(r->w-xdiff, s->w), 0);
-       else xov = min(-min(s->w+xdiff, r->w), 0);
+       if(dx >= 0) xov = max(min(rs->w - dx, ss->w), 0);
+       else xov = -max(min(ss->w + dx, rs->w), 0);
 
-       if(ydiff >= 0) yov = max(min(r->h-ydiff, s->h), 0);
-       else yov = min(-min(s->h+ydiff, r->h), 0);
+       if(dy >= 0) yov = max(min(rs->h - dy, ss->h), 0);
+       else yov = -max(min(ss->h + dy, rs->h), 0);
 
-       if(xov == 0 || yov == 0) return 0;  // bboxes hit?
-       else return mask_collide(xov, yov, r, s);
+       if(xov == 0 || yov == 0) return false;
+       else return mask_collide(xov, yov, rs, ss);
 }
 
 int
index c2d0a8e..939de08 100644 (file)
--- a/sprite.h
+++ b/sprite.h
@@ -1,9 +1,11 @@
 #ifndef VOR_SHAPE_H
 #define VOR_SHAPE_H
 
-#include <SDL/SDL.h>
+#include <SDL.h>
 #include <inttypes.h>
 
+typedef union sprite Sprite;
+
 
 // Shape stuff
 
@@ -15,15 +17,13 @@ struct shape {
 };
 
 void get_shape(SDL_Surface *img, struct shape *s);
-int collide(int xdiff, int ydiff, struct shape *r, struct shape *s);
-int pixel_collide(unsigned int xoff, unsigned int yoff, struct shape *r);
+int collide(Sprite *r, Sprite *s);
+int pixel_collide(unsigned int xdiff, unsigned int ydiff, struct shape *r);
 
 
 
 // Sprite stuff
 
-typedef union sprite Sprite;
-
 #define SPRITE(x) ((Sprite *) (x))
 
 struct base_sprite {
@@ -71,4 +71,7 @@ union sprite {
 #define SHIP_SPRITE 1
 #define ROCK_SPRITE 2
 
+SDL_Surface *load_image(char *filename);
+void load_sprite(Sprite *sprite, char *filename);
+
 #endif // VOR_SHAPE_H