summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c62
1 files changed, 41 insertions, 21 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