forked from devpack/mayhem
-
Notifications
You must be signed in to change notification settings - Fork 2
/
game_mgr.cpp
executable file
·277 lines (214 loc) · 7.32 KB
/
game_mgr.cpp
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
273
274
275
276
277
#include <allegro5/allegro.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include "game_mgr.h"
#include "xc.h"
#include "allegro_compatibility.h"
#include <cstdio>
#define CHECKFPS
//#define SHOWDEBUG
#define FULLSCREEN
#ifdef FULLSCREEN
#define GFXOPENARG ALLEGRO_FULLSCREEN_WINDOW
#else
#define GFXOPENARG ALLEGRO_WINDOWED
#endif
// initialise static members
int GameManager::display_height;
int GameManager::display_width;
int GameManager::native_height;
int GameManager::native_width;
ALLEGRO_DISPLAY *GameManager::display = nullptr;
ALLEGRO_FONT *GameManager::font = nullptr;
int GameManager::FPS = 65;
XC_STATE *GameManager::joysticks[MAX_NUM_CONTROLLERS] = {0};
int GameManager::num_joysticks_loaded = 0;
//debug
float GameManager::debug_start = 0;
float GameManager::debug_time = 0;
void GameManager::Init() {
if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return;
}
uint32_t version = al_get_allegro_version();
printf("Allegro started version %i.%i.%i[%i]\n", version >> 24,
(version >> 16) & 255, (version >> 8) & 255, version & 255);
if (!al_install_keyboard()) {
fprintf(stderr, "failed to initialize the keyboard!\n");
return;
}
al_init_image_addon();
al_init_primitives_addon();
al_init_font_addon(); // initialize the font addon
al_init_ttf_addon(); // initialize the ttf (True Type Font) addon
GameManager::font = al_load_font("assets/default/PressStart2P.ttf", 8, 0);
xc_install();
int i = 0;
for (i = 0; i < MAX_NUM_CONTROLLERS; i++) {
XC_STATE *controller = xc_get_state(i);
if (!controller) {
break;
}
GameManager::joysticks[i] = controller;
}
GameManager::num_joysticks_loaded = i;
printf("Found %d joysticks\n", i);
if (!al_install_audio()) {
fprintf(stderr, "failed to initialize audio!\n");
}
if (!al_init_acodec_addon()) {
fprintf(stderr, "failed to initialize audio codecs!\n");
}
if (!al_reserve_samples(8)) {
fprintf(stderr, "failed to reserve samples!\n");
}
// get the screen resolution
if (!get_desktop_resolution(0, &display_width, &display_height)) {
// failed to get resolution, set to a default
display_width = DEFAULT_WIDTH;
display_height = DEFAULT_HEIGHT;
}
// set the native display properties
native_width = display_width;
native_height = display_height;
al_set_new_display_flags(GFXOPENARG);
GameManager::display = al_create_display(display_width, display_height);
// hide mouse pointer
al_hide_mouse_cursor(GameManager::display);
}
void GameManager::StartDebugTime() {
GameManager::debug_start = al_get_time();
}
void GameManager::EndDebugTime() {
GameManager::debug_time = al_get_time() - GameManager::debug_start;
}
void GameManager::ChangeScreenRes(int width, int height) {
if (al_resize_display(GameManager::display, width, height)) {
display_width = width;
display_height = height;
} else {
fprintf(stderr, "failed to resize display!\n");
}
}
void GameManager::Shutdown() {
al_destroy_display(GameManager::display);
for (int i = 0; i < MAX_NUM_CONTROLLERS; i++)
delete GameManager::joysticks[i];
}
double old_time = 0.0;
double tick_time = 0.0;
double tick_fps = 0.0;
#ifdef CHECKFPS
void draw_fps(ALLEGRO_BITMAP *screen_buffer) {
double new_time = al_get_time();
char info[100];
#ifdef SHOWDEBUG
sprintf(info, "goal fps:%d, draw fps:%.1f, tick ms:%.1f, tick fps: %.1f, debug time %.1f",
GameManager::FPS, 1.0 / (new_time - old_time), 1000.0 * tick_time,
tick_fps, 1000 * GameManager::debug_time);
#else
sprintf(info, "fps:%.1f", tick_fps);
#endif
textout(screen_buffer, GameManager::font, info, 105, 5,
al_map_rgb(200, 200, 200));
char reso[100];
sprintf(reso, "%ix%i", GameManager::display_width,
GameManager::display_height);
textout(screen_buffer, GameManager::font, reso, 5, 5, al_map_rgb(200, 200, 200));
old_time = new_time;
}
#else
void draw_fps(ALLEGRO_BITMAP *) {}
#endif
void GameManager::Run(GameSequence *aSeq) {
auto event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
return;
}
al_register_event_source(event_queue,
al_get_display_event_source(GameManager::display));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, xc_get_event_source());
ALLEGRO_BITMAP *screen_buffer;
screen_buffer = al_get_backbuffer(GameManager::display);
al_set_target_bitmap(screen_buffer);
al_clear_to_color(al_map_rgb(0, 0, 0));
bool key_pressed[ALLEGRO_KEY_MAX] = {0};
bool key_down[ALLEGRO_KEY_MAX] = {0};
bool doexit = false;
bool exit_game = false;
GameSequence *seq_next = nullptr;
double last_time = al_get_time();
const double delta_min = ALLEGRO_BPS_TO_SECS(GameManager::FPS);
//joy debug
/*
char debug[50];
int but = 0, axis = 0;
double pos = 0.0;
*/
while (!doexit) {
ALLEGRO_EVENT ev;
while (al_get_next_event(event_queue, &ev)) {
if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
key_pressed[ev.keyboard.keycode] = true;
key_down[ev.keyboard.keycode] = true;
} else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
key_pressed[ev.keyboard.keycode] = false;
key_down[ev.keyboard.keycode] = false;
} else if (ev.type == XC_EVENT_AXIS || ev.type == XC_EVENT_BUTTON_DOWN ||
ev.type == XC_EVENT_BUTTON_UP) {
//joy debug
/*
if (ev.type == XC_EVENT_BUTTON_DOWN) but = ev.joystick.button;
if (ev.type == XC_EVENT_AXIS) {
axis = ev.joystick.axis;
pos = ev.joystick.pos;
}
*/
xc_update(ev);
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
exit_game = true;
doexit = true;
}
}
double now = al_get_time();
bool do_tick = (now - last_time) >= delta_min;
if (do_tick) {
al_set_target_bitmap(screen_buffer);
al_clear_to_color(al_map_rgb(0, 0, 0));
seq_next = aSeq->doTick(screen_buffer, key_pressed, key_down, &exit_game,
(now - last_time));
for (auto &pressed : key_pressed)
pressed = false;
doexit = exit_game || seq_next != nullptr;
tick_fps = 1.0 / (now - last_time);
last_time = now;
tick_time = al_get_time() - now;
}
// joy debug
/*
sprintf(debug, "%i.%i.%i", but, axis, pos);
//sprintf(debug, "%s", al_get_joystick_name(GameManager::joysticks[0]->joy));
textout(screen_buffer, GameManager::font, debug, 605, 5, al_map_rgb(200, 200, 200));
*/
draw_fps(screen_buffer);
if (do_tick) {
al_flip_display();
}
if (doexit) {
if (seq_next != nullptr && seq_next->ReturnScreen() != aSeq)
delete aSeq;
if (exit_game)
break;
aSeq = seq_next;
doexit = false;
}
}
al_destroy_event_queue(event_queue);
}