diff options
Diffstat (limited to 'gfx-app')
-rw-r--r-- | gfx-app/include/gfx/gfx_app.h | 53 | ||||
-rw-r--r-- | gfx-app/src/gfx_app.c | 6 |
2 files changed, 34 insertions, 25 deletions
diff --git a/gfx-app/include/gfx/gfx_app.h b/gfx-app/include/gfx/gfx_app.h index 86c502a..ffff4bc 100644 --- a/gfx-app/include/gfx/gfx_app.h +++ b/gfx-app/include/gfx/gfx_app.h | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | #include <stdbool.h> | 3 | #include <stdbool.h> |
4 | 4 | ||
5 | typedef struct GfxAppState GfxAppState; | ||
6 | |||
5 | /// Application settings. | 7 | /// Application settings. |
6 | typedef struct GfxAppDesc { | 8 | typedef struct GfxAppDesc { |
7 | int argc; // Number of application arguments. | 9 | int argc; // Number of application arguments. |
@@ -11,21 +13,22 @@ typedef struct GfxAppDesc { | |||
11 | int max_fps; // Desired maximum display framerate. 0 to disable. | 13 | int max_fps; // Desired maximum display framerate. 0 to disable. |
12 | double update_delta_time; // Desired delta time between frame updates. | 14 | double update_delta_time; // Desired delta time between frame updates. |
13 | const char* title; // Window title. | 15 | const char* title; // Window title. |
16 | GfxAppState* app_state; | ||
14 | } GfxAppDesc; | 17 | } GfxAppDesc; |
15 | 18 | ||
16 | typedef bool (*GfxAppInit)(const GfxAppDesc*, void** app_state); | 19 | typedef bool (*GfxAppInit)(GfxAppState*, int argc, const char** argv); |
17 | typedef void (*GfxAppUpdate)(void* app_state, double t, double dt); | 20 | typedef void (*GfxAppShutdown)(GfxAppState*); |
18 | typedef void (*GfxAppRender)(void* app_state); | 21 | typedef void (*GfxAppUpdate)(GfxAppState*, double t, double dt); |
19 | typedef void (*GfxAppResize)(void* app_state, int width, int height); | 22 | typedef void (*GfxAppRender)(GfxAppState*); |
20 | typedef void (*GfxAppShutdown)(void* app_state); | 23 | typedef void (*GfxAppResize)(GfxAppState*, int width, int height); |
21 | 24 | ||
22 | /// Application callback functions. | 25 | /// Application callback functions. |
23 | typedef struct GfxAppCallbacks { | 26 | typedef struct GfxAppCallbacks { |
24 | GfxAppInit init; | 27 | GfxAppInit init; |
28 | GfxAppShutdown shutdown; | ||
25 | GfxAppUpdate update; | 29 | GfxAppUpdate update; |
26 | GfxAppRender render; | 30 | GfxAppRender render; |
27 | GfxAppResize resize; | 31 | GfxAppResize resize; |
28 | GfxAppShutdown shutdown; | ||
29 | } GfxAppCallbacks; | 32 | } GfxAppCallbacks; |
30 | 33 | ||
31 | typedef enum Key { | 34 | typedef enum Key { |
@@ -68,21 +71,25 @@ bool gfx_is_key_pressed(Key); | |||
68 | 71 | ||
69 | /// Define a main function that initializes and puts the application in a loop. | 72 | /// Define a main function that initializes and puts the application in a loop. |
70 | /// See also: gfx_app_run(). | 73 | /// See also: gfx_app_run(). |
71 | #define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS) \ | 74 | #define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS, TITLE) \ |
72 | int main(int argc, const char** argv) { \ | 75 | int main(int argc, const char** argv) { \ |
73 | gfx_app_run( \ | 76 | GfxAppState app_state = {0}; \ |
74 | &(GfxAppDesc){ \ | 77 | gfx_app_run( \ |
75 | .argc = argc, \ | 78 | &(GfxAppDesc){ \ |
76 | .argv = argv, \ | 79 | .argc = argc, \ |
77 | .width = WIDTH, \ | 80 | .argv = argv, \ |
78 | .height = HEIGHT, \ | 81 | .width = WIDTH, \ |
79 | .max_fps = MAX_FPS, \ | 82 | .height = HEIGHT, \ |
80 | .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0}, \ | 83 | .max_fps = MAX_FPS, \ |
81 | &(GfxAppCallbacks){ \ | 84 | .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0, \ |
82 | .init = (GfxAppInit)app_init, \ | 85 | .title = TITLE, \ |
83 | .update = (GfxAppUpdate)app_update, \ | 86 | .app_state = &app_state, \ |
84 | .render = (GfxAppRender)app_render, \ | 87 | }, \ |
85 | .resize = (GfxAppResize)app_resize, \ | 88 | &(GfxAppCallbacks){ \ |
86 | .shutdown = (GfxAppShutdown)app_end}); \ | 89 | .init = (GfxAppInit)app_init, \ |
87 | return 0; \ | 90 | .update = (GfxAppUpdate)app_update, \ |
91 | .render = (GfxAppRender)app_render, \ | ||
92 | .resize = (GfxAppResize)app_resize, \ | ||
93 | .shutdown = (GfxAppShutdown)app_end}); \ | ||
94 | return 0; \ | ||
88 | } | 95 | } |
diff --git a/gfx-app/src/gfx_app.c b/gfx-app/src/gfx_app.c index e0211b1..a93756c 100644 --- a/gfx-app/src/gfx_app.c +++ b/gfx-app/src/gfx_app.c | |||
@@ -9,7 +9,7 @@ | |||
9 | 9 | ||
10 | /// Application state. | 10 | /// Application state. |
11 | typedef struct GfxApp { | 11 | typedef struct GfxApp { |
12 | void* app_state; | 12 | GfxAppState* app_state; |
13 | GfxAppCallbacks callbacks; | 13 | GfxAppCallbacks callbacks; |
14 | int max_fps; | 14 | int max_fps; |
15 | double update_delta_time; | 15 | double update_delta_time; |
@@ -79,6 +79,7 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { | |||
79 | 79 | ||
80 | bool success = false; | 80 | bool success = false; |
81 | 81 | ||
82 | g_gfx_app.app_state = desc->app_state; | ||
82 | g_gfx_app.callbacks = *callbacks; | 83 | g_gfx_app.callbacks = *callbacks; |
83 | g_gfx_app.max_fps = desc->max_fps; | 84 | g_gfx_app.max_fps = desc->max_fps; |
84 | g_gfx_app.update_delta_time = desc->update_delta_time; | 85 | g_gfx_app.update_delta_time = desc->update_delta_time; |
@@ -110,7 +111,8 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { | |||
110 | glfwMakeContextCurrent(g_gfx_app.window); | 111 | glfwMakeContextCurrent(g_gfx_app.window); |
111 | 112 | ||
112 | // Initialize the application's state before setting any callbacks. | 113 | // Initialize the application's state before setting any callbacks. |
113 | if (!(*g_gfx_app.callbacks.init)(desc, &g_gfx_app.app_state)) { | 114 | if (!(*g_gfx_app.callbacks.init)( |
115 | g_gfx_app.app_state, desc->argc, desc->argv)) { | ||
114 | LOGE("Failed to initialize application"); | 116 | LOGE("Failed to initialize application"); |
115 | goto cleanup; | 117 | goto cleanup; |
116 | } | 118 | } |