From be63cdf390b0bdbb00ba67cff95d165d214418fb Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 17 Jun 2023 20:37:26 -0700 Subject: Allow plugins to not have to define all plugin functions. --- gltfview/src/game.c | 57 ++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'gltfview/src/game.c') 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 @@ // Plugin to load if no plugin is provided. static const char* DEFAULT_PLUGIN = "texture_view"; -static bool validate_plugin(const Plugin* plugin) { -#define CHECK_FUNCTION(name, signature) \ - if (!plugin_resolve(plugin, signature, name)) { \ - LOGE("Plugin is missing function: " #name); \ - return false; \ - } - CHECK_FUNCTION("init", plugin_init); - CHECK_FUNCTION("boot", plugin_boot); - CHECK_FUNCTION("update", plugin_update); - CHECK_FUNCTION("render", plugin_render); - return true; -} - bool game_new(Game* game, int argc, const char** argv) { assert(game); @@ -76,9 +63,6 @@ bool game_new(Game* game, int argc, const char** argv) { if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) { goto cleanup; } - if (!validate_plugin(game->plugin)) { - goto cleanup; - } if (!(game->gfx = gfx_init())) { goto cleanup; @@ -90,16 +74,21 @@ bool game_new(Game* game, int argc, const char** argv) { goto cleanup; } - void* plugin_state = plugin_call(game->plugin, plugin_init, "init", game); - if (!plugin_state) { - 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); } - set_plugin_state(game->plugin, plugin_state); - bool boot_success = - plugin_call(game->plugin, plugin_boot, "boot", plugin_state, game); - if (!boot_success) { - goto cleanup; + 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; @@ -125,15 +114,19 @@ void game_end(Game* game) { void game_update(Game* game, double t, double dt) { plugin_engine_update(game->plugin_engine); - if (plugin_reloaded(game->plugin)) { + 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); } - void* plugin_state = get_plugin_state(game->plugin); - assert(plugin_state); - plugin_call(game->plugin, plugin_update, "update", plugin_state, game, t, dt); + 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) { @@ -144,9 +137,11 @@ void game_render(const Game* game) { &(RenderSceneParams){ .mode = RenderDefault, .scene = game->scene, .camera = game->camera}); - void* plugin_state = get_plugin_state(game->plugin); - assert(plugin_state); - plugin_call(game->plugin, plugin_render, "render", plugin_state, game); + 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); + } } void game_set_viewport(Game* game, int width, int height) { -- cgit v1.2.3