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 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 21 deletions(-) (limited to 'src/game.c') 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; -- cgit v1.2.3