summaryrefslogtreecommitdiff
path: root/gfx-app/include
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-01-18 19:33:18 -0800
committer3gg <3gg@shellblade.net>2024-01-18 19:33:18 -0800
commitfc883e0b0449509ba2e1c5d14d187feee098ab34 (patch)
tree83dec5ce272cf07ddf7855a44413253210438490 /gfx-app/include
parentcef3385c2bee0b098a7795548345a9281ace008e (diff)
Simplify game callbacks.
Diffstat (limited to 'gfx-app/include')
-rw-r--r--gfx-app/include/gfx/gfx_app.h39
1 files changed, 34 insertions, 5 deletions
diff --git a/gfx-app/include/gfx/gfx_app.h b/gfx-app/include/gfx/gfx_app.h
index 0033bde..3c544fa 100644
--- a/gfx-app/include/gfx/gfx_app.h
+++ b/gfx-app/include/gfx/gfx_app.h
@@ -2,6 +2,7 @@
2 2
3#include <stdbool.h> 3#include <stdbool.h>
4 4
5/// Application settings.
5typedef struct GfxAppDesc { 6typedef struct GfxAppDesc {
6 int argc; // Number of application arguments. 7 int argc; // Number of application arguments.
7 const char** argv; // Application arguments. 8 const char** argv; // Application arguments.
@@ -12,12 +13,19 @@ typedef struct GfxAppDesc {
12 const char* title; // Window title. 13 const char* title; // Window title.
13} GfxAppDesc; 14} GfxAppDesc;
14 15
16typedef bool (*GfxAppInit)(const GfxAppDesc*, void** app_state);
17typedef void (*GfxAppUpdate)(void* app_state, double t, double dt);
18typedef void (*GfxAppRender)(void* app_state);
19typedef void (*GfxAppResize)(void* app_state, int width, int height);
20typedef void (*GfxAppShutdown)(void* app_state);
21
22/// Application callback functions.
15typedef struct GfxAppCallbacks { 23typedef struct GfxAppCallbacks {
16 bool (*init)(const GfxAppDesc*, void** app_state); 24 GfxAppInit init;
17 void (*update)(void* app_state, double t, double dt); 25 GfxAppUpdate update;
18 void (*render)(void* app_state); 26 GfxAppRender render;
19 void (*resize)(void* app_state, int width, int height); 27 GfxAppResize resize;
20 void (*shutdown)(void* app_state); 28 GfxAppShutdown shutdown;
21} GfxAppCallbacks; 29} GfxAppCallbacks;
22 30
23/// Create a window with an OpenGL context and run the main loop. 31/// Create a window with an OpenGL context and run the main loop.
@@ -25,3 +33,24 @@ bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*);
25 33
26/// Get the mouse coordinates relative to the app's window. 34/// Get the mouse coordinates relative to the app's window.
27void gfx_app_get_mouse_position(double* x, double* y); 35void gfx_app_get_mouse_position(double* x, double* y);
36
37/// Define a main function that initializes and puts the application in a loop.
38/// See also: gfx_app_run().
39#define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS) \
40 int main(int argc, const char** argv) { \
41 gfx_app_run( \
42 &(GfxAppDesc){ \
43 .argc = argc, \
44 .argv = argv, \
45 .width = WIDTH, \
46 .height = HEIGHT, \
47 .max_fps = MAX_FPS, \
48 .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0}, \
49 &(GfxAppCallbacks){ \
50 .init = (GfxAppInit)app_init, \
51 .update = (GfxAppUpdate)app_update, \
52 .render = (GfxAppRender)app_render, \
53 .resize = (GfxAppResize)app_resize, \
54 .shutdown = (GfxAppShutdown)app_end}); \
55 return 0; \
56 }