-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
325 lines (266 loc) · 8.33 KB
/
core.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Set up common game functionality.
*
* Author: Tim Sjöstrand <tim.sjostrand@gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include "time.h"
#include "math4.h"
#include "graphics.h"
#include "input.h"
#include "sound.h"
#include "monotext.h"
#include "texture.h"
#include "assets.h"
#include "vfs.h"
#include "core.h"
#include "core_argv.h"
#include "core_console.h"
void core_glfw_resize_callback(GLFWwindow *window, int width, int height)
{
float ax = width;
float ay = height;
float ratio = core_global->view_height / core_global->view_width;
float max_width = height / ratio;
if (width < max_width)
{
ax = width;
ay = ax*ratio;
}
else
{
ax = ay/ratio;
ay = height;
}
float rest_width = width - ax;
float rest_height = height - ay;
glViewport(rest_width / 2.0f, rest_height / 2.0f, ax, ay);
}
void core_fix_aspect_ratio(struct core *core)
{
/* Set up ratio. */
int x, y, w, h;
glfwGetFramebufferSize(core->graphics.window, &w, &h);
core_glfw_resize_callback(core->graphics.window, w, h);
}
static void core_load(struct core* core)
{
/* Create a square white texture for texturing empty sprites with. */
texture_white(&core->textures.none);
/* Load fonts used for the console. */
monofont_new(&core->font_console, "04B03_8px.png", 8, 8, -2, 0);
/* Game specific load. */
if(core->load_callback != NULL) {
core->load_callback();
}
}
static void core_release(struct core* core)
{
texture_free(core->textures.none);
monofont_free(&core->font_console);
console_free(&core->console);
/* Release game specific resources. */
if(core->release_callback != NULL) {
core->release_callback();
}
}
static void core_assets_init(struct core* core)
{
/* Load console. */
console_new(&core->console, &core->font_console, core->view_width, 16, &core->textures.none,
core->console_shader);
/* Create core commands. */
core_console_new(&core->console);
/* Create game-specific commands. */
if (core->console_init_callback != NULL) {
core->console_init_callback(&core->console);
}
console_parse_conf(&core->console, &core->console.conf);
/* Game specific init. */
if (core->init_callback != NULL) {
core->init_callback();
}
}
static void core_think(struct core* core, struct graphics *g, float delta_time)
{
core_fix_aspect_ratio(core);
/* Common think functions. */
vfs_filewatch();
console_think(&core->console, delta_time);
sound_think(&core->sound, delta_time);
/* Game specific think. */
if(core->think_callback != NULL) {
core->think_callback(core, g, delta_time);
}
/* Input must think after game logic. */
input_think(&core->input, delta_time);
}
static void core_render(struct core* core, struct graphics *g, float delta_time)
{
/* Game specific render. */
if(core->render_callback != NULL) {
core->render_callback(core, g, delta_time);
}
/* Render the console. */
if(core->console.focused) {
console_render(&core->console, core->console_shader, g);
}
}
static void core_key_callback(struct core* core, struct input *input, GLFWwindow *window, int key, int scancode, int action, int mods)
{
/* Input control characters into console. */
if(core->console.focused && key >= 256) {
console_input_feed_control(&core->console, key, scancode, action, mods);
}
if(!core->console.focused) {
if(core->key_callback != NULL) {
core->key_callback(input->core, input, window, key, scancode, action, mods);
}
}
}
static void core_mousebutton_callback(struct core* core, GLFWwindow *window, int button, int action, int mods)
{
if (core->mousebutton_callback != NULL) {
core->mousebutton_callback(core, window, button, action, mods);
}
}
static void core_char_callback(struct core* core, struct input *input, GLFWwindow *window,
unsigned int key, int mods)
{
/* Input localized text into console. */
if(core->console.focused) {
console_input_feed_char(&core->console, key, mods);
}
/* Toggle console. */
if(key == CONSOLE_CHAR_FOCUS) {
console_toggle_focus(&core->console);
input->enabled = !core->console.focused;
}
if(core->char_callback != NULL) {
core->char_callback(input->core, input, window, key, mods);
}
}
void core_set_fps_callback(struct core* core, fps_func_t fps_callback)
{
core->fps_callback = fps_callback;
core->graphics.frames.callback = fps_callback;
}
void core_set_asset_callbacks(struct core* core, core_load_t load_callback,
core_init_t init_callback, core_release_t release_callback)
{
core->load_callback = load_callback;
core->init_callback = init_callback;
core->release_callback = release_callback;
}
void core_set_key_callback(struct core* core, input_callback_t key_callback)
{
core->key_callback = key_callback;
}
void core_set_mousebutton_callback(struct core* core, mousebutton_callback_t mousebutton_callback)
{
core->mousebutton_callback = mousebutton_callback;
}
void core_set_char_callback(struct core* core, input_char_callback_t char_callback)
{
core->char_callback = char_callback;
}
void core_set_up_sound(struct core* core, vec3 *listener, float distance_max)
{
core->sound_listener = listener;
core->sound_distance_max = distance_max;
}
void core_set_console_init_callback(struct core* core, core_console_init_t console_init_callback)
{
core->console_init_callback = console_init_callback;
}
void core_set_think_callback(struct core* core, think_func_t think_callback)
{
core->think_callback = think_callback;
}
void core_set_render_callback(struct core* core, render_func_t render_callback)
{
core->render_callback = render_callback;
}
void core_set_init_memory_callback(struct core* core, core_init_memory_t init_memory_callback)
{
core->init_memory_callback = init_memory_callback;
}
void core_get_viewport(struct core* core, float* x, float* y, float* w, float* h)
{
float buffer[4];
glGetFloatv(GL_VIEWPORT, buffer);
*x = buffer[0];
*y = buffer[1];
*w = buffer[2];
*h = buffer[3];
}
/**
* @param load_callback Responsible for registering all the VFS callbacks
* required for the game. After load_callback has been
* run, assets are immediately loaded.
* @param init_callback Runs after assets are loaded, and should set up game
* objects to their initial state.
* @param release_callback Runs just before the game quits, and should release
* all assets and free dynamically allocated memory.
*/
void core_setup(struct core* core, const char *title, int view_width, int view_height,
int window_width, int window_height, int window_mode, size_t game_memory_size)
{
/* Store global references. */
core->view_width = view_width;
core->view_height = view_height;
/* FIXME: nice way to set this pointer from game library OR engine specific console shader. */
core->console_shader = &assets->shaders.basic_shader;
/* Allocate game memory */
core->shared_memory.game_memory = malloc(game_memory_size);
core->shared_memory.core = core;
core->shared_memory.assets = assets;
core->shared_memory.vfs = vfs_global;
core->shared_memory.input = &core->input;
core->init_memory_callback(&core->shared_memory, 0);
/* Seed random number generator. */
srand(time(NULL));
/* Set up sound */
sound_init(&core->sound, (float *) core->sound_listener, core->sound_distance_max);
/* Set up graphics. */
int ret = graphics_init(&core->graphics, &core_think, &core_render,
core->fps_callback, view_width, view_height, window_mode, title,
window_width, window_height);
if(ret != GRAPHICS_OK) {
core_error("Graphics initialization failed (%d)\n", ret);
graphics_free(core, &core->graphics);
exit(ret);
}
glfwSetFramebufferSizeCallback(core->graphics.window, &core_glfw_resize_callback);
core_fix_aspect_ratio(core);
/* Get input events. */
ret = input_init(core, &core->input, core->graphics.window, &core_key_callback,
&core_char_callback, &core_mousebutton_callback);
if(ret != GRAPHICS_OK) {
core_error("Input initialization failed (%d)\n", ret);
graphics_free(core, &core->graphics);
exit(ret);
}
/* Load all assets */
core_load(core);
}
void core_run(struct core* core)
{
/* Setup. */
core_assets_init(core);
/* Loop until the user closes the window */
graphics_loop(core, &core->graphics);
/* Release assets. */
core_release(core);
/* Free OpenAL. */
sound_free(&core->sound);
/* If we reach here, quit the core. */
graphics_free(core, &core->graphics);
/* Release game memory */
free(core->shared_memory.game_memory);
}
void core_reload(struct core* core)
{
core->init_memory_callback(&core->shared_memory, 1);
}