JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added area back to get_shape, bounce() uses area.
[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         uint32_t area;
26 };
27
28 Sprite *free_sprites[N_TYPES];  // lists of free sprites, by type.
29
30 void init_sprites(void);
31 void add_sprite(Sprite *s);
32 void move_sprite(Sprite *s);
33 void move_sprites(void);
34
35 Sprite *collides(Sprite *s);
36 int pixel_collides(float x, float y);
37 void load_sprite(Sprite *sprite, char *filename);
38
39 void bounce(Sprite *a, Sprite *b);
40
41
42 // extended sprites
43
44 struct ship {
45         // core sprite fields
46         int8_t sprite_type;
47         struct ship *next;
48         float x, y;
49         float dx, dy;
50         SDL_Surface *image;
51         int w, h;
52         int mask_w;
53         uint32_t *mask;
54         uint32_t area;
55         // SHIP extras
56         int lives;
57         int jets;
58 };
59
60 struct rock {
61         // core sprite fields
62         int8_t sprite_type;
63         struct rock *next;
64         float x, y;
65         float dx, dy;
66         SDL_Surface *image;
67         int w, h;
68         int mask_w;
69         uint32_t *mask;
70         uint32_t area;
71         // ROCK extras
72         int type;
73 };
74
75
76
77 static inline void
78 insert_sprite(Sprite **head, Sprite *s)
79 {
80         s->next = *head;
81         *head = s;
82 }
83
84
85 static inline Sprite *
86 remove_sprite(Sprite **head)
87 {
88         Sprite *s = *head;
89         *head = s->next;
90         return s;
91 }
92
93
94 static inline void
95 draw_sprite(Sprite *s)
96 {
97         SDL_Rect dest;
98         if(s->type == NONE) return;
99         dest.x = s->x; dest.y = s->y;
100         SDL_BlitSurface(s->image, NULL, surf_screen, &dest);
101 }
102
103 #endif // VOR_SPRITE_H