JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bumped dev version to 0.5pre
[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 Sprite * pixel_collides(float x, float y);
48 void load_sprite(Sprite *sprite, char *filename);
49
50 float sprite_mass(Sprite *s);
51 void bounce(Sprite *a, Sprite *b);
52
53
54 // extended sprites
55
56 struct ship {
57         // core sprite fields
58         int8_t sprite_type;
59         int8_t flags;
60         struct ship *next;
61         float x, y;
62         float dx, dy;
63         SDL_Surface *image;
64         int w, h;
65         int mask_w;
66         uint32_t *mask;
67         uint32_t area;
68         // SHIP extras
69         int lives;
70         int jets;
71 };
72
73 struct rock {
74         // core sprite fields
75         int8_t sprite_type;
76         int8_t flags;
77         struct rock *next;
78         float x, y;
79         float dx, dy;
80         SDL_Surface *image;
81         int w, h;
82         int mask_w;
83         uint32_t *mask;
84         uint32_t area;
85         // ROCK extras
86         int type;
87 };
88
89
90
91 static inline void
92 insert_sprite(Sprite **head, Sprite *s)
93 {
94         s->next = *head;
95         *head = s;
96 }
97
98
99 static inline Sprite *
100 remove_sprite(Sprite **head)
101 {
102         Sprite *s = *head;
103         *head = s->next;
104         return s;
105 }
106
107
108 static inline void
109 draw_sprite(Sprite *s)
110 {
111         SDL_Rect dest;
112         if(s->flags & DRAW) {
113                 dest.x = s->x; dest.y = s->y;
114                 SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
115         }
116 }
117
118 #endif // VOR_SPRITE_H