From 4b340ab0db3898b36a7e975690359eef3747284d Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 22 Aug 2025 07:59:47 -0700 Subject: Fix issue with global GfxApp instance across plugins --- src/game.c | 62 ++++++++++++++++++++++++++++++++++------------------ src/game.h | 2 ++ src/plugins/plugin.h | 17 +++++--------- src/plugins/viewer.c | 19 +++++++++------- 4 files changed, 60 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/game.c b/src/game.c index 51f5cbe..cb45b3e 100644 --- a/src/game.c +++ b/src/game.c @@ -44,9 +44,9 @@ static bool init_plugin(Game* 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")) { + if (plugin_resolve(game->plugin, PluginInit, "init")) { State* plugin_state = 0; - if (!plugin_call(game->plugin, plugin_init, "init", game, &plugin_state)) { + if (!plugin_call(game->plugin, PluginInit, "init", game, &plugin_state)) { return false; } set_plugin_state(game->plugin, plugin_state); @@ -59,9 +59,9 @@ static bool init_plugin(Game* game) { static void shutdown_plugin(Game* game) { assert(game); if (game->plugin && - (plugin_resolve(game->plugin, plugin_shutdown, "shutdown"))) { + (plugin_resolve(game->plugin, PluginShutdown, "shutdown"))) { void* plugin_state = get_plugin_state(game->plugin); - plugin_call(game->plugin, plugin_shutdown, "shutdown", game, plugin_state); + plugin_call(game->plugin, PluginShutdown, "shutdown", game, plugin_state); set_plugin_state(game->plugin, 0); } } @@ -70,9 +70,9 @@ static void shutdown_plugin(Game* game) { static bool boot_plugin(Game* game) { assert(game); assert(game->plugin); - if (plugin_resolve(game->plugin, plugin_boot, "boot")) { + if (plugin_resolve(game->plugin, PluginBoot, "boot")) { void* plugin_state = get_plugin_state(game->plugin); - return plugin_call(game->plugin, plugin_boot, "boot", game, plugin_state); + return plugin_call(game->plugin, PluginBoot, "boot", game, plugin_state); } return true; // Plugin does not need to expose a boot(). } @@ -81,10 +81,10 @@ static bool boot_plugin(Game* game) { static void update_plugin(Game* game, double t, double dt) { assert(game); assert(game->plugin); - if (plugin_resolve(game->plugin, plugin_update, "update")) { + if (plugin_resolve(game->plugin, PluginUpdate, "update")) { void* plugin_state = get_plugin_state(game->plugin); plugin_call( - game->plugin, plugin_update, "update", game, plugin_state, t, dt); + game->plugin, PluginUpdate, "update", game, plugin_state, t, dt); } } @@ -92,9 +92,9 @@ static void update_plugin(Game* game, double t, double dt) { static void render_plugin(const Game* game) { assert(game); assert(game->plugin); - if (plugin_resolve(game->plugin, plugin_render, "render")) { + if (plugin_resolve(game->plugin, PluginRender, "render")) { void* plugin_state = get_plugin_state(game->plugin); - plugin_call(game->plugin, plugin_render, "render", game, plugin_state); + plugin_call(game->plugin, PluginRender, "render", game, plugin_state); } } @@ -102,24 +102,29 @@ static void render_plugin(const Game* game) { static void resize_plugin(Game* game, int width, int height) { assert(game); assert(game->plugin); - if (plugin_resolve(game->plugin, plugin_resize, "resize")) { + if (plugin_resolve(game->plugin, PluginResize, "resize")) { void* plugin_state = get_plugin_state(game->plugin); plugin_call( - game->plugin, plugin_resize, "resize", game, plugin_state, width, + game->plugin, PluginResize, "resize", game, plugin_state, width, height); } } -static void Shutdown(Game* game); +static void Shutdown(GfxApp* app, GfxAppState* app_state); -static bool Init(Game* game, int argc, const char** argv) { - assert(game); +static bool Init( + GfxApp* app, GfxAppState* app_state, int argc, const char** argv) { + assert(app); + assert(app_state); if (argc <= 1) { LOGE("Usage: %s [plugin args]", argv[0]); return false; } + Game* game = &app_state->game; + game->app = app; + // Syntax: game [plugin args] // // Here we consume the arg so that plugins receive the remainder @@ -165,12 +170,15 @@ static bool Init(Game* game, int argc, const char** argv) { cleanup: LOGE("Gfx error: %s", get_error()); - Shutdown(game); + Shutdown(app, app_state); return false; } -static void Shutdown(Game* game) { - assert(game); +static void Shutdown(GfxApp* app, GfxAppState* app_state) { + assert(app); + assert(app_state); + Game* game = &app_state->game; + shutdown_plugin(game); if (game->gfx) { gfx_destroy(&game->gfx); @@ -183,7 +191,11 @@ static void Shutdown(Game* game) { } } -static void Update(Game* game, double t, double dt) { +static void Update(GfxApp* app, GfxAppState* app_state, double t, double dt) { + assert(app); + assert(app_state); + Game* game = &app_state->game; + plugin_engine_update(game->plugin_engine); if (plugin_reloaded(game->plugin)) { shutdown_plugin(game); @@ -198,14 +210,22 @@ static void Update(Game* game, double t, double dt) { update_plugin(game, t, dt); } -static void Render(const Game* game) { +static void Render(const GfxApp* app, GfxAppState* app_state) { + assert(app); + assert(app_state); + Game* game = &app_state->game; + GfxCore* gfxcore = gfx_get_core(game->gfx); gfx_start_frame(gfxcore); render_plugin(game); gfx_end_frame(gfxcore); } -static void Resize(Game* game, int width, int height) { +static void Resize(GfxApp* app, GfxAppState* app_state, int width, int height) { + assert(app); + assert(app_state); + Game* game = &app_state->game; + game->width = width; game->height = height; diff --git a/src/game.h b/src/game.h index 579ba3c..4589496 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ typedef struct PluginEngine PluginEngine; typedef struct Plugin Plugin; typedef struct Gfx Gfx; +typedef struct GfxApp GfxApp; typedef struct Scene Scene; typedef struct SceneCamera SceneCamera; @@ -15,6 +16,7 @@ typedef struct { const char** argv; PluginEngine* plugin_engine; Plugin* plugin; + GfxApp* app; Gfx* gfx; int width; int height; diff --git a/src/plugins/plugin.h b/src/plugins/plugin.h index f7219c6..2831045 100644 --- a/src/plugins/plugin.h +++ b/src/plugins/plugin.h @@ -5,11 +5,6 @@ #include "../game.h" -#include -#include - -#include - typedef struct State State; /// Initialize the plugin, which may optionally return a state object. @@ -44,9 +39,9 @@ void render(const Game*, const State*); void resize(Game*, State*, int width, int height); // Signatures for the plugin's exposed functions. -typedef bool (*plugin_init)(Game*, State**); -typedef bool (*plugin_shutdown)(Game*, State*); -typedef bool (*plugin_boot)(Game*, State*); -typedef void (*plugin_update)(Game*, State*, double t, double dt); -typedef void (*plugin_render)(const Game*, const State*); -typedef void (*plugin_resize)(Game* game, State* state, int width, int height); +typedef bool (*PluginInit)(Game*, State**); +typedef bool (*PluginShutdown)(Game*, State*); +typedef bool (*PluginBoot)(Game*, State*); +typedef void (*PluginUpdate)(Game*, State*, double t, double dt); +typedef void (*PluginRender)(const Game*, const State*); +typedef void (*PluginResize)(Game* game, State* state, int width, int height); diff --git a/src/plugins/viewer.c b/src/plugins/viewer.c index 97c718e..83c7320 100644 --- a/src/plugins/viewer.c +++ b/src/plugins/viewer.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -204,13 +205,13 @@ void shutdown(Game* game, State* state) { } } -static CameraCommand make_camera_command_from_input() { +static CameraCommand make_camera_command_from_input(GfxApp* app) { return (CameraCommand){ - .CameraMoveLeft = gfx_app_is_key_pressed(KeyA), - .CameraMoveRight = gfx_app_is_key_pressed(KeyD), - .CameraMoveForward = gfx_app_is_key_pressed(KeyW), - .CameraMoveBackward = gfx_app_is_key_pressed(KeyS), - .CameraIsRotating = gfx_app_is_mouse_button_pressed(LMB), + .CameraMoveLeft = gfx_app_is_key_pressed(app, KeyA), + .CameraMoveRight = gfx_app_is_key_pressed(app, KeyD), + .CameraMoveForward = gfx_app_is_key_pressed(app, KeyW), + .CameraMoveBackward = gfx_app_is_key_pressed(app, KeyS), + .CameraIsRotating = gfx_app_is_mouse_button_pressed(app, LMB), }; } @@ -248,15 +249,17 @@ static void update_camera( void update(Game* game, State* state, double t, double dt) { assert(game); + assert(game->app); assert(state); assert(state->scene); assert(state->camera); double mouse_x, mouse_y; - gfx_app_get_mouse_position(&mouse_x, &mouse_y); + gfx_app_get_mouse_position(game->app, &mouse_x, &mouse_y); const vec2 mouse_position = {(R)mouse_x, (R)mouse_y}; - const CameraCommand camera_command = make_camera_command_from_input(); + const CameraCommand camera_command = + make_camera_command_from_input(game->app); update_camera( &state->camera_controller, (R)dt, mouse_position, camera_command, &gfx_get_camera_camera(state->camera)->spatial); -- cgit v1.2.3