aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-22 07:59:12 -0700
committer3gg <3gg@shellblade.net>2025-08-22 07:59:12 -0700
commitadb89aaca8c82149b23cc8bde63dc2c5f703c4e4 (patch)
tree766b6c86f7d31eb340cd31f379d086726f530a82
parent29f5ba7807f0b36f88da02ebee4732d9d9ab739c (diff)
Fix issue with global GfxApp instance across pluginsHEADmain
-rw-r--r--app/include/gfx/app.h30
-rw-r--r--app/src/app.c32
2 files changed, 31 insertions, 31 deletions
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 @@
1#pragma once 1#pragma once
2 2
3#include <stdbool.h> 3typedef struct GfxApp GfxApp;
4
5typedef struct GfxAppState GfxAppState; 4typedef struct GfxAppState GfxAppState;
6 5
7/// Application settings. 6/// Application settings.
@@ -16,11 +15,11 @@ typedef struct GfxAppDesc {
16 GfxAppState* app_state; 15 GfxAppState* app_state;
17} GfxAppDesc; 16} GfxAppDesc;
18 17
19typedef bool (*GfxAppInit)(GfxAppState*, int argc, const char** argv); 18typedef bool (*GfxAppInit)(GfxApp*, GfxAppState*, int argc, const char** argv);
20typedef void (*GfxAppShutdown)(GfxAppState*); 19typedef void (*GfxAppShutdown)(GfxApp*, GfxAppState*);
21typedef void (*GfxAppUpdate)(GfxAppState*, double t, double dt); 20typedef void (*GfxAppUpdate)(GfxApp*, GfxAppState*, double t, double dt);
22typedef void (*GfxAppRender)(GfxAppState*); 21typedef void (*GfxAppRender)(const GfxApp*, GfxAppState*);
23typedef void (*GfxAppResize)(GfxAppState*, int width, int height); 22typedef void (*GfxAppResize)(GfxApp*, GfxAppState*, int width, int height);
24 23
25/// Application callback functions. 24/// Application callback functions.
26typedef struct GfxAppCallbacks { 25typedef struct GfxAppCallbacks {
@@ -74,13 +73,13 @@ extern "C" {
74bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*); 73bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*);
75 74
76/// Get the mouse coordinates relative to the app's window. 75/// Get the mouse coordinates relative to the app's window.
77void gfx_app_get_mouse_position(double* x, double* y); 76void gfx_app_get_mouse_position(GfxApp*, double* x, double* y);
78 77
79/// Return if the given mouse button is pressed. 78/// Return if the given mouse button is pressed.
80bool gfx_app_is_mouse_button_pressed(MouseButton); 79bool gfx_app_is_mouse_button_pressed(GfxApp*, MouseButton);
81 80
82/// Return true if the given key is pressed. 81/// Return true if the given key is pressed.
83bool gfx_app_is_key_pressed(Key); 82bool gfx_app_is_key_pressed(GfxApp*, Key);
84 83
85#ifdef __cplusplus 84#ifdef __cplusplus
86} // extern "C" 85} // extern "C"
@@ -102,11 +101,10 @@ bool gfx_app_is_key_pressed(Key);
102 .title = TITLE, \ 101 .title = TITLE, \
103 .app_state = &app_state, \ 102 .app_state = &app_state, \
104 }, \ 103 }, \
105 &(GfxAppCallbacks){ \ 104 &(GfxAppCallbacks){.init = Init, \
106 .init = (GfxAppInit)Init, \ 105 .shutdown = Shutdown, \
107 .shutdown = (GfxAppShutdown)Shutdown, \ 106 .update = Update, \
108 .update = (GfxAppUpdate)Update, \ 107 .render = Render, \
109 .render = (GfxAppRender)Render, \ 108 .resize = Resize}); \
110 .resize = (GfxAppResize)Resize}); \
111 return 0; \ 109 return 0; \
112 } 110 }
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;
24 24
25/// Called by GLFW when the window is resized. 25/// Called by GLFW when the window is resized.
26static void on_resize(GLFWwindow* window, int width, int height) { 26static void on_resize(GLFWwindow* window, int width, int height) {
27 (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, width, height); 27 (*g_gfx_app.callbacks.resize)(&g_gfx_app, g_gfx_app.app_state, width, height);
28} 28}
29 29
30/// Run the application's main loop. 30/// Run the application's main loop.
@@ -41,12 +41,13 @@ static void loop(GfxApp* app) {
41 41
42 // Warm up the update to initialize the application's state. 42 // Warm up the update to initialize the application's state.
43 (*app->callbacks.update)( 43 (*app->callbacks.update)(
44 app->app_state, time_delta_to_sec(time), time_delta_to_sec(update_dt)); 44 app, app->app_state, time_delta_to_sec(time),
45 time_delta_to_sec(update_dt));
45 46
46 // Warm up the rendering before entering the main loop. A renderer can 47 // Warm up the rendering before entering the main loop. A renderer can
47 // compile shaders and do other initialization the first time it renders a 48 // compile shaders and do other initialization the first time it renders a
48 // scene. 49 // scene.
49 (*app->callbacks.render)(app->app_state); 50 (*app->callbacks.render)(app, app->app_state);
50 glfwSwapBuffers(app->window); 51 glfwSwapBuffers(app->window);
51 52
52 timer_start(&timer); 53 timer_start(&timer);
@@ -56,14 +57,14 @@ static void loop(GfxApp* app) {
56 57
57 while (time_budget >= update_dt) { 58 while (time_budget >= update_dt) {
58 (*app->callbacks.update)( 59 (*app->callbacks.update)(
59 app->app_state, time_delta_to_sec(time), 60 app, app->app_state, time_delta_to_sec(time),
60 time_delta_to_sec(update_dt)); 61 time_delta_to_sec(update_dt));
61 62
62 time += update_dt; 63 time += update_dt;
63 time_budget -= update_dt; 64 time_budget -= update_dt;
64 } 65 }
65 66
66 (*app->callbacks.render)(app->app_state); 67 (*app->callbacks.render)(app, app->app_state);
67 glfwSwapBuffers(app->window); 68 glfwSwapBuffers(app->window);
68 glfwPollEvents(); 69 glfwPollEvents();
69 70
@@ -85,7 +86,7 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) {
85 g_gfx_app.callbacks = *callbacks; 86 g_gfx_app.callbacks = *callbacks;
86 g_gfx_app.max_fps = desc->max_fps; 87 g_gfx_app.max_fps = desc->max_fps;
87 g_gfx_app.update_delta_time = desc->update_delta_time; 88 g_gfx_app.update_delta_time = desc->update_delta_time;
88 g_gfx_app.window = 0; 89 g_gfx_app.window = nullptr;
89 90
90 if (!glfwInit()) { 91 if (!glfwInit()) {
91 LOGE("glfwInit() failed"); 92 LOGE("glfwInit() failed");
@@ -124,20 +125,21 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) {
124 125
125 // Initialize the application's state before setting any callbacks. 126 // Initialize the application's state before setting any callbacks.
126 if (!(*g_gfx_app.callbacks.init)( 127 if (!(*g_gfx_app.callbacks.init)(
127 g_gfx_app.app_state, desc->argc, desc->argv)) { 128 &g_gfx_app, g_gfx_app.app_state, desc->argc, desc->argv)) {
128 LOGE("Failed to initialize application"); 129 LOGE("Failed to initialize application");
129 goto cleanup; 130 goto cleanup;
130 } 131 }
131 132
132 // Trigger an initial resize for convenience. 133 // Trigger an initial resize for convenience.
133 (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, desc->width, desc->height); 134 (*g_gfx_app.callbacks.resize)(
135 &g_gfx_app, g_gfx_app.app_state, desc->width, desc->height);
134 136
135 // Set GLFW callbacks now that the application has been initialized. 137 // Set GLFW callbacks now that the application has been initialized.
136 glfwSetWindowSizeCallback(g_gfx_app.window, on_resize); 138 glfwSetWindowSizeCallback(g_gfx_app.window, on_resize);
137 139
138 loop(&g_gfx_app); 140 loop(&g_gfx_app);
139 141
140 (*g_gfx_app.callbacks.shutdown)(g_gfx_app.app_state); 142 (*g_gfx_app.callbacks.shutdown)(&g_gfx_app, g_gfx_app.app_state);
141 143
142 success = true; 144 success = true;
143 145
@@ -149,21 +151,21 @@ cleanup:
149 return success; 151 return success;
150} 152}
151 153
152void gfx_app_get_mouse_position(double* x, double* y) { 154void gfx_app_get_mouse_position(GfxApp* app, double* x, double* y) {
153 glfwGetCursorPos(g_gfx_app.window, x, y); 155 glfwGetCursorPos(app->window, x, y);
154} 156}
155 157
156static int to_glfw_mouse_button(MouseButton button); 158static int to_glfw_mouse_button(MouseButton button);
157 159
158bool gfx_app_is_mouse_button_pressed(MouseButton button) { 160bool gfx_app_is_mouse_button_pressed(GfxApp* app, MouseButton button) {
159 return glfwGetMouseButton(g_gfx_app.window, to_glfw_mouse_button(button)) == 161 return glfwGetMouseButton(app->window, to_glfw_mouse_button(button)) ==
160 GLFW_PRESS; 162 GLFW_PRESS;
161} 163}
162 164
163static int to_glfw_key(Key key); 165static int to_glfw_key(Key key);
164 166
165bool gfx_app_is_key_pressed(Key key) { 167bool gfx_app_is_key_pressed(GfxApp* app, Key key) {
166 return glfwGetKey(g_gfx_app.window, to_glfw_key(key)) == GLFW_PRESS; 168 return glfwGetKey(app->window, to_glfw_key(key)) == GLFW_PRESS;
167} 169}
168 170
169static int to_glfw_mouse_button(MouseButton button) { 171static int to_glfw_mouse_button(MouseButton button) {