diff options
author | 3gg <3gg@shellblade.net> | 2024-01-18 19:33:18 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-01-18 19:33:18 -0800 |
commit | fc883e0b0449509ba2e1c5d14d187feee098ab34 (patch) | |
tree | 83dec5ce272cf07ddf7855a44413253210438490 /gltfview/src | |
parent | cef3385c2bee0b098a7795548345a9281ace008e (diff) |
Simplify game callbacks.
Diffstat (limited to 'gltfview/src')
-rw-r--r-- | gltfview/src/game.c | 52 | ||||
-rw-r--r-- | gltfview/src/game.h | 34 | ||||
-rw-r--r-- | gltfview/src/main.c | 67 |
3 files changed, 47 insertions, 106 deletions
diff --git a/gltfview/src/game.c b/gltfview/src/game.c index 5ca9ad4..c331190 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c | |||
@@ -7,11 +7,17 @@ | |||
7 | #define _GNU_SOURCE 200112L // For readlink() | 7 | #define _GNU_SOURCE 200112L // For readlink() |
8 | 8 | ||
9 | #include "game.h" | 9 | #include "game.h" |
10 | |||
10 | #include "plugins/plugin.h" | 11 | #include "plugins/plugin.h" |
11 | 12 | ||
13 | #include <gfx/gfx.h> | ||
14 | #include <gfx/gfx_app.h> | ||
12 | #include <gfx/render_backend.h> | 15 | #include <gfx/render_backend.h> |
16 | #include <gfx/renderer.h> | ||
13 | #include <gfx/scene/camera.h> | 17 | #include <gfx/scene/camera.h> |
18 | #include <gfx/scene/node.h> | ||
14 | #include <gfx/scene/object.h> | 19 | #include <gfx/scene/object.h> |
20 | #include <gfx/scene/scene.h> | ||
15 | 21 | ||
16 | #include <error.h> | 22 | #include <error.h> |
17 | #include <log/log.h> | 23 | #include <log/log.h> |
@@ -21,6 +27,7 @@ | |||
21 | #include <assert.h> | 27 | #include <assert.h> |
22 | #include <stdbool.h> | 28 | #include <stdbool.h> |
23 | #include <stdio.h> | 29 | #include <stdio.h> |
30 | #include <stdlib.h> | ||
24 | 31 | ||
25 | #include <linux/limits.h> | 32 | #include <linux/limits.h> |
26 | 33 | ||
@@ -28,18 +35,32 @@ | |||
28 | 35 | ||
29 | #undef _GNU_SOURCE | 36 | #undef _GNU_SOURCE |
30 | 37 | ||
31 | // Plugin to load if no plugin is provided. | 38 | static const int WIDTH = 1350; |
32 | static const char* DEFAULT_PLUGIN = "texture_view"; | 39 | static const int HEIGHT = 900; |
40 | static const int MAX_FPS = 60; | ||
33 | 41 | ||
34 | bool game_new(Game* game, int argc, const char** argv) { | 42 | void app_end(Game* game); |
35 | assert(game); | 43 | |
44 | bool app_init(const GfxAppDesc* desc, void** app_state) { | ||
45 | assert(desc); | ||
46 | |||
47 | if (desc->argc <= 1) { | ||
48 | LOGE("Usage: %s <plugin> [plugin args]", desc->argv[0]); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | Game* game = calloc(1, sizeof(Game)); | ||
53 | if (!game) { | ||
54 | LOGE("Failed to allocate game state"); | ||
55 | return false; | ||
56 | } | ||
36 | 57 | ||
37 | // Syntax: game [plugin] <plugin args> | 58 | // Syntax: game <plugin> [plugin args] |
38 | // | 59 | // |
39 | // Here we consume the [plugin] arg so that plugins receive the remainder | 60 | // Here we consume the <plugin> arg so that plugins receive the remainder |
40 | // args starting from 0. | 61 | // args starting from 0. |
41 | game->argc = argc - 1; | 62 | game->argc = desc->argc - 1; |
42 | game->argv = argv + 1; | 63 | game->argv = desc->argv + 1; |
43 | 64 | ||
44 | char exe_path_buf[NAME_MAX] = {0}; | 65 | char exe_path_buf[NAME_MAX] = {0}; |
45 | if (readlink("/proc/self/exe", exe_path_buf, sizeof(exe_path_buf)) == -1) { | 66 | if (readlink("/proc/self/exe", exe_path_buf, sizeof(exe_path_buf)) == -1) { |
@@ -59,7 +80,7 @@ bool game_new(Game* game, int argc, const char** argv) { | |||
59 | goto cleanup; | 80 | goto cleanup; |
60 | } | 81 | } |
61 | 82 | ||
62 | const char* plugin = argc > 1 ? argv[1] : DEFAULT_PLUGIN; | 83 | const char* plugin = desc->argv[1]; |
63 | if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) { | 84 | if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) { |
64 | goto cleanup; | 85 | goto cleanup; |
65 | } | 86 | } |
@@ -91,15 +112,16 @@ bool game_new(Game* game, int argc, const char** argv) { | |||
91 | } | 112 | } |
92 | } | 113 | } |
93 | 114 | ||
115 | *app_state = game; | ||
94 | return true; | 116 | return true; |
95 | 117 | ||
96 | cleanup: | 118 | cleanup: |
97 | LOGE("Gfx error: %s", get_error()); | 119 | LOGE("Gfx error: %s", get_error()); |
98 | game_end(game); | 120 | app_end(game); |
99 | return false; | 121 | return false; |
100 | } | 122 | } |
101 | 123 | ||
102 | void game_end(Game* game) { | 124 | void app_end(Game* game) { |
103 | assert(game); | 125 | assert(game); |
104 | if (game->gfx) { | 126 | if (game->gfx) { |
105 | gfx_destroy(&game->gfx); | 127 | gfx_destroy(&game->gfx); |
@@ -112,7 +134,7 @@ void game_end(Game* game) { | |||
112 | } | 134 | } |
113 | } | 135 | } |
114 | 136 | ||
115 | void game_update(Game* game, double t, double dt) { | 137 | void app_update(Game* game, double t, double dt) { |
116 | plugin_engine_update(game->plugin_engine); | 138 | plugin_engine_update(game->plugin_engine); |
117 | if (plugin_reloaded(game->plugin) && | 139 | if (plugin_reloaded(game->plugin) && |
118 | plugin_resolve(game->plugin, plugin_init, "init")) { | 140 | plugin_resolve(game->plugin, plugin_init, "init")) { |
@@ -129,7 +151,7 @@ void game_update(Game* game, double t, double dt) { | |||
129 | } | 151 | } |
130 | } | 152 | } |
131 | 153 | ||
132 | void game_render(const Game* game) { | 154 | void app_render(const Game* game) { |
133 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); | 155 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); |
134 | Renderer* renderer = gfx_get_renderer(game->gfx); | 156 | Renderer* renderer = gfx_get_renderer(game->gfx); |
135 | 157 | ||
@@ -149,7 +171,7 @@ void game_render(const Game* game) { | |||
149 | gfx_end_frame(render_backend); | 171 | gfx_end_frame(render_backend); |
150 | } | 172 | } |
151 | 173 | ||
152 | void game_set_viewport(Game* game, int width, int height) { | 174 | void app_resize(Game* game, int width, int height) { |
153 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); | 175 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); |
154 | gfx_set_viewport(render_backend, width, height); | 176 | gfx_set_viewport(render_backend, width, height); |
155 | 177 | ||
@@ -162,3 +184,5 @@ void game_set_viewport(Game* game, int width, int height) { | |||
162 | Camera* camera = gfx_get_camera_camera(game->camera); | 184 | Camera* camera = gfx_get_camera_camera(game->camera); |
163 | camera->projection = projection; | 185 | camera->projection = projection; |
164 | } | 186 | } |
187 | |||
188 | GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS); | ||
diff --git a/gltfview/src/game.h b/gltfview/src/game.h index 2a7b7ef..53725c7 100644 --- a/gltfview/src/game.h +++ b/gltfview/src/game.h | |||
@@ -1,19 +1,13 @@ | |||
1 | /* | ||
2 | * Header file defining the game state, included by plugins. | ||
3 | */ | ||
1 | #pragma once | 4 | #pragma once |
2 | 5 | ||
3 | #include <gfx/gfx.h> | ||
4 | #include <gfx/render_backend.h> | ||
5 | #include <gfx/renderer.h> | ||
6 | #include <gfx/scene/camera.h> | ||
7 | #include <gfx/scene/node.h> | ||
8 | #include <gfx/scene/scene.h> | ||
9 | |||
10 | #include <stdbool.h> | ||
11 | |||
12 | typedef struct Plugin Plugin; | ||
13 | typedef struct PluginEngine PluginEngine; | 6 | typedef struct PluginEngine PluginEngine; |
14 | 7 | typedef struct Plugin Plugin; | |
15 | /// The delta time the game should be updated with. | 8 | typedef struct Gfx Gfx; |
16 | static const double game_dt = 1.0 / 60.0; | 9 | typedef struct Scene Scene; |
10 | typedef struct SceneCamera SceneCamera; | ||
17 | 11 | ||
18 | /// Game state. | 12 | /// Game state. |
19 | typedef struct { | 13 | typedef struct { |
@@ -22,16 +16,6 @@ typedef struct { | |||
22 | PluginEngine* plugin_engine; | 16 | PluginEngine* plugin_engine; |
23 | Plugin* plugin; | 17 | Plugin* plugin; |
24 | Gfx* gfx; | 18 | Gfx* gfx; |
25 | Scene* scene; | 19 | Scene* scene; // TODO: Move scene graph to plugin? |
26 | SceneCamera* camera; | 20 | SceneCamera* camera; // TODO: Move too. |
27 | } Game; | 21 | } Game; |
28 | |||
29 | bool game_new(Game*, int argc, const char** argv); | ||
30 | |||
31 | void game_end(Game*); | ||
32 | |||
33 | void game_update(Game*, double t, double dt); | ||
34 | |||
35 | void game_render(const Game*); | ||
36 | |||
37 | void game_set_viewport(Game*, int width, int height); | ||
diff --git a/gltfview/src/main.c b/gltfview/src/main.c deleted file mode 100644 index f4863b4..0000000 --- a/gltfview/src/main.c +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | #include "game.h" | ||
2 | |||
3 | #include <gfx/gfx_app.h> | ||
4 | #include <log/log.h> | ||
5 | |||
6 | #include <stdlib.h> | ||
7 | |||
8 | static bool init(const GfxAppDesc* desc, void** app_state) { | ||
9 | Game* game = calloc(1, sizeof(Game)); | ||
10 | if (!game) { | ||
11 | LOGE("Failed to allocate game state"); | ||
12 | return false; | ||
13 | } | ||
14 | if (!game_new(game, desc->argc, desc->argv)) { | ||
15 | LOGE("Failed to initialize game"); | ||
16 | return false; | ||
17 | } | ||
18 | *app_state = game; | ||
19 | return true; | ||
20 | } | ||
21 | |||
22 | static void shutdown(void* app_state) { | ||
23 | assert(app_state); | ||
24 | Game* game = (Game*)(app_state); | ||
25 | game_end(game); | ||
26 | } | ||
27 | |||
28 | static void update(void* app_state, double t, double dt) { | ||
29 | assert(app_state); | ||
30 | Game* game = (Game*)(app_state); | ||
31 | game_update(game, t, dt); | ||
32 | } | ||
33 | |||
34 | static void render(void* app_state) { | ||
35 | assert(app_state); | ||
36 | Game* game = (Game*)(app_state); | ||
37 | game_render(game); | ||
38 | } | ||
39 | |||
40 | static void resize(void* app_state, int width, int height) { | ||
41 | assert(app_state); | ||
42 | Game* game = (Game*)(app_state); | ||
43 | game_set_viewport(game, width, height); | ||
44 | } | ||
45 | |||
46 | int main(int argc, const char** argv) { | ||
47 | const int initial_width = 1350; | ||
48 | const int initial_height = 900; | ||
49 | const int max_fps = 60; | ||
50 | |||
51 | gfx_app_run( | ||
52 | &(GfxAppDesc){ | ||
53 | .argc = argc, | ||
54 | .argv = argv, | ||
55 | .width = initial_width, | ||
56 | .height = initial_height, | ||
57 | .max_fps = max_fps, | ||
58 | .update_delta_time = max_fps > 0 ? 1.0 / (double)max_fps : 0.0}, | ||
59 | &(GfxAppCallbacks){ | ||
60 | .init = init, | ||
61 | .update = update, | ||
62 | .render = render, | ||
63 | .resize = resize, | ||
64 | .shutdown = shutdown}); | ||
65 | |||
66 | return 0; | ||
67 | } | ||