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
|
/*
* Main game module with entry point and game loop.
*
* The game module sets up the window and GL context and defers the core game
* logic to a plugin.
*/
#define _GNU_SOURCE 200112L // For readlink()
#include "game.h"
#include "plugins/plugin.h"
#include <gfx/render_backend.h>
#include <gfx/scene/camera.h>
#include <gfx/scene/object.h>
#include <error.h>
#include <log/log.h>
#include <math/camera.h>
#include <plugin.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <linux/limits.h>
#include <unistd.h>
#undef _GNU_SOURCE
// Plugin to load if no plugin is provided.
static const char* DEFAULT_PLUGIN = "texture_view";
bool game_new(Game* game, int argc, const char** argv) {
assert(game);
// Syntax: game [plugin] <plugin args>
//
// Here we consume the [plugin] arg so that plugins receive the remainder
// args starting from 0.
game->argc = argc - 1;
game->argv = argv + 1;
char exe_path_buf[NAME_MAX] = {0};
if (readlink("/proc/self/exe", exe_path_buf, sizeof(exe_path_buf)) == -1) {
LOGE("readlink(/proc/self/exe) failed");
goto cleanup;
}
// Replace the last / with a null terminator to remove the exe file from the
// path. This gets the file's parent directory.
*strrchr(exe_path_buf, '/') = 0;
const mstring exe_dir = mstring_make(exe_path_buf);
const mstring plugins_path = mstring_concat_cstr(exe_dir, "/src/plugins");
if (!(game->plugin_engine = new_plugin_engine(
&(PluginEngineDesc){.plugins_dir = mstring_cstr(&plugins_path)}))) {
goto cleanup;
}
const char* plugin = argc > 1 ? argv[1] : DEFAULT_PLUGIN;
if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) {
goto cleanup;
}
if (!(game->gfx = gfx_init())) {
goto cleanup;
}
if (!(game->scene = gfx_make_scene())) {
goto cleanup;
}
if (!(game->camera = gfx_make_camera())) {
goto cleanup;
}
if (plugin_resolve(game->plugin, plugin_init, "init")) {
void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game);
if (!plugin_state) {
goto cleanup;
}
set_plugin_state(game->plugin, plugin_state);
}
if (plugin_resolve(game->plugin, plugin_boot, "boot")) {
void* plugin_state = get_plugin_state(game->plugin);
bool boot_success =
plugin_call(game->plugin, plugin_boot, "boot", plugin_state, game);
if (!boot_success) {
goto cleanup;
}
}
return true;
cleanup:
LOGE("Gfx error: %s", get_error());
game_end(game);
return false;
}
void game_end(Game* game) {
assert(game);
if (game->gfx) {
gfx_destroy(&game->gfx);
}
if (game->plugin) {
delete_plugin(&game->plugin);
}
if (game->plugin_engine) {
delete_plugin_engine(&game->plugin_engine);
}
}
void game_update(Game* game, double t, double dt) {
plugin_engine_update(game->plugin_engine);
if (plugin_reloaded(game->plugin) &&
plugin_resolve(game->plugin, plugin_init, "init")) {
void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game);
assert(plugin_state); // TODO: handle error better.
set_plugin_state(game->plugin, plugin_state);
}
if (plugin_resolve(game->plugin, plugin_update, "update")) {
// Plugin state may be null if plugin does not expose init().
void* plugin_state = get_plugin_state(game->plugin);
plugin_call(
game->plugin, plugin_update, "update", plugin_state, game, t, dt);
}
}
void game_render(const Game* game) {
RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
Renderer* renderer = gfx_get_renderer(game->gfx);
gfx_start_frame(render_backend);
gfx_render_scene(
renderer,
&(RenderSceneParams){
.mode = RenderDefault, .scene = game->scene, .camera = game->camera});
if (plugin_resolve(game->plugin, plugin_render, "render")) {
// Plugin state may be null if plugin does not expose init().
void* plugin_state = get_plugin_state(game->plugin);
plugin_call(game->plugin, plugin_render, "render", plugin_state, game);
}
gfx_end_frame(render_backend);
}
void game_set_viewport(Game* game, int width, int height) {
RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
gfx_set_viewport(render_backend, width, height);
const R fovy = 90 * TO_RAD;
const R aspect = (R)width / (R)height;
const R near = 0.1;
const R far = 1000;
const mat4 projection = mat4_perspective(fovy, aspect, near, far);
Camera* camera = gfx_get_camera_camera(game->camera);
camera->projection = projection;
}
|