JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bouncy ship :)
[vor.git] / sprite.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "config.h"
5 #include "common.h"
6 #include "globals.h"
7 #include "sprite.h"
8 #include "rocks.h"
9
10 SDL_Surface *load_image(char *filename);
11 void load_ship(void);
12
13 // 2 sets of sprites, sorted by position
14 static Sprite **sprites[2] = { NULL, NULL };
15
16 // which set are we using?
17 static int set = 0;
18
19 // size of squares into which sprites are sorted.
20 static int grid_size = 0;
21
22 // screen size in grid squares.
23 static int gw = 0, gh = 0;
24
25 // lists of free sprites, by type.
26 Sprite *free_sprites[N_TYPES];
27
28
29 static void
30 get_shape(Sprite *s)
31 {
32         int x, y;
33         uint16_t *px, transp;
34         uint32_t bits = 0, bit, *p;
35
36         if(s->image->format->BytesPerPixel != 2) {
37                 fprintf(stderr, "get_shape(): not a 16-bit image!\n");
38                 exit(1);
39         }
40
41         s->w = s->image->w; s->h = s->image->h;
42         grid_size = max(grid_size, max(s->w, s->h));
43         s->mask_w = ((s->w+31)>>5);
44         s->mask = malloc(s->mask_w*s->h*sizeof(uint32_t));
45         if(!s->mask) {
46                 fprintf(stderr, "get_shape(): can't allocate bitmask.\n");
47                 exit(1);
48         }
49
50         SDL_LockSurface(s->image);
51         px = s->image->pixels;
52         transp = s->image->format->colorkey;
53         p = s->mask;
54         for(y=0; y<s->image->h; y++) {
55                 bit = 0;
56                 for(x=0; x<s->image->w; x++) {
57                         if(!bit) { bits = 0; bit = 0x80000000; }
58                         if(*px++ != transp) { bits |= bit; }
59                         bit >>= 1;
60                         if(!bit || x == s->image->w - 1) { *(p++) = bits; }
61                 }
62                 px = (uint16_t *) ((uint8_t *) px + s->image->pitch - 2*s->image->w);
63         }
64         SDL_UnlockSurface(s->image);
65 }
66
67
68 void
69 load_sprite(Sprite *s, char *filename)
70 {
71         s->image = load_image(filename);
72         if(s->image) get_shape(s);
73 }
74
75
76 static void
77 load_sprites(void)
78 {
79         load_ship();
80         load_rocks();
81 }
82
83
84 void
85 init_sprites(void)
86 {
87         load_sprites();
88
89         grid_size = grid_size * 3 / 2;
90         gw = (XSIZE-1 + 2*grid_size) / grid_size;
91         gh = (YSIZE-1 + 2*grid_size) / grid_size;
92
93         sprites[0] = malloc(2 * gw * gh * sizeof(Sprite *));
94         sprites[1] = (void *)sprites[0] + gw * gh * sizeof(Sprite *);
95         if(!sprites[0]) {
96                 fprintf(stderr, "init_sprites(): can't allocate grid squares.\n");
97                 exit(1);
98         }
99         memset(sprites[0], 0, 2 * gw * gh * sizeof(Sprite *));
100         set = 0;
101 }
102
103 static inline Sprite **
104 square(int x, int y, int set)
105 {
106         int b = (x+grid_size)/grid_size + gw*((y+grid_size)/grid_size);
107         return &sprites[set][b];
108 }
109
110 void
111 add_sprite(Sprite *s)
112 {
113         insert_sprite(square(s->x, s->y, set), s);
114 }
115
116 void
117 move_sprite(Sprite *s)
118 {
119         // move it.
120         s->x += (s->dx - screendx)*t_frame;
121         s->y += (s->dy - screendy)*t_frame;
122 }
123
124 void
125 sort_sprite(Sprite *s)
126 {
127         // clip it, or sort it into the other set of sprites.
128         if(s->x + s->w < 0 || s->x >= XSIZE
129            || s->y + s->h < 0 || s->y >= YSIZE) {
130                 insert_sprite(&free_sprites[s->type], s);
131                 s->type = NONE;
132         } else insert_sprite(square(s->x, s->y, 1-set), s);
133 }
134
135 void
136 move_sprites(void)
137 {
138         int sq;
139         Sprite **head;
140
141         // Move all the sprites (position and set)
142         for(sq=0; sq<gw*gh; sq++) {
143                 head=&sprites[set][sq];
144                 while(*head) {
145                         Sprite *s = remove_sprite(head);
146                         move_sprite(s); sort_sprite(s);
147                 }
148         }
149         set = 1-set;  // switch to other set of sprites.
150 }
151
152
153 static int
154 line_collide(int xov, unsigned bit, uint32_t *amask, uint32_t *bmask)
155 {
156         int i, words = (xov-1) >> 5;
157         uint32_t abits;
158
159         for(i=0; i<words; i++) {
160                 abits = *amask++ << bit;
161                 abits |= *amask >> (32-bit);
162                 if(abits & *bmask++) return true;
163         }
164         abits = *amask << bit;
165         if(abits & *bmask) return true;
166
167         return false;
168 }
169
170 static int
171 mask_collide(int xov, int yov, Sprite *a, Sprite *b)
172 {
173         int y;
174         int xoffset = a->w - xov;
175         int word = xoffset >> 5, bit = xoffset & 31;
176         uint32_t *amask = a->mask, *bmask = b->mask;
177
178         if(yov > 0) {
179                 amask = a->mask + ((a->h - yov) * a->mask_w) + word;
180                 bmask = b->mask;
181         } else {
182                 yov = -yov;
183                 amask = a->mask;
184                 bmask = b->mask + ((b->h - yov) * b->mask_w) + word;
185         }
186
187         for(y=0; y<yov; y++) {
188                 if(line_collide(xov, bit, amask, bmask)) return 1;
189                 amask += a->mask_w; bmask += b->mask_w;
190         }
191
192         return 0;
193 }
194
195 int
196 collide(Sprite *a, Sprite *b)
197 {
198         int dx, dy, xov, yov;
199
200         if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
201
202         dx = b->x - a->x;
203         dy = b->y - a->y;
204
205         xov = max(min(a->w - dx, b->w), 0);
206
207         if(dy >= 0) yov = max(min(a->h - dy, b->h), 0);
208         else yov = -max(min(a->h - -dy, b->h), 0);
209
210         if(xov == 0 || yov == 0) return false;
211         else return mask_collide(xov, yov, a, b);
212 }
213
214 Sprite *
215 hit_in_square(Sprite *r, Sprite *s)
216 {
217         for(; r; r=r->next)
218                 if(collide(r, s)) break;
219         return r;
220 }
221
222 Sprite *
223 collides(Sprite *s)
224 {
225         int l, r, t, b;
226         Sprite **sq;
227         Sprite *c;
228
229         l = (s->x + grid_size) / grid_size;
230         r = (s->x + s->w + grid_size) / grid_size;
231         t = (s->y + grid_size) / grid_size;
232         b = (s->y + s->h + grid_size) / grid_size;
233         sq = &sprites[set][l + t*gw];
234
235         if((c = hit_in_square(*sq, s))) return c;
236         if(l > 0 && (c = hit_in_square(*(sq-1), s))) return c;
237         if(t > 0 && (c = hit_in_square(*(sq-gw), s))) return c;
238         if(l > 0 && t > 0 && (c = hit_in_square(*(sq-1-gw), s))) return c;
239
240         if(r > l) {
241                 if((c = hit_in_square(*(sq+1), s))) return c;
242                 if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c;
243         }
244         if(b > t) {
245                 if((c = hit_in_square(*(sq+gw), s))) return c;
246                 if(l > 0 && (c = hit_in_square(*(sq-1+gw), s))) return c;
247         }
248         if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c;
249         return NULL;
250 }
251
252 int
253 pixel_collide(Sprite *s, int x, int y)
254 {
255         uint32_t pmask;
256         
257         if(x < s->x || y < s->y || x >= s->x + s->w || y >= s->y + s->h) return 0;
258
259         x -= s->x; y -= s->y;
260         pmask = 0x80000000 >> (x&0x1f);
261         return s->mask[(y*s->mask_w) + (x>>5)] & pmask;
262 }
263
264 int
265 pixel_hit_in_square(Sprite *r, float x, float y)
266 {
267         for(; r; r=r->next) {
268                 if(pixel_collide(r, x, y)) return 1;
269         }
270         return 0;
271 }
272
273 int
274 pixel_collides(float x, float y)
275 {
276         int l, t;
277         Sprite **sq;
278
279         l = (x + grid_size) / grid_size; t = (y + grid_size) / grid_size;
280         sq = &sprites[set][l + t*gw];
281         if(pixel_hit_in_square(*sq, x, y)) return true;
282         if(l > 0 && pixel_hit_in_square(*(sq-1), x, y)) return true;
283         if(t > 0 && pixel_hit_in_square(*(sq-gw), x, y)) return true;
284         if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true;
285         return false;
286 }
287
288 void
289 bounce(Sprite *a, Sprite *b)
290 {
291         float x, y, n;
292         float na, nb;
293
294         x = (b->x + b->w / 2) - (a->x + a->w / 2);
295         y = (b->y + b->h / 2) - (a->y + a->h / 2);
296         n = sqrt(x*x + y*y); x /= n; y /= n;
297
298         na = (x*a->dx + y*a->dy); // sqrt(a->dx*a->dx + a->dy*a->dy);
299         nb = (x*b->dx + y*b->dy); // sqrt(b->dx*b->dx + b->dy*b->dy);
300
301         a->dx += x*(nb-na); a->dy += y*(nb-na);
302         b->dx += x*(na-nb); b->dy += y*(na-nb);
303 }