From fc883e0b0449509ba2e1c5d14d187feee098ab34 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 18 Jan 2024 19:33:18 -0800 Subject: Simplify game callbacks. --- gfx-app/include/gfx/gfx_app.h | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'gfx-app') 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 @@ #include +/// Application settings. typedef struct GfxAppDesc { int argc; // Number of application arguments. const char** argv; // Application arguments. @@ -12,12 +13,19 @@ typedef struct GfxAppDesc { const char* title; // Window title. } GfxAppDesc; +typedef bool (*GfxAppInit)(const GfxAppDesc*, void** app_state); +typedef void (*GfxAppUpdate)(void* app_state, double t, double dt); +typedef void (*GfxAppRender)(void* app_state); +typedef void (*GfxAppResize)(void* app_state, int width, int height); +typedef void (*GfxAppShutdown)(void* app_state); + +/// Application callback functions. typedef struct GfxAppCallbacks { - bool (*init)(const GfxAppDesc*, void** app_state); - void (*update)(void* app_state, double t, double dt); - void (*render)(void* app_state); - void (*resize)(void* app_state, int width, int height); - void (*shutdown)(void* app_state); + GfxAppInit init; + GfxAppUpdate update; + GfxAppRender render; + GfxAppResize resize; + GfxAppShutdown shutdown; } GfxAppCallbacks; /// Create a window with an OpenGL context and run the main loop. @@ -25,3 +33,24 @@ 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); + +/// Define a main function that initializes and puts the application in a loop. +/// See also: gfx_app_run(). +#define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS) \ + int main(int argc, const char** argv) { \ + gfx_app_run( \ + &(GfxAppDesc){ \ + .argc = argc, \ + .argv = argv, \ + .width = WIDTH, \ + .height = HEIGHT, \ + .max_fps = MAX_FPS, \ + .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0}, \ + &(GfxAppCallbacks){ \ + .init = (GfxAppInit)app_init, \ + .update = (GfxAppUpdate)app_update, \ + .render = (GfxAppRender)app_render, \ + .resize = (GfxAppResize)app_resize, \ + .shutdown = (GfxAppShutdown)app_end}); \ + return 0; \ + } -- cgit v1.2.3