summaryrefslogtreecommitdiff
path: root/gltfview/src/game.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-06-17 20:37:26 -0700
committer3gg <3gg@shellblade.net>2023-06-17 20:37:26 -0700
commitbe63cdf390b0bdbb00ba67cff95d165d214418fb (patch)
treef730f79bf6f04a44cb7489e3d9fd6b50f4e3b716 /gltfview/src/game.c
parent9d1eb251b5d26759e17dd7bee316cf27fdcb28bf (diff)
Allow plugins to not have to define all plugin functions.
Diffstat (limited to 'gltfview/src/game.c')
-rw-r--r--gltfview/src/game.c57
1 files changed, 26 insertions, 31 deletions
diff --git a/gltfview/src/game.c b/gltfview/src/game.c
index 7470f75..d618447 100644
--- a/gltfview/src/game.c
+++ b/gltfview/src/game.c
@@ -31,19 +31,6 @@
31// Plugin to load if no plugin is provided. 31// Plugin to load if no plugin is provided.
32static const char* DEFAULT_PLUGIN = "texture_view"; 32static const char* DEFAULT_PLUGIN = "texture_view";
33 33
34static bool validate_plugin(const Plugin* plugin) {
35#define CHECK_FUNCTION(name, signature) \
36 if (!plugin_resolve(plugin, signature, name)) { \
37 LOGE("Plugin is missing function: " #name); \
38 return false; \
39 }
40 CHECK_FUNCTION("init", plugin_init);
41 CHECK_FUNCTION("boot", plugin_boot);
42 CHECK_FUNCTION("update", plugin_update);
43 CHECK_FUNCTION("render", plugin_render);
44 return true;
45}
46
47bool game_new(Game* game, int argc, const char** argv) { 34bool game_new(Game* game, int argc, const char** argv) {
48 assert(game); 35 assert(game);
49 36
@@ -76,9 +63,6 @@ bool game_new(Game* game, int argc, const char** argv) {
76 if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) { 63 if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) {
77 goto cleanup; 64 goto cleanup;
78 } 65 }
79 if (!validate_plugin(game->plugin)) {
80 goto cleanup;
81 }
82 66
83 if (!(game->gfx = gfx_init())) { 67 if (!(game->gfx = gfx_init())) {
84 goto cleanup; 68 goto cleanup;
@@ -90,16 +74,21 @@ bool game_new(Game* game, int argc, const char** argv) {
90 goto cleanup; 74 goto cleanup;
91 } 75 }
92 76
93 void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game); 77 if (plugin_resolve(game->plugin, plugin_init, "init")) {
94 if (!plugin_state) { 78 void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game);
95 goto cleanup; 79 if (!plugin_state) {
80 goto cleanup;
81 }
82 set_plugin_state(game->plugin, plugin_state);
96 } 83 }
97 set_plugin_state(game->plugin, plugin_state);
98 84
99 bool boot_success = 85 if (plugin_resolve(game->plugin, plugin_boot, "boot")) {
100 plugin_call(game->plugin, plugin_boot, "boot", plugin_state, game); 86 void* plugin_state = get_plugin_state(game->plugin);
101 if (!boot_success) { 87 bool boot_success =
102 goto cleanup; 88 plugin_call(game->plugin, plugin_boot, "boot", plugin_state, game);
89 if (!boot_success) {
90 goto cleanup;
91 }
103 } 92 }
104 93
105 return true; 94 return true;
@@ -125,15 +114,19 @@ void game_end(Game* game) {
125 114
126void game_update(Game* game, double t, double dt) { 115void game_update(Game* game, double t, double dt) {
127 plugin_engine_update(game->plugin_engine); 116 plugin_engine_update(game->plugin_engine);
128 if (plugin_reloaded(game->plugin)) { 117 if (plugin_reloaded(game->plugin) &&
118 plugin_resolve(game->plugin, plugin_init, "init")) {
129 void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game); 119 void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game);
130 assert(plugin_state); // TODO: handle error better. 120 assert(plugin_state); // TODO: handle error better.
131 set_plugin_state(game->plugin, plugin_state); 121 set_plugin_state(game->plugin, plugin_state);
132 } 122 }
133 123
134 void* plugin_state = get_plugin_state(game->plugin); 124 if (plugin_resolve(game->plugin, plugin_update, "update")) {
135 assert(plugin_state); 125 // Plugin state may be null if plugin does not expose init().
136 plugin_call(game->plugin, plugin_update, "update", plugin_state, game, t, dt); 126 void* plugin_state = get_plugin_state(game->plugin);
127 plugin_call(
128 game->plugin, plugin_update, "update", plugin_state, game, t, dt);
129 }
137} 130}
138 131
139void game_render(const Game* game) { 132void game_render(const Game* game) {
@@ -144,9 +137,11 @@ void game_render(const Game* game) {
144 &(RenderSceneParams){ 137 &(RenderSceneParams){
145 .mode = RenderDefault, .scene = game->scene, .camera = game->camera}); 138 .mode = RenderDefault, .scene = game->scene, .camera = game->camera});
146 139
147 void* plugin_state = get_plugin_state(game->plugin); 140 if (plugin_resolve(game->plugin, plugin_render, "render")) {
148 assert(plugin_state); 141 // Plugin state may be null if plugin does not expose init().
149 plugin_call(game->plugin, plugin_render, "render", plugin_state, game); 142 void* plugin_state = get_plugin_state(game->plugin);
143 plugin_call(game->plugin, plugin_render, "render", plugin_state, game);
144 }
150} 145}
151 146
152void game_set_viewport(Game* game, int width, int height) { 147void game_set_viewport(Game* game, int width, int height) {