removed references to stdint.h (using inttypes.h where necessary).
Makefile: update header dependencies.
$(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 $^
#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
#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>
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"));
}
if(state == GAMEPLAY) {
- bang = hit_rocks(ship.x, ship.y, ship.shape);
+ bang = hit_rocks(SPRITE(&ship));
}
ms_frame = SDL_GetTicks() - ms_end;
#include "mt.h"
#include "rocks.h"
-SDL_Surface *load_image(char *filename);
-
struct rock rocks[MAXROCKS], *free_rocks;
struct rock **rock_buckets[2];
}
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;
}
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);
#include <SDL.h>
#include <SDL_keysym.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VOR_SCORE_H
#include <SDL.h>
-#include <stdint.h>
+#include <inttypes.h>
#include <stdio.h>
#define N_SCORES 8
#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;
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);
}
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;
return ret;
}
-int
+static int
mask_collide(int xov, int yov, struct shape *r, struct shape *s)
{
int y, ry, sy;
}
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
#ifndef VOR_SHAPE_H
#define VOR_SHAPE_H
-#include <SDL/SDL.h>
+#include <SDL.h>
#include <inttypes.h>
+typedef union sprite Sprite;
+
// Shape stuff
};
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 {
#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