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