diff options
author | 3gg <3gg@shellblade.net> | 2024-03-09 08:55:46 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-03-09 08:55:46 -0800 |
commit | f9e38bf24e1874d773083d06ca443cce9bd92d56 (patch) | |
tree | a86b156f921c57e6855c08b076d731added79c85 /app/include | |
parent | adbd2511beec8f1caa1752bdfd755cc2f62ba425 (diff) |
Rename gfx_app -> app.
Diffstat (limited to 'app/include')
-rw-r--r-- | app/include/gfx/app.h | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/app/include/gfx/app.h b/app/include/gfx/app.h new file mode 100644 index 0000000..ffff4bc --- /dev/null +++ b/app/include/gfx/app.h | |||
@@ -0,0 +1,95 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <stdbool.h> | ||
4 | |||
5 | typedef struct GfxAppState GfxAppState; | ||
6 | |||
7 | /// Application settings. | ||
8 | typedef struct GfxAppDesc { | ||
9 | int argc; // Number of application arguments. | ||
10 | const char** argv; // Application arguments. | ||
11 | int width; // Window width. | ||
12 | int height; // Window height. | ||
13 | int max_fps; // Desired maximum display framerate. 0 to disable. | ||
14 | double update_delta_time; // Desired delta time between frame updates. | ||
15 | const char* title; // Window title. | ||
16 | GfxAppState* app_state; | ||
17 | } GfxAppDesc; | ||
18 | |||
19 | typedef bool (*GfxAppInit)(GfxAppState*, int argc, const char** argv); | ||
20 | typedef void (*GfxAppShutdown)(GfxAppState*); | ||
21 | typedef void (*GfxAppUpdate)(GfxAppState*, double t, double dt); | ||
22 | typedef void (*GfxAppRender)(GfxAppState*); | ||
23 | typedef void (*GfxAppResize)(GfxAppState*, int width, int height); | ||
24 | |||
25 | /// Application callback functions. | ||
26 | typedef struct GfxAppCallbacks { | ||
27 | GfxAppInit init; | ||
28 | GfxAppShutdown shutdown; | ||
29 | GfxAppUpdate update; | ||
30 | GfxAppRender render; | ||
31 | GfxAppResize resize; | ||
32 | } GfxAppCallbacks; | ||
33 | |||
34 | typedef enum Key { | ||
35 | KeyA = 'a', | ||
36 | KeyB, | ||
37 | KeyC, | ||
38 | KeyD, | ||
39 | KeyE, | ||
40 | KeyF, | ||
41 | KeyG, | ||
42 | KeyH, | ||
43 | KeyI, | ||
44 | KeyJ, | ||
45 | KeyK, | ||
46 | KeyL, | ||
47 | KeyM, | ||
48 | KeyN, | ||
49 | KeyO, | ||
50 | KeyP, | ||
51 | KeyQ, | ||
52 | KeyR, | ||
53 | KeyS, | ||
54 | KeyT, | ||
55 | KeyU, | ||
56 | KeyV, | ||
57 | KeyW, | ||
58 | KeyX, | ||
59 | KeyY, | ||
60 | KeyZ, | ||
61 | } Key; | ||
62 | |||
63 | /// Create a window with an OpenGL context and run the main loop. | ||
64 | bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*); | ||
65 | |||
66 | /// Get the mouse coordinates relative to the app's window. | ||
67 | void gfx_app_get_mouse_position(double* x, double* y); | ||
68 | |||
69 | /// Return true if the given key is pressed. | ||
70 | bool gfx_is_key_pressed(Key); | ||
71 | |||
72 | /// Define a main function that initializes and puts the application in a loop. | ||
73 | /// See also: gfx_app_run(). | ||
74 | #define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS, TITLE) \ | ||
75 | int main(int argc, const char** argv) { \ | ||
76 | GfxAppState app_state = {0}; \ | ||
77 | gfx_app_run( \ | ||
78 | &(GfxAppDesc){ \ | ||
79 | .argc = argc, \ | ||
80 | .argv = argv, \ | ||
81 | .width = WIDTH, \ | ||
82 | .height = HEIGHT, \ | ||
83 | .max_fps = MAX_FPS, \ | ||
84 | .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0, \ | ||
85 | .title = TITLE, \ | ||
86 | .app_state = &app_state, \ | ||
87 | }, \ | ||
88 | &(GfxAppCallbacks){ \ | ||
89 | .init = (GfxAppInit)app_init, \ | ||
90 | .update = (GfxAppUpdate)app_update, \ | ||
91 | .render = (GfxAppRender)app_render, \ | ||
92 | .resize = (GfxAppResize)app_resize, \ | ||
93 | .shutdown = (GfxAppShutdown)app_end}); \ | ||
94 | return 0; \ | ||
95 | } | ||