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