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