diff options
| author | 3gg <3gg@shellblade.net> | 2023-06-17 20:37:26 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2023-06-17 20:37:26 -0700 |
| commit | be63cdf390b0bdbb00ba67cff95d165d214418fb (patch) | |
| tree | f730f79bf6f04a44cb7489e3d9fd6b50f4e3b716 | |
| parent | 9d1eb251b5d26759e17dd7bee316cf27fdcb28bf (diff) | |
Allow plugins to not have to define all plugin functions.
| -rw-r--r-- | gltfview/src/game.c | 57 | ||||
| -rw-r--r-- | gltfview/src/plugins/texture_view.c | 22 | ||||
| -rw-r--r-- | gltfview/src/plugins/texture_view.h | 6 |
3 files changed, 27 insertions, 58 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. |
| 32 | static const char* DEFAULT_PLUGIN = "texture_view"; | 32 | static const char* DEFAULT_PLUGIN = "texture_view"; |
| 33 | 33 | ||
| 34 | static 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 | |||
| 47 | bool game_new(Game* game, int argc, const char** argv) { | 34 | bool 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 | ||
| 126 | void game_update(Game* game, double t, double dt) { | 115 | void 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 | ||
| 139 | void game_render(const Game* game) { | 132 | void 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 | ||
| 152 | void game_set_viewport(Game* game, int width, int height) { | 147 | 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 @@ | |||
| 1 | #include "texture_view.h" | 1 | #include "texture_view.h" |
| 2 | 2 | ||
| 3 | #include <gfx/render_backend.h> | 3 | #include <gfx/render_backend.h> |
| 4 | #include <gfx/scene.h> | ||
| 4 | #include <gfx/util/geometry.h> | 5 | #include <gfx/util/geometry.h> |
| 5 | #include <gfx/util/shader.h> | 6 | #include <gfx/util/shader.h> |
| 6 | #include <gfx/util/texture.h> | 7 | #include <gfx/util/texture.h> |
| @@ -13,15 +14,7 @@ | |||
| 13 | // Default texture to load if no texture is provided. | 14 | // Default texture to load if no texture is provided. |
| 14 | static const char* CLOUDS1_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; | 15 | static const char* CLOUDS1_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; |
| 15 | 16 | ||
| 16 | State* init(Game* game) { | ||
| 17 | assert(game); | ||
| 18 | |||
| 19 | State* state = calloc(1, sizeof(State)); | ||
| 20 | return state; | ||
| 21 | } | ||
| 22 | |||
| 23 | bool boot(State* state, Game* game) { | 17 | bool boot(State* state, Game* game) { |
| 24 | assert(state); | ||
| 25 | assert(game); | 18 | assert(game); |
| 26 | 19 | ||
| 27 | // Usage: [texture file] | 20 | // Usage: [texture file] |
| @@ -37,9 +30,6 @@ bool boot(State* state, Game* game) { | |||
| 37 | .mipmaps = false, | 30 | .mipmaps = false, |
| 38 | .data.texture.filepath = mstring_make(texture_file)}); | 31 | .data.texture.filepath = mstring_make(texture_file)}); |
| 39 | 32 | ||
| 40 | Camera* camera = gfx_get_camera_camera(game->camera); | ||
| 41 | spatial3_set_position(&camera->spatial, vec3_make(0, 0, 1)); | ||
| 42 | |||
| 43 | ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); | 33 | ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); |
| 44 | if (!shader) { | 34 | if (!shader) { |
| 45 | return false; | 35 | return false; |
| @@ -82,13 +72,3 @@ bool boot(State* state, Game* game) { | |||
| 82 | 72 | ||
| 83 | return true; | 73 | return true; |
| 84 | } | 74 | } |
| 85 | |||
| 86 | void update(State* state, Game* game, double t, double dt) { | ||
| 87 | assert(state); | ||
| 88 | assert(game); | ||
| 89 | } | ||
| 90 | |||
| 91 | void render(State* state, const Game* game) { | ||
| 92 | assert(state); | ||
| 93 | assert(game); | ||
| 94 | } | ||
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 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "plugin.h" | 3 | #include "plugin.h" |
| 4 | |||
| 5 | #include <gfx/scene.h> | ||
| 6 | |||
| 7 | typedef struct State { | ||
| 8 | int unused; | ||
| 9 | } State; | ||
