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/include/gfx/app.h | 30 ++++++++++++++---------------- app/src/app.c | 32 +++++++++++++++++--------------- 2 files changed, 31 insertions(+), 31 deletions(-) (limited to 'app') diff --git a/app/include/gfx/app.h b/app/include/gfx/app.h index 77b6ad2..639fa2d 100644 --- a/app/include/gfx/app.h +++ b/app/include/gfx/app.h @@ -1,7 +1,6 @@ #pragma once -#include - +typedef struct GfxApp GfxApp; typedef struct GfxAppState GfxAppState; /// Application settings. @@ -16,11 +15,11 @@ typedef struct GfxAppDesc { GfxAppState* app_state; } GfxAppDesc; -typedef bool (*GfxAppInit)(GfxAppState*, int argc, const char** argv); -typedef void (*GfxAppShutdown)(GfxAppState*); -typedef void (*GfxAppUpdate)(GfxAppState*, double t, double dt); -typedef void (*GfxAppRender)(GfxAppState*); -typedef void (*GfxAppResize)(GfxAppState*, int width, int height); +typedef bool (*GfxAppInit)(GfxApp*, GfxAppState*, int argc, const char** argv); +typedef void (*GfxAppShutdown)(GfxApp*, GfxAppState*); +typedef void (*GfxAppUpdate)(GfxApp*, GfxAppState*, double t, double dt); +typedef void (*GfxAppRender)(const GfxApp*, GfxAppState*); +typedef void (*GfxAppResize)(GfxApp*, GfxAppState*, int width, int height); /// Application callback functions. typedef struct GfxAppCallbacks { @@ -74,13 +73,13 @@ extern "C" { bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*); /// Get the mouse coordinates relative to the app's window. -void gfx_app_get_mouse_position(double* x, double* y); +void gfx_app_get_mouse_position(GfxApp*, double* x, double* y); /// Return if the given mouse button is pressed. -bool gfx_app_is_mouse_button_pressed(MouseButton); +bool gfx_app_is_mouse_button_pressed(GfxApp*, MouseButton); /// Return true if the given key is pressed. -bool gfx_app_is_key_pressed(Key); +bool gfx_app_is_key_pressed(GfxApp*, Key); #ifdef __cplusplus } // extern "C" @@ -102,11 +101,10 @@ bool gfx_app_is_key_pressed(Key); .title = TITLE, \ .app_state = &app_state, \ }, \ - &(GfxAppCallbacks){ \ - .init = (GfxAppInit)Init, \ - .shutdown = (GfxAppShutdown)Shutdown, \ - .update = (GfxAppUpdate)Update, \ - .render = (GfxAppRender)Render, \ - .resize = (GfxAppResize)Resize}); \ + &(GfxAppCallbacks){.init = Init, \ + .shutdown = Shutdown, \ + .update = Update, \ + .render = Render, \ + .resize = Resize}); \ return 0; \ } 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