-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.h
157 lines (114 loc) · 3.89 KB
/
game.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <stdint.h>
#include "defs.h" // generated from REFERENCE.md
// Defines
// -----------------------------------
#define IMAGE_WIDTH 256
#define TITLE_HEIGHT 128
// position of the title "level"
#define TILE_TITLE_X 2
#define TILE_TITLE_Y 15
#define LEVEL_HEIGHT 256
#define TRANSPARENT 230
#define HITBOX_COLOR 25
#define MAX_SPRITES 64 // max onscreen, can be many more per level
#define NB_SPRITETYPES 16-4 // TODO 2x16 for whole game
#define START_LIVES 5
#define START_LEVEL 0
#define SPRITE_INACTIVE 200 // still loaded but not active, of type 200+old_type
#define SPRITE_FREE 255 // not loaded, place is free
// Types
// -----------------------------------
struct Sprite {
int32_t x,y; // pos * 256
int vx,vy; // speed (as dx/dy per frame)
uint8_t hflip; // h flip. not always linked to vx
uint8_t frame; // in animation
uint8_t state; // zero init, spec depends on type
uint8_t type; // TRANSPARENT meaning undefined sprite, else type id 0-31
uint8_t tx,ty; // original position on level in tiles (absolute), 0,0 means none
uint8_t val; // dummy value for internal state
};
struct SpriteType {
// copy from data for speed/convenience
uint8_t color; // TRANSPARENT if undefined
uint8_t movement;
uint8_t collision; // type of collision - sprite_collide
uint8_t spawn; // type of sprite spawned
//uint8_t speed; // speed as byte-vector (zero is 119).
uint8_t pad[4];
// interpreted
uint8_t x,y,w,h;
uint8_t hitx1,hity1,hitx2,hity2;
};
// Variables
// -----------------------------------
extern void (*frame_handler)( void ); // pointer to frame handler.
extern uint8_t data[256*256];
extern int player_x, player_y; // position inside whole map (data seen as a 256x256 tilemap.
extern int camera_x, camera_y; // vertical position of the title/scroll
extern uint8_t current_level_color; // color in minimap of current level
extern uint8_t level_x1,level_y1,level_x2,level_y2;
extern struct SpriteType sprtype[NB_SPRITETYPES];
extern struct Sprite sprite[MAX_SPRITES];
extern int lives, coins, level, keys;
extern char hud[20]; // 0123456789:lkgtWLEVS (l=life,k=key,g=gold, t=time) -> 0123456789ABCDEFGHIJ and ' ' is space
// Functions
// -----------------------------------
void frame_die (void);
void frame_play (void);
void frame_leveltitle(void);
void frame_title(void);
void frame_logo (void);
void frame_error(void);
void enter_title(void);
void enter_leveltitle(void);
void enter_logo (void);
void next_level(void);
// loader
int loader_init (); // init loader
int load_next(); // load next file in directory
int load_bmp (const char *filename); // --> init + load_next (cycle)
int load_game_data (uint8_t *data);
int sine (uint8_t phi);
// sprites
void interpret_spritetypes();
void sprites_reset();
void sprite_kill(struct Sprite *spr);
void sprite_move(struct Sprite *spr);
void all_sprite_move() ;
uint8_t collision_tile(const struct Sprite *spr);
struct Sprite *spawn_sprite(uint8_t type, int x, int y);
// player
void move_camera(void);
void move_player(void);
void player_reset(void);
void player_kill();
void get_level_start(void); // set player at its first position in level
// sounds
void play_sfx( int sfx_id );
void play_song();
void stop_song();
// player
void manage_sprites();
uint8_t terrain_at(int x, int y);
// mapper
void black_mapper(void);
// Pure inlines
// ----------------------------------
// read pixel at x,y (in PIXELS) in tile number X
inline uint8_t read_tile(const uint8_t tile_id, const int x, const int y)
{
return data[(tile_id/16*16+y)*256+(tile_id%16)*16 + x];
}
// get property id (0-7) from object id (to sprite_type)
// objects 0-4 are levels !
inline uint8_t get_property(const int object_id, const int property)
{
return read_tile(0xF1,property+(object_id>=16)*8,object_id%16);
}
// tile id to terrain (ie. read minimap)
inline uint8_t get_terrain (const uint8_t tile_id)
{
return read_tile(240,tile_id%16,tile_id/16);
}