X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=sprite.c;h=1a35a6a16266a6c3fb4b0db34c86d31063c30988;hp=38dae30fc504afa41b5346cd963734a5625eec13;hb=37b3a2e06abc694205e843a573c4bb2eaaabb730;hpb=5c8f059629c2127848ce4051296d2f5897bf5c0f diff --git a/sprite.c b/sprite.c index 38dae30..1a35a6a 100644 --- a/sprite.c +++ b/sprite.c @@ -1,3 +1,4 @@ +#include #include #include #include "config.h" @@ -32,6 +33,7 @@ get_shape(Sprite *s) uint16_t *px, transp; uint32_t bits = 0, bit, *p; + s->area = 0; if(s->image->format->BytesPerPixel != 2) { fprintf(stderr, "get_shape(): not a 16-bit image!\n"); exit(1); @@ -54,7 +56,7 @@ get_shape(Sprite *s) bit = 0; for(x=0; ximage->w; x++) { if(!bit) { bits = 0; bit = 0x80000000; } - if(*px++ != transp) { bits |= bit; } + if(*px++ != transp) { bits |= bit; s->area++; } bit >>= 1; if(!bit || x == s->image->w - 1) { *(p++) = bits; } } @@ -113,11 +115,25 @@ add_sprite(Sprite *s) } void +reset_sprites(void) +{ + int i; + + for(i=0; itype], s); + s->flags = 0; + } +} + +void move_sprite(Sprite *s) { - // move it. - s->x += (s->dx - screendx)*t_frame; - s->y += (s->dy - screendy)*t_frame; + if(s->flags & MOVE) { + s->x += (s->dx - screendx)*t_frame; + s->y += (s->dy - screendy)*t_frame; + } } void @@ -127,7 +143,7 @@ sort_sprite(Sprite *s) if(s->x + s->w < 0 || s->x >= XSIZE || s->y + s->h < 0 || s->y >= YSIZE) { insert_sprite(&free_sprites[s->type], s); - s->type = NONE; + s->flags = 0; } else insert_sprite(square(s->x, s->y, 1-set), s); } @@ -137,7 +153,7 @@ move_sprites(void) int sq; Sprite **head; - // Move all the sprites (position and set) + // Move all the sprites for(sq=0; sqx < a->x) { Sprite *tmp = a; a = b; b = tmp; } dx = b->x - a->x; @@ -204,26 +222,48 @@ collide(Sprite *a, Sprite *b) xov = max(min(a->w - dx, b->w), 0); if(dy >= 0) yov = max(min(a->h - dy, b->h), 0); - else yov = -max(min(a->h - -dy, b->h), 0); + else yov = -max(min(b->h - -dy, a->h), 0); if(xov == 0 || yov == 0) return false; else return mask_collide(xov, yov, a, b); } -int -hit_in_square(Sprite *r, Sprite *s) +void +collide_with_list(Sprite *s, Sprite *list) { - for(; r; r=r->next) { - if(collide(r, s)) return true; + for(; list; list=list->next) + if(collide(s, list)) do_collision(s, list); +} + +void +collisions(void) +{ + int i, end = gw*gh; + Sprite *s; + for(i=0; inext) { + collide_with_list(s, s->next); + if(i+1 < end) collide_with_list(s, sprites[set][i+1]); + if(i+gw < end) collide_with_list(s, sprites[set][i+gw]); + if(i+gw+1 < end) collide_with_list(s, sprites[set][i+gw+1]); + } } - return false; } -int +Sprite * +hit_in_square(Sprite *r, Sprite *s) +{ + for(; r; r=r->next) + if(collide(r, s)) break; + return r; +} + +Sprite * collides(Sprite *s) { int l, r, t, b; Sprite **sq; + Sprite *c; l = (s->x + grid_size) / grid_size; r = (s->x + s->w + grid_size) / grid_size; @@ -231,27 +271,29 @@ collides(Sprite *s) b = (s->y + s->h + grid_size) / grid_size; sq = &sprites[set][l + t*gw]; - if(hit_in_square(*sq, s)) return true; - if(l > 0 && hit_in_square(*(sq-1), s)) return true; - if(t > 0 && hit_in_square(*(sq-gw), s)) return true; - if(l > 0 && t > 0 && hit_in_square(*(sq-1-gw), s)) return true; + if((c = hit_in_square(*sq, s))) return c; + if(l > 0 && (c = hit_in_square(*(sq-1), s))) return c; + if(t > 0 && (c = hit_in_square(*(sq-gw), s))) return c; + if(l > 0 && t > 0 && (c = hit_in_square(*(sq-1-gw), s))) return c; if(r > l) { - if(hit_in_square(*(sq+1), s)) return true; - if(t > 0 && hit_in_square(*(sq+1-gw), s)) return true; + if((c = hit_in_square(*(sq+1), s))) return c; + if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c; } if(b > t) { - if(hit_in_square(*(sq+gw), s)) return true; - if(l > 0 && hit_in_square(*(sq-1+gw), s)) return true; + if((c = hit_in_square(*(sq+gw), s))) return c; + if(l > 0 && (c = hit_in_square(*(sq-1+gw), s))) return c; } - if(r > l && b > t && hit_in_square(*(sq+1+gw), s)) return true; - return false; + if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c; + return NULL; } int pixel_collide(Sprite *s, int x, int y) { uint32_t pmask; + + if(!COLLIDES(s)) return false; if(x < s->x || y < s->y || x >= s->x + s->w || y >= s->y + s->h) return 0; @@ -260,26 +302,60 @@ pixel_collide(Sprite *s, int x, int y) return s->mask[(y*s->mask_w) + (x>>5)] & pmask; } -int +Sprite * pixel_hit_in_square(Sprite *r, float x, float y) { for(; r; r=r->next) { - if(pixel_collide(r, x, y)) return 1; + if(COLLIDES(r) && pixel_collide(r, x, y)) return r; } return 0; } -int +Sprite * pixel_collides(float x, float y) { int l, t; Sprite **sq; + Sprite *ret; l = (x + grid_size) / grid_size; t = (y + grid_size) / grid_size; sq = &sprites[set][l + t*gw]; - if(pixel_hit_in_square(*sq, x, y)) return true; - if(l > 0 && pixel_hit_in_square(*(sq-1), x, y)) return true; - if(t > 0 && pixel_hit_in_square(*(sq-gw), x, y)) return true; - if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true; - return false; + if((ret = pixel_hit_in_square(*sq, x, y))) return ret; + if(l > 0 && (ret = pixel_hit_in_square(*(sq-1), x, y))) return ret; + if(t > 0 && (ret = pixel_hit_in_square(*(sq-gw), x, y))) return ret; + if(l > 0 && t > 0 && (ret = pixel_hit_in_square(*(sq-1-gw), x, y))) return ret; + return 0; +} + + +float +sprite_mass(Sprite *s) +{ + if(s->type == SHIP) return s->area; + else if(s->type == ROCK) return 3 * s->area; + else return 0; +} + +void +bounce(Sprite *a, Sprite *b) +{ + float x, y, n; + float va, vb, vc; + float ma, mb; + + // (x, y) is unit vector pointing from A's center to B's center. + x = (b->x + b->w / 2) - (a->x + a->w / 2); + y = (b->y + b->h / 2) - (a->y + a->h / 2); + n = sqrt(x*x + y*y); x /= n; y /= n; + + // velocities along (x, y), or 0 if already moving away. + va = x*a->dx + y*a->dy; + vb = x*b->dx + y*b->dy; + if(vb-va > 0) return; + + ma = sprite_mass(a); mb = sprite_mass(b); + vc = (va*ma + vb*mb) / (ma+mb); + + a->dx += 2*x*(vc-va); a->dy += 2*y*(vc-va); + b->dx += 2*x*(vc-vb); b->dy += 2*y*(vc-vb); }