JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* rocks.c: refactored stuff and fixed linked-list bug (I think).
[vor.git] / shape.c
1 #include <stdlib.h>
2 #include "common.h"
3 #include "shape.h"
4
5 void
6 get_shape(SDL_Surface *img, struct shape *s)
7 {
8         int x, y;
9         uint16_t *px, transp;
10         uint32_t bits = 0, bit, *p;
11
12         if(img->format->BytesPerPixel != 2) {
13                 fprintf(stderr, "get_shape(): not a 16-bit image!\n");
14                 exit(1);
15         }
16
17         s->area = 0;
18         s->w = img->w; s->h = img->h;
19         s->mw = ((img->w+31)>>5);
20         s->mask = malloc(4*s->mw*s->h);
21         if(!s->mask) {
22                 fprintf(stderr, "can't malloc bitmask");
23                 exit(1);
24         }
25
26         SDL_LockSurface(img);
27         px = img->pixels;
28         transp = img->format->colorkey;
29         p = s->mask;
30         for(y=0; y<img->h; y++) {
31                 bit = 0;
32                 for(x=0; x<img->w; x++) {
33                         if(!bit) { bits = 0; bit = 0x80000000; }
34                         if(*px++ != transp) { bits |= bit; s->area++; }
35                         bit >>= 1;
36                         if(!bit || x == img->w - 1) { *(p++) = bits; }
37                 }
38                 px = (uint16_t *) ((uint8_t *) px + img->pitch - 2*img->w);
39         }
40         SDL_UnlockSurface(img);
41 }
42
43 int
44 line_collide(int xov, struct shape *r, uint32_t *rbits, struct shape *s, uint32_t *sbits)
45 {
46         int lshift, n, i, ret = 0;
47         uint32_t lbits;
48         struct shape *st;
49         uint32_t *bt;
50
51
52         if(xov < 0) {
53                 st = r; r = s; s = st;
54                 bt = rbits; rbits = sbits; sbits = bt;
55                 xov = -xov;
56         }
57
58
59         lshift = (r->w - xov) & 31; 
60         rbits += (r->w - xov) >> 5;
61         n = (xov + 31) >> 5;
62         for(i=0; i<n-1; i++) {
63                 lbits = *rbits++ << lshift;
64                 lbits |= *rbits >> (32 - lshift);
65                 if(lbits & *sbits++) ret = 1;
66         }
67         lbits = *rbits << lshift;
68         if(lbits & *sbits) ret = 1;
69
70         return ret;
71 }
72
73 int
74 mask_collide(int xov, int yov, struct shape *r, struct shape *s)
75 {
76         int y, ry, sy;
77         uint32_t *rbits, *sbits;
78
79         if(yov > 0) {
80                 ry = r->h - yov; sy = 0;
81                 rbits = r->mask + (r->h - yov) * r->mw;
82                 sbits = s->mask;
83         } else {
84                 ry = 0; sy = s->h + yov;
85                 rbits = r->mask;
86                 sbits = s->mask + (s->h + yov) * s->mw;
87         }
88
89         for(y=0; y<abs(yov); y++) {
90                 if(line_collide(xov, r, rbits, s, sbits)) return 1;
91                 rbits += r->mw; sbits += s->mw;
92         }
93
94         return 0;
95 }
96
97 int
98 collide(int xdiff, int ydiff, struct shape *r, struct shape *s)
99 {
100         int xov, yov;
101
102         if(xdiff >= 0) xov = max(min(r->w-xdiff, s->w), 0);
103         else xov = min(-min(s->w+xdiff, r->w), 0);
104
105         if(ydiff >= 0) yov = max(min(r->h-ydiff, s->h), 0);
106         else yov = min(-min(s->h+ydiff, r->h), 0);
107
108         if(xov == 0 || yov == 0) return 0;  // bboxes hit?
109         else return mask_collide(xov, yov, r, s);
110 }
111
112 int
113 pixel_collide(unsigned int xoff, unsigned int yoff, struct shape *r)
114 {
115         uint32_t pmask;
116         
117         if(xoff >= r->w || yoff >= r->h) return 0;
118
119         pmask = 0x80000000 >> (xoff&0x1f);
120         return r->mask[(yoff*r->mw) + (xoff>>5)] & pmask;
121 }