From adb89aaca8c82149b23cc8bde63dc2c5f703c4e4 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 22 Aug 2025 07:59:12 -0700 Subject: Fix issue with global GfxApp instance across plugins --- app/src/app.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'app/src/app.c') diff --git a/app/src/app.c b/app/src/app.c index e1edf88..a7bb9c9 100644 --- a/app/src/app.c +++ b/app/src/app.c @@ -24,7 +24,7 @@ static GfxApp g_gfx_app; /// Called by GLFW when the window is resized. static void on_resize(GLFWwindow* window, int width, int height) { - (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, width, height); + (*g_gfx_app.callbacks.resize)(&g_gfx_app, g_gfx_app.app_state, width, height); } /// Run the application's main loop. @@ -41,12 +41,13 @@ static void loop(GfxApp* app) { // Warm up the update to initialize the application's state. (*app->callbacks.update)( - app->app_state, time_delta_to_sec(time), time_delta_to_sec(update_dt)); + app, app->app_state, time_delta_to_sec(time), + time_delta_to_sec(update_dt)); // Warm up the rendering before entering the main loop. A renderer can // compile shaders and do other initialization the first time it renders a // scene. - (*app->callbacks.render)(app->app_state); + (*app->callbacks.render)(app, app->app_state); glfwSwapBuffers(app->window); timer_start(&timer); @@ -56,14 +57,14 @@ static void loop(GfxApp* app) { while (time_budget >= update_dt) { (*app->callbacks.update)( - app->app_state, time_delta_to_sec(time), + app, app->app_state, time_delta_to_sec(time), time_delta_to_sec(update_dt)); time += update_dt; time_budget -= update_dt; } - (*app->callbacks.render)(app->app_state); + (*app->callbacks.render)(app, app->app_state); glfwSwapBuffers(app->window); glfwPollEvents(); @@ -85,7 +86,7 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { g_gfx_app.callbacks = *callbacks; g_gfx_app.max_fps = desc->max_fps; g_gfx_app.update_delta_time = desc->update_delta_time; - g_gfx_app.window = 0; + g_gfx_app.window = nullptr; if (!glfwInit()) { LOGE("glfwInit() failed"); @@ -124,20 +125,21 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { // Initialize the application's state before setting any callbacks. if (!(*g_gfx_app.callbacks.init)( - g_gfx_app.app_state, desc->argc, desc->argv)) { + &g_gfx_app, g_gfx_app.app_state, desc->argc, desc->argv)) { LOGE("Failed to initialize application"); goto cleanup; } // Trigger an initial resize for convenience. - (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, desc->width, desc->height); + (*g_gfx_app.callbacks.resize)( + &g_gfx_app, g_gfx_app.app_state, desc->width, desc->height); // Set GLFW callbacks now that the application has been initialized. glfwSetWindowSizeCallback(g_gfx_app.window, on_resize); loop(&g_gfx_app); - (*g_gfx_app.callbacks.shutdown)(g_gfx_app.app_state); + (*g_gfx_app.callbacks.shutdown)(&g_gfx_app, g_gfx_app.app_state); success = true; @@ -149,21 +151,21 @@ cleanup: return success; } -void gfx_app_get_mouse_position(double* x, double* y) { - glfwGetCursorPos(g_gfx_app.window, x, y); +void gfx_app_get_mouse_position(GfxApp* app, double* x, double* y) { + glfwGetCursorPos(app->window, x, y); } static int to_glfw_mouse_button(MouseButton button); -bool gfx_app_is_mouse_button_pressed(MouseButton button) { - return glfwGetMouseButton(g_gfx_app.window, to_glfw_mouse_button(button)) == +bool gfx_app_is_mouse_button_pressed(GfxApp* app, MouseButton button) { + return glfwGetMouseButton(app->window, to_glfw_mouse_button(button)) == GLFW_PRESS; } static int to_glfw_key(Key key); -bool gfx_app_is_key_pressed(Key key) { - return glfwGetKey(g_gfx_app.window, to_glfw_key(key)) == GLFW_PRESS; +bool gfx_app_is_key_pressed(GfxApp* app, Key key) { + return glfwGetKey(app->window, to_glfw_key(key)) == GLFW_PRESS; } static int to_glfw_mouse_button(MouseButton button) { -- cgit v1.2.3