JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
75d7730b1f67cb2f8848f10e93746c4414f50fae
[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 0
12 #define SHIP 1
13 #define ROCK 2
14 #define N_TYPES 3
15
16 struct sprite {
17         int8_t type;
18         int8_t flags;
19         Sprite *next;
20         float x, y;
21         float dx, dy;
22         SDL_Surface *image;
23         int w, h;
24         int mask_w;
25         uint32_t *mask;
26         uint32_t area;
27 };
28
29 #define MOVE 1
30 #define DRAW 2
31 #define COLLIDE 4
32
33 #define COLLIDES(sprite) ((sprite)->flags & COLLIDE)
34
35 Sprite *free_sprites[N_TYPES];  // lists of free sprites, by type.
36
37 void do_collision(Sprite *a, Sprite *b);
38 void collisions(void);
39
40 void init_sprites(void);
41 void reset_sprites(void);
42 void add_sprite(Sprite *s);
43 void move_sprite(Sprite *s);
44 void move_sprites(void);
45
46 Sprite *collides(Sprite *s);
47 int pixel_collides(float x, float y);
48 void load_sprite(Sprite *sprite, char *filename);
49
50 void bounce(Sprite *a, Sprite *b);
51
52
53 // extended sprites
54
55 struct ship {
56         // core sprite fields
57         int8_t sprite_type;
58         int8_t flags;
59         struct ship *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         uint32_t area;
67         // SHIP extras
68         int lives;
69         int jets;
70 };
71
72 struct rock {
73         // core sprite fields
74         int8_t sprite_type;
75         int8_t flags;
76         struct rock *next;
77         float x, y;
78         float dx, dy;
79         SDL_Surface *image;
80         int w, h;
81         int mask_w;
82         uint32_t *mask;
83         uint32_t area;
84         // ROCK extras
85         int type;
86 };
87
88
89
90 static inline void
91 insert_sprite(Sprite **head, Sprite *s)
92 {
93         s->next = *head;
94         *head = s;
95 }
96
97
98 static inline Sprite *
99 remove_sprite(Sprite **head)
100 {
101         Sprite *s = *head;
102         *head = s->next;
103         return s;
104 }
105
106
107 static inline void
108 draw_sprite(Sprite *s)
109 {
110         SDL_Rect dest;
111         if(s->flags & DRAW) {
112                 dest.x = s->x; dest.y = s->y;
113                 SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
114         }
115 }
116
117 #endif // VOR_SPRITE_H