JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
4c0cefdc107ccec500d6f145fb863beb26d381d3
[vor.git] / sprite.h
1 #ifndef VOR_SPRITE_H
2 #define VOR_SPRITE_H
3
4 #include <SDL.h>
5 #include <inttypes.h>
6
7 typedef struct sprite Sprite;
8
9 #define SPRITE(x) ((Sprite *) (x))
10
11 #define BASE_SPRITE 0
12 #define SHIP_SPRITE 1
13 #define ROCK_SPRITE 2
14 #define N_TYPES 3
15
16 struct sprite {
17         int8_t type;
18         Sprite *next;
19         float x, y;
20         float dx, dy;
21         SDL_Surface *image;
22         int w, h;
23         int mask_w;
24         uint32_t *mask;
25 };
26
27 Sprite *free_sprites[N_TYPES];  // lists of free sprites, by type.
28
29 void init_sprites(void);
30 void add_sprite(Sprite *s);
31 void move_sprite(Sprite *s);
32 void move_sprites(void);
33
34 int collides(Sprite *s);
35 int pixel_collides(float x, float y);
36 void load_sprite(Sprite *sprite, char *filename);
37
38
39 // extended sprites
40
41 struct ship {
42         // core sprite fields
43         int8_t sprite_type;
44         struct ship *next;
45         float x, y;
46         float dx, dy;
47         SDL_Surface *image;
48         int w, h;
49         int mask_w;
50         uint32_t *mask;
51         // SHIP extras
52         int lives;
53         int jets;
54 };
55
56 struct rock {
57         // core sprite fields
58         int8_t sprite_type;
59         struct rock *next;
60         float x, y;
61         float dx, dy;
62         SDL_Surface *image;
63         int w, h;
64         int mask_w;
65         uint32_t *mask;
66         // ROCK extras
67         int type;
68 };
69
70
71
72 static inline void
73 insert_sprite(Sprite **head, Sprite *s)
74 {
75         s->next = *head;
76         *head = s;
77 }
78
79
80 static inline Sprite *
81 remove_sprite(Sprite **head)
82 {
83         Sprite *s = *head;
84         *head = s->next;
85         return s;
86 }
87
88
89 static inline void
90 draw_sprite(Sprite *s)
91 {
92         SDL_Rect dest;
93         if(s->type == NONE) return;
94         dest.x = s->x; dest.y = s->y;
95         SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
96 }
97
98 #endif // VOR_SPRITE_H