+
+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; sq<gw*gh; sq++) {
+ head=&sprites[set][sq];
+ while(*head) {
+ Sprite *s = remove_sprite(head);
+ move_sprite(s); sort_sprite(s);
+ }
+ }
+ set = 1-set; // switch to other set of sprites.
+}
+
+