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