summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-22 07:59:47 -0700
committer3gg <3gg@shellblade.net>2025-08-22 07:59:47 -0700
commit4b340ab0db3898b36a7e975690359eef3747284d (patch)
tree943f31cd133b9c815992bef7af072bdc90af7bb7 /src
parenta82c2bbf3f80697b38706d288deecd100ef95a61 (diff)
Fix issue with global GfxApp instance across pluginsHEADmain
Diffstat (limited to 'src')
-rw-r--r--src/game.c62
-rw-r--r--src/game.h2
-rw-r--r--src/plugins/plugin.h17
-rw-r--r--src/plugins/viewer.c19
4 files changed, 60 insertions, 40 deletions
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) {
44 assert(game->plugin); 44 assert(game->plugin);
45 // Plugin state is allowed to be null, either when the plugin does not 45 // Plugin state is allowed to be null, either when the plugin does not
46 // expose an init() or when init() does not initialize a state. 46 // expose an init() or when init() does not initialize a state.
47 if (plugin_resolve(game->plugin, plugin_init, "init")) { 47 if (plugin_resolve(game->plugin, PluginInit, "init")) {
48 State* plugin_state = 0; 48 State* plugin_state = 0;
49 if (!plugin_call(game->plugin, plugin_init, "init", game, &plugin_state)) { 49 if (!plugin_call(game->plugin, PluginInit, "init", game, &plugin_state)) {
50 return false; 50 return false;
51 } 51 }
52 set_plugin_state(game->plugin, plugin_state); 52 set_plugin_state(game->plugin, plugin_state);
@@ -59,9 +59,9 @@ static bool init_plugin(Game* game) {
59static void shutdown_plugin(Game* game) { 59static void shutdown_plugin(Game* game) {
60 assert(game); 60 assert(game);
61 if (game->plugin && 61 if (game->plugin &&
62 (plugin_resolve(game->plugin, plugin_shutdown, "shutdown"))) { 62 (plugin_resolve(game->plugin, PluginShutdown, "shutdown"))) {
63 void* plugin_state = get_plugin_state(game->plugin); 63 void* plugin_state = get_plugin_state(game->plugin);
64 plugin_call(game->plugin, plugin_shutdown, "shutdown", game, plugin_state); 64 plugin_call(game->plugin, PluginShutdown, "shutdown", game, plugin_state);
65 set_plugin_state(game->plugin, 0); 65 set_plugin_state(game->plugin, 0);
66 } 66 }
67} 67}
@@ -70,9 +70,9 @@ static void shutdown_plugin(Game* game) {
70static bool boot_plugin(Game* game) { 70static bool boot_plugin(Game* game) {
71 assert(game); 71 assert(game);
72 assert(game->plugin); 72 assert(game->plugin);
73 if (plugin_resolve(game->plugin, plugin_boot, "boot")) { 73 if (plugin_resolve(game->plugin, PluginBoot, "boot")) {
74 void* plugin_state = get_plugin_state(game->plugin); 74 void* plugin_state = get_plugin_state(game->plugin);
75 return plugin_call(game->plugin, plugin_boot, "boot", game, plugin_state); 75 return plugin_call(game->plugin, PluginBoot, "boot", game, plugin_state);
76 } 76 }
77 return true; // Plugin does not need to expose a boot(). 77 return true; // Plugin does not need to expose a boot().
78} 78}
@@ -81,10 +81,10 @@ static bool boot_plugin(Game* game) {
81static void update_plugin(Game* game, double t, double dt) { 81static void update_plugin(Game* game, double t, double dt) {
82 assert(game); 82 assert(game);
83 assert(game->plugin); 83 assert(game->plugin);
84 if (plugin_resolve(game->plugin, plugin_update, "update")) { 84 if (plugin_resolve(game->plugin, PluginUpdate, "update")) {
85 void* plugin_state = get_plugin_state(game->plugin); 85 void* plugin_state = get_plugin_state(game->plugin);
86 plugin_call( 86 plugin_call(
87 game->plugin, plugin_update, "update", game, plugin_state, t, dt); 87 game->plugin, PluginUpdate, "update", game, plugin_state, t, dt);
88 } 88 }
89} 89}
90 90
@@ -92,9 +92,9 @@ static void update_plugin(Game* game, double t, double dt) {
92static void render_plugin(const Game* game) { 92static void render_plugin(const Game* game) {
93 assert(game); 93 assert(game);
94 assert(game->plugin); 94 assert(game->plugin);
95 if (plugin_resolve(game->plugin, plugin_render, "render")) { 95 if (plugin_resolve(game->plugin, PluginRender, "render")) {
96 void* plugin_state = get_plugin_state(game->plugin); 96 void* plugin_state = get_plugin_state(game->plugin);
97 plugin_call(game->plugin, plugin_render, "render", game, plugin_state); 97 plugin_call(game->plugin, PluginRender, "render", game, plugin_state);
98 } 98 }
99} 99}
100 100
@@ -102,24 +102,29 @@ static void render_plugin(const Game* game) {
102static void resize_plugin(Game* game, int width, int height) { 102static void resize_plugin(Game* game, int width, int height) {
103 assert(game); 103 assert(game);
104 assert(game->plugin); 104 assert(game->plugin);
105 if (plugin_resolve(game->plugin, plugin_resize, "resize")) { 105 if (plugin_resolve(game->plugin, PluginResize, "resize")) {
106 void* plugin_state = get_plugin_state(game->plugin); 106 void* plugin_state = get_plugin_state(game->plugin);
107 plugin_call( 107 plugin_call(
108 game->plugin, plugin_resize, "resize", game, plugin_state, width, 108 game->plugin, PluginResize, "resize", game, plugin_state, width,
109 height); 109 height);
110 } 110 }
111} 111}
112 112
113static void Shutdown(Game* game); 113static void Shutdown(GfxApp* app, GfxAppState* app_state);
114 114
115static bool Init(Game* game, int argc, const char** argv) { 115static bool Init(
116 assert(game); 116 GfxApp* app, GfxAppState* app_state, int argc, const char** argv) {
117 assert(app);
118 assert(app_state);
117 119
118 if (argc <= 1) { 120 if (argc <= 1) {
119 LOGE("Usage: %s <plugin> [plugin args]", argv[0]); 121 LOGE("Usage: %s <plugin> [plugin args]", argv[0]);
120 return false; 122 return false;
121 } 123 }
122 124
125 Game* game = &app_state->game;
126 game->app = app;
127
123 // Syntax: game <plugin> [plugin args] 128 // Syntax: game <plugin> [plugin args]
124 // 129 //
125 // Here we consume the <plugin> arg so that plugins receive the remainder 130 // Here we consume the <plugin> arg so that plugins receive the remainder
@@ -165,12 +170,15 @@ static bool Init(Game* game, int argc, const char** argv) {
165 170
166cleanup: 171cleanup:
167 LOGE("Gfx error: %s", get_error()); 172 LOGE("Gfx error: %s", get_error());
168 Shutdown(game); 173 Shutdown(app, app_state);
169 return false; 174 return false;
170} 175}
171 176
172static void Shutdown(Game* game) { 177static void Shutdown(GfxApp* app, GfxAppState* app_state) {
173 assert(game); 178 assert(app);
179 assert(app_state);
180 Game* game = &app_state->game;
181
174 shutdown_plugin(game); 182 shutdown_plugin(game);
175 if (game->gfx) { 183 if (game->gfx) {
176 gfx_destroy(&game->gfx); 184 gfx_destroy(&game->gfx);
@@ -183,7 +191,11 @@ static void Shutdown(Game* game) {
183 } 191 }
184} 192}
185 193
186static void Update(Game* game, double t, double dt) { 194static void Update(GfxApp* app, GfxAppState* app_state, double t, double dt) {
195 assert(app);
196 assert(app_state);
197 Game* game = &app_state->game;
198
187 plugin_engine_update(game->plugin_engine); 199 plugin_engine_update(game->plugin_engine);
188 if (plugin_reloaded(game->plugin)) { 200 if (plugin_reloaded(game->plugin)) {
189 shutdown_plugin(game); 201 shutdown_plugin(game);
@@ -198,14 +210,22 @@ static void Update(Game* game, double t, double dt) {
198 update_plugin(game, t, dt); 210 update_plugin(game, t, dt);
199} 211}
200 212
201static void Render(const Game* game) { 213static void Render(const GfxApp* app, GfxAppState* app_state) {
214 assert(app);
215 assert(app_state);
216 Game* game = &app_state->game;
217
202 GfxCore* gfxcore = gfx_get_core(game->gfx); 218 GfxCore* gfxcore = gfx_get_core(game->gfx);
203 gfx_start_frame(gfxcore); 219 gfx_start_frame(gfxcore);
204 render_plugin(game); 220 render_plugin(game);
205 gfx_end_frame(gfxcore); 221 gfx_end_frame(gfxcore);
206} 222}
207 223
208static void Resize(Game* game, int width, int height) { 224static void Resize(GfxApp* app, GfxAppState* app_state, int width, int height) {
225 assert(app);
226 assert(app_state);
227 Game* game = &app_state->game;
228
209 game->width = width; 229 game->width = width;
210 game->height = height; 230 game->height = height;
211 231
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 @@
6typedef struct PluginEngine PluginEngine; 6typedef struct PluginEngine PluginEngine;
7typedef struct Plugin Plugin; 7typedef struct Plugin Plugin;
8typedef struct Gfx Gfx; 8typedef struct Gfx Gfx;
9typedef struct GfxApp GfxApp;
9typedef struct Scene Scene; 10typedef struct Scene Scene;
10typedef struct SceneCamera SceneCamera; 11typedef struct SceneCamera SceneCamera;
11 12
@@ -15,6 +16,7 @@ typedef struct {
15 const char** argv; 16 const char** argv;
16 PluginEngine* plugin_engine; 17 PluginEngine* plugin_engine;
17 Plugin* plugin; 18 Plugin* plugin;
19 GfxApp* app;
18 Gfx* gfx; 20 Gfx* gfx;
19 int width; 21 int width;
20 int height; 22 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 @@
5 5
6#include "../game.h" 6#include "../game.h"
7 7
8#include <gfx/gfx.h>
9#include <gfx/scene.h>
10
11#include <stdbool.h>
12
13typedef struct State State; 8typedef struct State State;
14 9
15/// Initialize the plugin, which may optionally return a state object. 10/// Initialize the plugin, which may optionally return a state object.
@@ -44,9 +39,9 @@ void render(const Game*, const State*);
44void resize(Game*, State*, int width, int height); 39void resize(Game*, State*, int width, int height);
45 40
46// Signatures for the plugin's exposed functions. 41// Signatures for the plugin's exposed functions.
47typedef bool (*plugin_init)(Game*, State**); 42typedef bool (*PluginInit)(Game*, State**);
48typedef bool (*plugin_shutdown)(Game*, State*); 43typedef bool (*PluginShutdown)(Game*, State*);
49typedef bool (*plugin_boot)(Game*, State*); 44typedef bool (*PluginBoot)(Game*, State*);
50typedef void (*plugin_update)(Game*, State*, double t, double dt); 45typedef void (*PluginUpdate)(Game*, State*, double t, double dt);
51typedef void (*plugin_render)(const Game*, const State*); 46typedef void (*PluginRender)(const Game*, const State*);
52typedef void (*plugin_resize)(Game* game, State* state, int width, int height); 47typedef 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 @@
2 2
3#include <gfx/app.h> 3#include <gfx/app.h>
4#include <gfx/asset.h> 4#include <gfx/asset.h>
5#include <gfx/gfx.h>
5#include <gfx/llr/llr.h> 6#include <gfx/llr/llr.h>
6#include <gfx/renderer.h> 7#include <gfx/renderer.h>
7#include <gfx/renderer/imm_renderer.h> 8#include <gfx/renderer/imm_renderer.h>
@@ -204,13 +205,13 @@ void shutdown(Game* game, State* state) {
204 } 205 }
205} 206}
206 207
207static CameraCommand make_camera_command_from_input() { 208static CameraCommand make_camera_command_from_input(GfxApp* app) {
208 return (CameraCommand){ 209 return (CameraCommand){
209 .CameraMoveLeft = gfx_app_is_key_pressed(KeyA), 210 .CameraMoveLeft = gfx_app_is_key_pressed(app, KeyA),
210 .CameraMoveRight = gfx_app_is_key_pressed(KeyD), 211 .CameraMoveRight = gfx_app_is_key_pressed(app, KeyD),
211 .CameraMoveForward = gfx_app_is_key_pressed(KeyW), 212 .CameraMoveForward = gfx_app_is_key_pressed(app, KeyW),
212 .CameraMoveBackward = gfx_app_is_key_pressed(KeyS), 213 .CameraMoveBackward = gfx_app_is_key_pressed(app, KeyS),
213 .CameraIsRotating = gfx_app_is_mouse_button_pressed(LMB), 214 .CameraIsRotating = gfx_app_is_mouse_button_pressed(app, LMB),
214 }; 215 };
215} 216}
216 217
@@ -248,15 +249,17 @@ static void update_camera(
248 249
249void update(Game* game, State* state, double t, double dt) { 250void update(Game* game, State* state, double t, double dt) {
250 assert(game); 251 assert(game);
252 assert(game->app);
251 assert(state); 253 assert(state);
252 assert(state->scene); 254 assert(state->scene);
253 assert(state->camera); 255 assert(state->camera);
254 256
255 double mouse_x, mouse_y; 257 double mouse_x, mouse_y;
256 gfx_app_get_mouse_position(&mouse_x, &mouse_y); 258 gfx_app_get_mouse_position(game->app, &mouse_x, &mouse_y);
257 const vec2 mouse_position = {(R)mouse_x, (R)mouse_y}; 259 const vec2 mouse_position = {(R)mouse_x, (R)mouse_y};
258 260
259 const CameraCommand camera_command = make_camera_command_from_input(); 261 const CameraCommand camera_command =
262 make_camera_command_from_input(game->app);
260 update_camera( 263 update_camera(
261 &state->camera_controller, (R)dt, mouse_position, camera_command, 264 &state->camera_controller, (R)dt, mouse_position, camera_command,
262 &gfx_get_camera_camera(state->camera)->spatial); 265 &gfx_get_camera_camera(state->camera)->spatial);