1 #include "sprite.h"
2 #ifdef DEBUG
3 #include <stdio.h>
4 #endif
5 
draw_sprite(struct sprite * sprite,Sint16 x,Sint16 y,SDL_Surface * target,Uint32 now)6 int draw_sprite(struct sprite *sprite, Sint16 x, Sint16 y,
7 		SDL_Surface * target, Uint32 now) {
8   static SDL_Rect dest;
9   static SDL_Surface *source;
10 
11   source = get_current_sprite_surface(sprite, now);
12   dest.x = x;
13   dest.y = y;
14   dest.w = source->w;
15   dest.h = source->h;
16   return SDL_BlitSurface(source, NULL, target, &dest);
17 }
18 
get_current_sprite_surface(struct sprite * sprite,Uint32 now)19 SDL_Surface *get_current_sprite_surface(struct sprite * sprite, Uint32 now) {
20   return sprite->
21     surface_ptr[((now -
22 		  sprite->start_tick) / sprite->speed_divisor) %
23 		sprite->surface_count];
24 }
25 
init_sprite(struct sprite * sprite,SDL_Surface ** surfaces,Uint16 surface_count,Uint16 delay,Uint32 now)26 void init_sprite(struct sprite *sprite, SDL_Surface ** surfaces,
27 		 Uint16 surface_count, Uint16 delay, Uint32 now) {
28   sprite->start_tick = now;
29   sprite->surface_ptr = surfaces;
30   sprite->surface_count = surface_count;
31   sprite->speed_divisor = delay;
32 #ifdef DEBUG
33   printf("init sprite=%p start=$%08X divisor=$%04hx count=$%04hx\n",
34 	 sprite, sprite->start_tick, sprite->speed_divisor, surface_count);
35 #endif
36 }
37 
init_sprite2(struct sprite * sprite,sdl_surfaces_t * surfaces,Uint16 delay,Uint32 now)38 void init_sprite2(struct sprite *sprite, sdl_surfaces_t *surfaces, Uint16 delay, Uint32 now) {
39   init_sprite(sprite, surfaces->surfaces, surfaces->num_surfaces, delay, now);
40 }
41