X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=sprite.c;h=38dae30fc504afa41b5346cd963734a5625eec13;hp=760af111e0ac65253a7b9dd8dfbc4c9f721462d3;hb=5c8f059629c2127848ce4051296d2f5897bf5c0f;hpb=0d415d88f7ecd55273b09b619170078f7d35a910 diff --git a/sprite.c b/sprite.c index 760af11..38dae30 100644 --- a/sprite.c +++ b/sprite.c @@ -1,15 +1,31 @@ #include +#include +#include "config.h" #include "common.h" +#include "globals.h" #include "sprite.h" +#include "rocks.h" -void -load_sprite(Sprite *s, char *filename) -{ - s->image = load_image(filename); - if(s->image) get_shape(s); -} +SDL_Surface *load_image(char *filename); +void load_ship(void); -void +// 2 sets of sprites, sorted by position +static Sprite **sprites[2] = { NULL, NULL }; + +// which set are we using? +static int set = 0; + +// size of squares into which sprites are sorted. +static int grid_size = 0; + +// screen size in grid squares. +static int gw = 0, gh = 0; + +// lists of free sprites, by type. +Sprite *free_sprites[N_TYPES]; + + +static void get_shape(Sprite *s) { int x, y; @@ -22,8 +38,9 @@ get_shape(Sprite *s) } s->w = s->image->w; s->h = s->image->h; - s->mask_w = ((s->image->w+31)>>5); - s->mask = malloc(4*s->mask_w*s->h); + grid_size = max(grid_size, max(s->w, s->h)); + s->mask_w = ((s->w+31)>>5); + s->mask = malloc(s->mask_w*s->h*sizeof(uint32_t)); if(!s->mask) { fprintf(stderr, "get_shape(): can't allocate bitmask.\n"); exit(1); @@ -46,6 +63,92 @@ get_shape(Sprite *s) SDL_UnlockSurface(s->image); } + +void +load_sprite(Sprite *s, char *filename) +{ + s->image = load_image(filename); + if(s->image) get_shape(s); +} + + +static void +load_sprites(void) +{ + load_ship(); + load_rocks(); +} + + +void +init_sprites(void) +{ + load_sprites(); + + grid_size = grid_size * 3 / 2; + gw = (XSIZE-1 + 2*grid_size) / grid_size; + gh = (YSIZE-1 + 2*grid_size) / grid_size; + + sprites[0] = malloc(2 * gw * gh * sizeof(Sprite *)); + sprites[1] = (void *)sprites[0] + gw * gh * sizeof(Sprite *); + if(!sprites[0]) { + fprintf(stderr, "init_sprites(): can't allocate grid squares.\n"); + exit(1); + } + memset(sprites[0], 0, 2 * gw * gh * sizeof(Sprite *)); + set = 0; +} + +static inline Sprite ** +square(int x, int y, int set) +{ + int b = (x+grid_size)/grid_size + gw*((y+grid_size)/grid_size); + return &sprites[set][b]; +} + +void +add_sprite(Sprite *s) +{ + insert_sprite(square(s->x, s->y, set), s); +} + +void +move_sprite(Sprite *s) +{ + // move it. + s->x += (s->dx - screendx)*t_frame; + s->y += (s->dy - screendy)*t_frame; +} + +void +sort_sprite(Sprite *s) +{ + // clip it, or sort it into the other set of sprites. + 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; + } else insert_sprite(square(s->x, s->y, 1-set), s); +} + +void +move_sprites(void) +{ + int sq; + Sprite **head; + + // Move all the sprites (position and set) + for(sq=0; sqnext) { + if(collide(r, s)) return true; + } + return false; +} + +int +collides(Sprite *s) +{ + int l, r, t, b; + Sprite **sq; + + l = (s->x + grid_size) / grid_size; + r = (s->x + s->w + grid_size) / grid_size; + t = (s->y + grid_size) / grid_size; + 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(r > l) { + if(hit_in_square(*(sq+1), s)) return true; + if(t > 0 && hit_in_square(*(sq+1-gw), s)) return true; + } + 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(r > l && b > t && hit_in_square(*(sq+1+gw), s)) return true; + return false; +} + +int pixel_collide(Sprite *s, int x, int y) { uint32_t pmask; @@ -118,3 +259,27 @@ pixel_collide(Sprite *s, int x, int y) pmask = 0x80000000 >> (x&0x1f); return s->mask[(y*s->mask_w) + (x>>5)] & pmask; } + +int +pixel_hit_in_square(Sprite *r, float x, float y) +{ + for(; r; r=r->next) { + if(pixel_collide(r, x, y)) return 1; + } + return 0; +} + +int +pixel_collides(float x, float y) +{ + int l, t; + Sprite **sq; + + 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; +}