JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
renamed *_SPRITE to *, made rocks bounce off each other.
[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         s->area = 0;
37         if(s->image->format->BytesPerPixel != 2) {
38                 fprintf(stderr, "get_shape(): not a 16-bit image!\n");
39                 exit(1);
40         }
41
42         s->w = s->image->w; s->h = s->image->h;
43         grid_size = max(grid_size, max(s->w, s->h));
44         s->mask_w = ((s->w+31)>>5);
45         s->mask = malloc(s->mask_w*s->h*sizeof(uint32_t));
46         if(!s->mask) {
47                 fprintf(stderr, "get_shape(): can't allocate bitmask.\n");
48                 exit(1);
49         }
50
51         SDL_LockSurface(s->image);
52         px = s->image->pixels;
53         transp = s->image->format->colorkey;
54         p = s->mask;
55         for(y=0; y<s->image->h; y++) {
56                 bit = 0;
57                 for(x=0; x<s->image->w; x++) {
58                         if(!bit) { bits = 0; bit = 0x80000000; }
59                         if(*px++ != transp) { bits |= bit; s->area++; }
60                         bit >>= 1;
61                         if(!bit || x == s->image->w - 1) { *(p++) = bits; }
62                 }
63                 px = (uint16_t *) ((uint8_t *) px + s->image->pitch - 2*s->image->w);
64         }
65         SDL_UnlockSurface(s->image);
66 }
67
68
69 void
70 load_sprite(Sprite *s, char *filename)
71 {
72         s->image = load_image(filename);
73         if(s->image) get_shape(s);
74 }
75
76
77 static void
78 load_sprites(void)
79 {
80         load_ship();
81         load_rocks();
82 }
83
84
85 void
86 init_sprites(void)
87 {
88         load_sprites();
89
90         grid_size = grid_size * 3 / 2;
91         gw = (XSIZE-1 + 2*grid_size) / grid_size;
92         gh = (YSIZE-1 + 2*grid_size) / grid_size;
93
94         sprites[0] = malloc(2 * gw * gh * sizeof(Sprite *));
95         sprites[1] = (void *)sprites[0] + gw * gh * sizeof(Sprite *);
96         if(!sprites[0]) {
97                 fprintf(stderr, "init_sprites(): can't allocate grid squares.\n");
98                 exit(1);
99         }
100         memset(sprites[0], 0, 2 * gw * gh * sizeof(Sprite *));
101         set = 0;
102 }
103
104 static inline Sprite **
105 square(int x, int y, int set)
106 {
107         int b = (x+grid_size)/grid_size + gw*((y+grid_size)/grid_size);
108         return &sprites[set][b];
109 }
110
111 void
112 add_sprite(Sprite *s)
113 {
114         insert_sprite(square(s->x, s->y, set), s);
115 }
116
117 void
118 move_sprite(Sprite *s)
119 {
120         // move it.
121         s->x += (s->dx - screendx)*t_frame;
122         s->y += (s->dy - screendy)*t_frame;
123 }
124
125 void
126 sort_sprite(Sprite *s)
127 {
128         // clip it, or sort it into the other set of sprites.
129         if(s->x + s->w < 0 || s->x >= XSIZE
130            || s->y + s->h < 0 || s->y >= YSIZE) {
131                 insert_sprite(&free_sprites[s->type], s);
132                 s->type = NONE;
133         } else insert_sprite(square(s->x, s->y, 1-set), s);
134 }
135
136 void
137 move_sprites(void)
138 {
139         int sq;
140         Sprite **head;
141
142         // Move all the sprites (position and set)
143         for(sq=0; sq<gw*gh; sq++) {
144                 head=&sprites[set][sq];
145                 while(*head) {
146                         Sprite *s = remove_sprite(head);
147                         move_sprite(s); sort_sprite(s);
148                 }
149         }
150         set = 1-set;  // switch to other set of sprites.
151 }
152
153
154 static int
155 line_collide(int xov, unsigned bit, uint32_t *amask, uint32_t *bmask)
156 {
157         int i, words = (xov-1) >> 5;
158         uint32_t abits;
159
160         for(i=0; i<words; i++) {
161                 abits = *amask++ << bit;
162                 abits |= *amask >> (32-bit);
163                 if(abits & *bmask++) return true;
164         }
165         abits = *amask << bit;
166         if(abits & *bmask) return true;
167
168         return false;
169 }
170
171 static int
172 mask_collide(int xov, int yov, Sprite *a, Sprite *b)
173 {
174         int y;
175         int xoffset = a->w - xov;
176         int word = xoffset >> 5, bit = xoffset & 31;
177         uint32_t *amask = a->mask, *bmask = b->mask;
178
179         if(yov > 0) {
180                 amask = a->mask + ((a->h - yov) * a->mask_w) + word;
181                 bmask = b->mask;
182         } else {
183                 yov = -yov;
184                 amask = a->mask;
185                 bmask = b->mask + ((b->h - yov) * b->mask_w) + word;
186         }
187
188         for(y=0; y<yov; y++) {
189                 if(line_collide(xov, bit, amask, bmask)) return 1;
190                 amask += a->mask_w; bmask += b->mask_w;
191         }
192
193         return 0;
194 }
195
196 int
197 collide(Sprite *a, Sprite *b)
198 {
199         int dx, dy, xov, yov;
200
201         if(a->type < 0 || b->type < 0) return false;
202
203         if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
204
205         dx = b->x - a->x;
206         dy = b->y - a->y;
207
208         xov = max(min(a->w - dx, b->w), 0);
209
210         if(dy >= 0) yov = max(min(a->h - dy, b->h), 0);
211         else yov = -max(min(b->h - -dy, a->h), 0);
212
213         if(xov == 0 || yov == 0) return false;
214         else return mask_collide(xov, yov, a, b);
215 }
216
217 void
218 collisions(void)
219 {
220         int i;
221         Sprite *a, *b;
222         for(i=0; i<gw*gh; i++)
223                 for(a=sprites[set][i]; a; a=a->next)
224                         for(b=a->next; b; b=b->next)
225                                 if(collide(a, b)) do_collision(a, b);
226 }
227
228 Sprite *
229 hit_in_square(Sprite *r, Sprite *s)
230 {
231         for(; r; r=r->next)
232                 if(collide(r, s)) break;
233         return r;
234 }
235
236 Sprite *
237 collides(Sprite *s)
238 {
239         int l, r, t, b;
240         Sprite **sq;
241         Sprite *c;
242
243         l = (s->x + grid_size) / grid_size;
244         r = (s->x + s->w + grid_size) / grid_size;
245         t = (s->y + grid_size) / grid_size;
246         b = (s->y + s->h + grid_size) / grid_size;
247         sq = &sprites[set][l + t*gw];
248
249         if((c = hit_in_square(*sq, s))) return c;
250         if(l > 0 && (c = hit_in_square(*(sq-1), s))) return c;
251         if(t > 0 && (c = hit_in_square(*(sq-gw), s))) return c;
252         if(l > 0 && t > 0 && (c = hit_in_square(*(sq-1-gw), s))) return c;
253
254         if(r > l) {
255                 if((c = hit_in_square(*(sq+1), s))) return c;
256                 if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c;
257         }
258         if(b > t) {
259                 if((c = hit_in_square(*(sq+gw), s))) return c;
260                 if(l > 0 && (c = hit_in_square(*(sq-1+gw), s))) return c;
261         }
262         if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c;
263         return NULL;
264 }
265
266 int
267 pixel_collide(Sprite *s, int x, int y)
268 {
269         uint32_t pmask;
270         
271         if(x < s->x || y < s->y || x >= s->x + s->w || y >= s->y + s->h) return 0;
272
273         x -= s->x; y -= s->y;
274         pmask = 0x80000000 >> (x&0x1f);
275         return s->mask[(y*s->mask_w) + (x>>5)] & pmask;
276 }
277
278 int
279 pixel_hit_in_square(Sprite *r, float x, float y)
280 {
281         for(; r; r=r->next) {
282                 if(pixel_collide(r, x, y)) return 1;
283         }
284         return 0;
285 }
286
287 int
288 pixel_collides(float x, float y)
289 {
290         int l, t;
291         Sprite **sq;
292
293         l = (x + grid_size) / grid_size; t = (y + grid_size) / grid_size;
294         sq = &sprites[set][l + t*gw];
295         if(pixel_hit_in_square(*sq, x, y)) return true;
296         if(l > 0 && pixel_hit_in_square(*(sq-1), x, y)) return true;
297         if(t > 0 && pixel_hit_in_square(*(sq-gw), x, y)) return true;
298         if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true;
299         return false;
300 }
301
302
303 static float
304 sprite_mass(Sprite *s)
305 {
306         if(s->type == SHIP) return s->area;
307         else if(s->type == ROCK) return 3*s->area;
308         else return 0;
309 }
310
311 void
312 bounce(Sprite *a, Sprite *b)
313 {
314         float x, y, n;
315         float va, vb;
316         float ma, mb, mr;
317
318         // (x, y) is unit vector pointing from A's center to B's center.
319         x = (b->x + b->w / 2) - (a->x + a->w / 2);
320         y = (b->y + b->h / 2) - (a->y + a->h / 2);
321         n = sqrt(x*x + y*y); x /= n; y /= n;
322
323         // velocities along (x, y), or 0 if already moving away.
324         va = max(x*a->dx + y*a->dy, 0);
325         vb = min(x*b->dx + y*b->dy, 0);
326
327         // mass ratio
328         ma = sprite_mass(a); mb = sprite_mass(b);
329         if(ma && mb) mr = mb/ma; else mr = 1;
330
331         a->dx += x*(mb*vb - ma*va)/ma; a->dy += y*(mb*vb - ma*va)/ma;
332         b->dx += x*(ma*va - mb*vb)/mb; b->dy += y*(ma*va - mb*vb)/mb;
333 }