X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=shape.c;h=f29bd6ed0a6edcbb59d68d4b784a193f71ee757e;hp=867610e8541993ae7b1a208e1487263b3cdcd721;hb=cd7fb220b16a73c15ff9dff7a5627bf78478875f;hpb=b7de8607ee49edb99979968d5149665bf5590087 diff --git a/shape.c b/shape.c index 867610e..f29bd6e 100644 --- a/shape.c +++ b/shape.c @@ -1,4 +1,5 @@ #include +#include "common.h" #include "shape.h" void @@ -6,13 +7,14 @@ get_shape(SDL_Surface *img, struct shape *s) { int x, y; uint16_t *px, transp; - uint32_t bits, bit, *p; + uint32_t bits = 0, bit, *p; if(img->format->BytesPerPixel != 2) { fprintf(stderr, "get_shape(): not a 16-bit image!\n"); exit(1); } + s->area = 0; s->w = img->w; s->h = img->h; s->mw = ((img->w+31)>>5); s->mask = malloc(4*s->mw*s->h); @@ -29,7 +31,7 @@ get_shape(SDL_Surface *img, struct shape *s) bit = 0; for(x=0; xw; x++) { if(!bit) { bits = 0; bit = 0x80000000; } - if(*px++ != transp) bits |= bit; + if(*px++ != transp) { bits |= bit; s->area++; } bit >>= 1; if(!bit || x == img->w - 1) { *(p++) = bits; } } @@ -38,18 +40,6 @@ get_shape(SDL_Surface *img, struct shape *s) 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 - -#ifndef abs -#define abs(a) ((a)<=0 ? -(a) : (a)) -#endif - int line_collide(int xov, struct shape *r, uint32_t *rbits, struct shape *s, uint32_t *sbits) { @@ -118,3 +108,14 @@ collide(int xdiff, int ydiff, struct shape *r, struct shape *s) if(xov == 0 || yov == 0) return 0; // bboxes hit? else return mask_collide(xov, yov, r, s); } + +int +pixel_collide(unsigned int xoff, unsigned int yoff, struct shape *r) +{ + uint32_t pmask; + + if(xoff >= r->w || yoff >= r->h) return 0; + + pmask = 0x80000000 >> (xoff&0x1f); + return r->mask[(yoff*r->mw) + (xoff>>5)] & pmask; +}