X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=shape.c;fp=shape.c;h=9d0501c24485ae6539af2c340b2daf1efee38fc6;hp=0000000000000000000000000000000000000000;hb=8193e09e49bc571f9c61e6e3adf45010271c08d5;hpb=05bfe1eca8ec4981dc087a97b7dfaa74eb1de59e diff --git a/shape.c b/shape.c new file mode 100644 index 0000000..9d0501c --- /dev/null +++ b/shape.c @@ -0,0 +1,63 @@ +#include +#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; yh; y++) { + for(x=0; xw; 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; +}