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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#ifndef __GAME_H__
#define __GAME_H__
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <stdbool.h>
#define MAX_AUDIOS 10
#define MAX_TEXTURES 100
#define MAX_WIDGETS_PER_LAYER 10
#define MAX_ENTITIES 128
#define MAX_TILES 64
#define MAX_BULLETS 32
#define BULLET_WIDTH 8
#define MENU_LAYER 0
#define GAME_LAYER 1
#define MOUSE_BUTTON 0
#define MOUSE_MOTION 1
#define POSITION_CENTERED -1
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
enum GameStates
{
QUIT = 0,
RUNNING = 1,
MENU = 2
};
struct Entity
{
float x, y;
float dx, dy; // Velocity
float d2x, d2y; // Acceleration
int w, h;
bool collidable;
bool mirror;
bool fixed;
bool use_src_rect;
int i, j;
SDL_Rect src_rect;
SDL_Texture *texture;
char *name;
void (*on_collision)(struct Entity *this, struct Entity *other);
};
struct Camera
{
SDL_Rect rect;
};
struct Color
{
unsigned char r, g, b;
};
struct Widget
{
bool use_src_rect;
SDL_Rect rect, src_rect;
SDL_Texture *texture;
struct Color color;
struct Color on_hover_color;
struct Color default_color;
bool is_hover;
bool is_focus;
bool is_blended;
int initial_x;
int initial_y;
void (*callback)(struct Widget *);
};
enum GunType
{
REVOLVER = 0,
RIFLE = 1,
SHOTGUN = 2
};
struct Gun
{
unsigned char type;
float angle;
unsigned char projectiles_per_shot;
bool ready;
float cooldown;
float cooldown_elapsed;
unsigned char damage;
unsigned char bullets;
unsigned char max_bullets;
Mix_Chunk *sound_effect;
};
struct PlayerKeys
{
unsigned char jump;
unsigned char left, right;
unsigned char fire, reload;
unsigned char weaponds[3];
};
struct Player
{
struct Entity entity;
unsigned char current_gun;
struct Gun guns[3];
bool facing_left;
bool on_ground;
bool reloading;
char life;
char score;
float reload_time;
char name[16];
struct PlayerKeys keys;
struct Widget *hearts[3];
struct Widget *gun;
int counter;
};
struct Bullet
{
struct Entity entity;
struct Player *owner;
struct Bullet *next;
unsigned char index;
unsigned char damage;
};
struct Layer
{
//int num_entities;
//struct Entity *entities[MAX_ENTITIES_PER_LAYER];
int num_widgets;
struct Widget widgets[MAX_WIDGETS_PER_LAYER];
};
struct Menu
{
struct Widget *play_button;
struct Widget *quit_button;
};
struct TextureContainer
{
char name[16];
SDL_Texture *texture;
};
struct Resources
{
int num_textures;
SDL_Texture *textures[MAX_TEXTURES];
int num_stored_textures;
struct TextureContainer contained_textures[MAX_TEXTURES];
int num_audios;
Mix_Chunk *audios[MAX_AUDIOS];
TTF_Font *font;
};
struct MainGame
{
SDL_Window *window;
SDL_Renderer *renderer;
int width, height;
unsigned char state;
int gravity;
int num_players;
struct Player players[2];
// 0 -> game, 1 -> menu
struct Layer layers[2];
unsigned char current_layer;
struct Resources resources;
struct Menu menu;
const Uint8 *keys;
unsigned int bullets_left;
SDL_Texture *bullet_texture;
struct Bullet *bullets[MAX_BULLETS];
int num_entities;
struct Entity *entities[MAX_ENTITIES];
int num_tiles;
struct Entity tiles[MAX_TILES];
float frame_time;
SDL_Texture *guns[3];
Mix_Chunk *revolver_sound, *rifle_sound;
struct Widget *score;
struct Camera camera;
};
extern struct MainGame *game;
/* Util */
SDL_Texture *load_texture(const char *name);
Mix_Chunk *load_sound(const char *name);
void die(const char *error, ...) __attribute__ ((noreturn));
/* GUI */
struct Widget *add_button(unsigned char layer,
const char *label,
int x, int y,
void (*callback)(struct Widget *));
struct Widget *add_image(unsigned char layer, SDL_Texture *image,
int x, int y, int w, int h);
void process_widget_events(unsigned char type, int x, int y);
void set_color(struct Widget *w, struct Color color);
void set_text(struct Widget *w, const char *text);
struct Widget *add_label(unsigned char layer,
const char *text,
struct Color color,
int x, int y);
/* Renderer */
void render_begin(void);
void render_entities(void);
void render_flush(void);
/* Bullets */
void init_bullets(void);
void shoot_bullet(struct Player *owner);
void update_bullets(void);
/* Players */
void init_player(struct Player *p, struct PlayerKeys *keys,
float x, float y, const char *name);
void update_player(struct Player *p);
void update_score(void);
/* Collisions */
void check_collisions(void);
void load_config(const char *config);
#endif
|