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