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
|
/*
* 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/gfx.h>
#include <gfx/gfx_app.h>
#include <gfx/render_backend.h>
#include <gfx/renderer.h>
#include <gfx/scene/camera.h>
#include <gfx/scene/node.h>
#include <gfx/scene/object.h>
#include <gfx/scene/scene.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 <stdlib.h>
#include <linux/limits.h>
#include <unistd.h>
#undef _GNU_SOURCE
static const int WIDTH = 1350;
static const int HEIGHT = 900;
static const int MAX_FPS = 60;
void app_end(Game* game);
bool app_init(const GfxAppDesc* desc, void** app_state) {
assert(desc);
if (desc->argc <= 1) {
LOGE("Usage: %s <plugin> [plugin args]", desc->argv[0]);
return false;
}
Game* game = calloc(1, sizeof(Game));
if (!game) {
LOGE("Failed to allocate game state");
return false;
}
// Syntax: game <plugin> [plugin args]
//
// Here we consume the <plugin> arg so that plugins receive the remainder
// args starting from 0.
game->argc = desc->argc - 1;
game->argv = desc->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 = desc->argv[1];
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;
}
}
*app_state = game;
return true;
cleanup:
LOGE("Gfx error: %s", get_error());
app_end(game);
return false;
}
void app_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 app_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 app_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 app_resize(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;
}
GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS);
|