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