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/game.c | |
parent | cef3385c2bee0b098a7795548345a9281ace008e (diff) |
Simplify game callbacks.
Diffstat (limited to 'gltfview/src/game.c')
-rw-r--r-- | gltfview/src/game.c | 52 |
1 files changed, 38 insertions, 14 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); | ||