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 +++++++++++++++++-------------------- gltfview/src/plugins/texture_view.c | 22 +------------- gltfview/src/plugins/texture_view.h | 6 ---- 3 files changed, 27 insertions(+), 58 deletions(-) (limited to 'gltfview/src') 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) { diff --git a/gltfview/src/plugins/texture_view.c b/gltfview/src/plugins/texture_view.c index f2c650f..2bca4a1 100644 --- a/gltfview/src/plugins/texture_view.c +++ b/gltfview/src/plugins/texture_view.c @@ -1,6 +1,7 @@ #include "texture_view.h" #include +#include #include #include #include @@ -13,15 +14,7 @@ // Default texture to load if no texture is provided. static const char* CLOUDS1_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; -State* init(Game* game) { - assert(game); - - State* state = calloc(1, sizeof(State)); - return state; -} - bool boot(State* state, Game* game) { - assert(state); assert(game); // Usage: [texture file] @@ -37,9 +30,6 @@ bool boot(State* state, Game* game) { .mipmaps = false, .data.texture.filepath = mstring_make(texture_file)}); - Camera* camera = gfx_get_camera_camera(game->camera); - spatial3_set_position(&camera->spatial, vec3_make(0, 0, 1)); - ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); if (!shader) { return false; @@ -82,13 +72,3 @@ bool boot(State* state, Game* game) { return true; } - -void update(State* state, Game* game, double t, double dt) { - assert(state); - assert(game); -} - -void render(State* state, const Game* game) { - assert(state); - assert(game); -} diff --git a/gltfview/src/plugins/texture_view.h b/gltfview/src/plugins/texture_view.h index 670d88d..956f34a 100644 --- a/gltfview/src/plugins/texture_view.h +++ b/gltfview/src/plugins/texture_view.h @@ -1,9 +1,3 @@ #pragma once #include "plugin.h" - -#include - -typedef struct State { - int unused; -} State; -- cgit v1.2.3