From 4212e57d06afac8a19b09fdebc24bad10b78f1ac Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 19 Jan 2024 07:21:45 -0800 Subject: Plugin refactor, moving scene from game to plugin. --- gltfview/src/game.c | 138 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 55 deletions(-) (limited to 'gltfview/src/game.c') diff --git a/gltfview/src/game.c b/gltfview/src/game.c index c331190..64be4f3 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c @@ -39,6 +39,78 @@ static const int WIDTH = 1350; static const int HEIGHT = 900; static const int MAX_FPS = 60; +/// Initialize the game's plugin. +static bool init_plugin(Game* game) { + assert(game); + assert(game->plugin); + // Plugin state is allowed to be null, either when the plugin does not + // expose an init() or when init() does not initialize a state. + if (plugin_resolve(game->plugin, plugin_init, "init")) { + State* plugin_state = 0; + if (!plugin_call(game->plugin, plugin_init, "init", game, &plugin_state)) { + return false; + } + set_plugin_state(game->plugin, plugin_state); + } + return true; // Plugin does not need to expose an init(). +} + +/// Shutdown the game's plugin. +/// The game's plugin is allowed to be null in the call to this function. +static void shutdown_plugin(Game* game) { + assert(game); + if (game->plugin && + (plugin_resolve(game->plugin, plugin_shutdown, "shutdown"))) { + void* plugin_state = get_plugin_state(game->plugin); + plugin_call(game->plugin, plugin_shutdown, "shutdown", game, plugin_state); + set_plugin_state(game->plugin, 0); + } +} + +/// Boot the game's plugin. +static bool boot_plugin(Game* game) { + assert(game); + assert(game->plugin); + if (plugin_resolve(game->plugin, plugin_boot, "boot")) { + void* plugin_state = get_plugin_state(game->plugin); + return plugin_call(game->plugin, plugin_boot, "boot", game, plugin_state); + } + return true; // Plugin does not need to expose a boot(). +} + +/// Update the plugin's state. +static void update_plugin(Game* game, double t, double dt) { + assert(game); + assert(game->plugin); + if (plugin_resolve(game->plugin, plugin_update, "update")) { + void* plugin_state = get_plugin_state(game->plugin); + plugin_call( + game->plugin, plugin_update, "update", game, plugin_state, t, dt); + } +} + +/// Plugin render. +static void render_plugin(const Game* game) { + assert(game); + assert(game->plugin); + if (plugin_resolve(game->plugin, plugin_render, "render")) { + void* plugin_state = get_plugin_state(game->plugin); + plugin_call(game->plugin, plugin_render, "render", game, plugin_state); + } +} + +/// Plugin resize. +static void resize_plugin(Game* game, int width, int height) { + assert(game); + assert(game->plugin); + if (plugin_resolve(game->plugin, plugin_resize, "resize")) { + void* plugin_state = get_plugin_state(game->plugin); + plugin_call( + game->plugin, plugin_resize, "resize", game, plugin_state, width, + height); + } +} + void app_end(Game* game); bool app_init(const GfxAppDesc* desc, void** app_state) { @@ -88,30 +160,14 @@ bool app_init(const GfxAppDesc* desc, void** app_state) { if (!(game->gfx = gfx_init())) { goto cleanup; } - if (!(game->scene = gfx_make_scene())) { + + if (!init_plugin(game)) { goto cleanup; } - if (!(game->camera = gfx_make_camera())) { + if (!boot_plugin(game)) { 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; @@ -123,6 +179,7 @@ cleanup: void app_end(Game* game) { assert(game); + shutdown_plugin(game); if (game->gfx) { gfx_destroy(&game->gfx); } @@ -136,53 +193,24 @@ void app_end(Game* game) { 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_reloaded(game->plugin)) { + shutdown_plugin(game); + const bool result = init_plugin(game); + assert(result); // TODO: handle error better. } - 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); - } + update_plugin(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); - } - + render_plugin(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; + resize_plugin(game, width, height); } GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS); -- cgit v1.2.3