10 SDL_Surface *load_image(char *filename);
13 // 2 sets of sprites, sorted by position
14 static Sprite **sprites[2] = { NULL, NULL };
16 // which set are we using?
19 // size of squares into which sprites are sorted.
20 static int grid_size = 0;
22 // screen size in grid squares.
23 static int gw = 0, gh = 0;
25 // lists of free sprites, by type.
26 Sprite *free_sprites[N_TYPES];
34 uint32_t bits = 0, bit, *p;
36 if(s->image->format->BytesPerPixel != 2) {
37 fprintf(stderr, "get_shape(): not a 16-bit image!\n");
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));
46 fprintf(stderr, "get_shape(): can't allocate bitmask.\n");
50 SDL_LockSurface(s->image);
51 px = s->image->pixels;
52 transp = s->image->format->colorkey;
54 for(y=0; y<s->image->h; y++) {
56 for(x=0; x<s->image->w; x++) {
57 if(!bit) { bits = 0; bit = 0x80000000; }
58 if(*px++ != transp) { bits |= bit; }
60 if(!bit || x == s->image->w - 1) { *(p++) = bits; }
62 px = (uint16_t *) ((uint8_t *) px + s->image->pitch - 2*s->image->w);
64 SDL_UnlockSurface(s->image);
69 load_sprite(Sprite *s, char *filename)
71 s->image = load_image(filename);
72 if(s->image) get_shape(s);
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;
93 sprites[0] = malloc(2 * gw * gh * sizeof(Sprite *));
94 sprites[1] = (void *)sprites[0] + gw * gh * sizeof(Sprite *);
96 fprintf(stderr, "init_sprites(): can't allocate grid squares.\n");
99 memset(sprites[0], 0, 2 * gw * gh * sizeof(Sprite *));
103 static inline Sprite **
104 square(int x, int y, int set)
106 int b = (x+grid_size)/grid_size + gw*((y+grid_size)/grid_size);
107 return &sprites[set][b];
111 add_sprite(Sprite *s)
113 insert_sprite(square(s->x, s->y, set), s);
117 move_sprite(Sprite *s)
120 s->x += (s->dx - screendx)*t_frame;
121 s->y += (s->dy - screendy)*t_frame;
125 sort_sprite(Sprite *s)
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);
132 } else insert_sprite(square(s->x, s->y, 1-set), s);
141 // Move all the sprites (position and set)
142 for(sq=0; sq<gw*gh; sq++) {
143 head=&sprites[set][sq];
145 Sprite *s = remove_sprite(head);
146 move_sprite(s); sort_sprite(s);
149 set = 1-set; // switch to other set of sprites.
154 line_collide(int xov, unsigned bit, uint32_t *amask, uint32_t *bmask)
156 int i, words = (xov-1) >> 5;
159 for(i=0; i<words; i++) {
160 abits = *amask++ << bit;
161 abits |= *amask >> (32-bit);
162 if(abits & *bmask++) return true;
164 abits = *amask << bit;
165 if(abits & *bmask) return true;
171 mask_collide(int xov, int yov, Sprite *a, Sprite *b)
174 int xoffset = a->w - xov;
175 int word = xoffset >> 5, bit = xoffset & 31;
176 uint32_t *amask = a->mask, *bmask = b->mask;
179 amask = a->mask + ((a->h - yov) * a->mask_w) + word;
184 bmask = b->mask + ((b->h - yov) * b->mask_w) + word;
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;
196 collide(Sprite *a, Sprite *b)
198 int dx, dy, xov, yov;
200 if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
205 xov = max(min(a->w - dx, b->w), 0);
207 if(dy >= 0) yov = max(min(a->h - dy, b->h), 0);
208 else yov = -max(min(a->h - -dy, b->h), 0);
210 if(xov == 0 || yov == 0) return false;
211 else return mask_collide(xov, yov, a, b);
215 hit_in_square(Sprite *r, Sprite *s)
218 if(collide(r, s)) break;
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];
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;
241 if((c = hit_in_square(*(sq+1), s))) return c;
242 if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c;
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;
248 if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c;
253 pixel_collide(Sprite *s, int x, int y)
257 if(x < s->x || y < s->y || x >= s->x + s->w || y >= s->y + s->h) return 0;
259 x -= s->x; y -= s->y;
260 pmask = 0x80000000 >> (x&0x1f);
261 return s->mask[(y*s->mask_w) + (x>>5)] & pmask;
265 pixel_hit_in_square(Sprite *r, float x, float y)
267 for(; r; r=r->next) {
268 if(pixel_collide(r, x, y)) return 1;
274 pixel_collides(float x, float y)
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;
289 bounce(Sprite *a, Sprite *b)
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;
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);
301 a->dx += x*(nb-na); a->dy += y*(nb-na);
302 b->dx += x*(na-nb); b->dy += y*(na-nb);