shape.c: generates bitmasks for graphics, no collision detection yet though.
rocks += 30 31 32 33 34 35 36 37 38 39
rocks += 40 41 42 43 44 45 46 47 48 49
-graphics := data/sprites/ship.png $(rocks:%=data/sprites/rock%.png)
+graphics := data/sprites/ship.png data/indicators/life.png $(rocks:%=data/sprites/rock%.png)
INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -o games -g games
pnmtopng -transparent =white data/sprites/ship.pnm >$@
rm ship.png ship.pnm data/sprites/ship.pnm
+data/indicators/life.png: ship.pov gfx.mk
+ povray -D +A +UA +W17 +H17 $< -O$@
+
data/sprites/rock%.png: rocks.pov gfx.mk
povray -Irocks.pov -D +H52 +W52 +K`echo "$@" | grep -o '[0-9][0-9]'` +Fp -O$@.pnm 2>/dev/null
pnmcrop < $@.pnm > $@-c.pnm
#include "config.h"
#include "file.h"
#include "score.h"
+#include "shape.h"
#include "sound.h"
#include <math.h>
int dead; // has been blown out of the way
// to make room for a new ship appearing.
SDL_Surface *image;
+ struct shape *shape;
int type_number;
};
struct black_point_struct {
*surf_rock[NROCKS], // THE ROCKS
*surf_font_big; // The big font
+struct shape rock_shapes[NROCKS];
+
SFont_Font *g_font;
// Structure global variables
SDL_UnlockSurface(s);
}
+ char a[MAX_PATH_LEN];
int
init(int fullscreen) {
// Load all our lovely rocks
for(i = 0; i<NROCKS; i++) {
- char a[MAX_PATH_LEN];
snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
NULLERROR(temp = IMG_Load(a));
NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
+ get_shape(surf_rock[i], &rock_shapes[i]);
}
// Remove the mouse cursor
--- /dev/null
+#include <stdlib.h>
+#include "shape.h"
+
+void
+get_shape(SDL_Surface *img, struct shape *s)
+{
+ int x, y;
+ uint16_t *px, transp;
+ uint32_t bits;
+
+ if(img->format->BytesPerPixel != 2) {
+ fprintf(stderr, "get_shape(): not a 16-bit image!\n");
+ exit(1);
+ }
+
+ s->w = img->w; s->h = img->h;
+ s->mw = ((img->w+31)>>5) * img->h;
+ s->mask = malloc(4*s->mw);
+ if(!s->mask) {
+ fprintf(stderr, "can't malloc bitmask");
+ exit(1);
+ }
+
+ SDL_LockSurface(img);
+ px = img->pixels;
+ transp = img->format->colorkey;
+ bits = 0;
+ for(y=0; y<img->h; y++) {
+ for(x=0; x<img->w; x++) {
+ if(*px++ != transp) bits |= 1;
+ if(x == img->w-1 || !(x+1)%32) {
+ *(s->mask++) = bits;
+ bits = 0;
+ } else bits = bits << 1;
+ }
+ px = (uint16_t *) ((uint8_t *) px + img->pitch - 2*img->w);
+ }
+ SDL_UnlockSurface(img);
+}
+
+#ifndef max
+#define max(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
+int
+collide(int xdiff, int ydiff, struct shape *r, struct shape *s)
+{
+ int xov, yov;
+
+ if(xdiff >= 0) xov = max(min(r->w-xdiff, s->w), 0);
+ else xov = -max(min(s->w+xdiff, r->w), 0);
+
+ if(ydiff >= 0) yov = max(min(r->h-ydiff, s->h), 0);
+ else yov = -max(min(s->h+ydiff, r->h), 0);
+
+ if(xov == 0 && yov == 0) return 0;
+
+ return 0;
+}
--- /dev/null
+#include <SDL/SDL.h>
+#include <stdint.h>
+
+struct shape {
+ uint32_t w, h;
+ uint32_t mw;
+ uint32_t *mask;
+};
+
+void get_shape(SDL_Surface *img, struct shape *s);
+int collide(int xdiff, int ydiff, struct shape *r, struct shape *s);