summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-01-18 19:33:18 -0800
committer3gg <3gg@shellblade.net>2024-01-18 19:33:18 -0800
commitfc883e0b0449509ba2e1c5d14d187feee098ab34 (patch)
tree83dec5ce272cf07ddf7855a44413253210438490
parentcef3385c2bee0b098a7795548345a9281ace008e (diff)
Simplify game callbacks.
-rw-r--r--gfx-app/include/gfx/gfx_app.h39
-rw-r--r--gltfview/CMakeLists.txt3
-rw-r--r--gltfview/src/game.c52
-rw-r--r--gltfview/src/game.h34
-rw-r--r--gltfview/src/main.c67
5 files changed, 82 insertions, 113 deletions
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 @@
2 2
3#include <stdbool.h> 3#include <stdbool.h>
4 4
5/// Application settings.
5typedef struct GfxAppDesc { 6typedef struct GfxAppDesc {
6 int argc; // Number of application arguments. 7 int argc; // Number of application arguments.
7 const char** argv; // Application arguments. 8 const char** argv; // Application arguments.
@@ -12,12 +13,19 @@ typedef struct GfxAppDesc {
12 const char* title; // Window title. 13 const char* title; // Window title.
13} GfxAppDesc; 14} GfxAppDesc;
14 15
16typedef bool (*GfxAppInit)(const GfxAppDesc*, void** app_state);
17typedef void (*GfxAppUpdate)(void* app_state, double t, double dt);
18typedef void (*GfxAppRender)(void* app_state);
19typedef void (*GfxAppResize)(void* app_state, int width, int height);
20typedef void (*GfxAppShutdown)(void* app_state);
21
22/// Application callback functions.
15typedef struct GfxAppCallbacks { 23typedef struct GfxAppCallbacks {
16 bool (*init)(const GfxAppDesc*, void** app_state); 24 GfxAppInit init;
17 void (*update)(void* app_state, double t, double dt); 25 GfxAppUpdate update;
18 void (*render)(void* app_state); 26 GfxAppRender render;
19 void (*resize)(void* app_state, int width, int height); 27 GfxAppResize resize;
20 void (*shutdown)(void* app_state); 28 GfxAppShutdown shutdown;
21} GfxAppCallbacks; 29} GfxAppCallbacks;
22 30
23/// Create a window with an OpenGL context and run the main loop. 31/// Create a window with an OpenGL context and run the main loop.
@@ -25,3 +33,24 @@ bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*);
25 33
26/// Get the mouse coordinates relative to the app's window. 34/// Get the mouse coordinates relative to the app's window.
27void gfx_app_get_mouse_position(double* x, double* y); 35void gfx_app_get_mouse_position(double* x, double* y);
36
37/// Define a main function that initializes and puts the application in a loop.
38/// See also: gfx_app_run().
39#define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS) \
40 int main(int argc, const char** argv) { \
41 gfx_app_run( \
42 &(GfxAppDesc){ \
43 .argc = argc, \
44 .argv = argv, \
45 .width = WIDTH, \
46 .height = HEIGHT, \
47 .max_fps = MAX_FPS, \
48 .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0}, \
49 &(GfxAppCallbacks){ \
50 .init = (GfxAppInit)app_init, \
51 .update = (GfxAppUpdate)app_update, \
52 .render = (GfxAppRender)app_render, \
53 .resize = (GfxAppResize)app_resize, \
54 .shutdown = (GfxAppShutdown)app_end}); \
55 return 0; \
56 }
diff --git a/gltfview/CMakeLists.txt b/gltfview/CMakeLists.txt
index de745ce..98c3b47 100644
--- a/gltfview/CMakeLists.txt
+++ b/gltfview/CMakeLists.txt
@@ -5,8 +5,7 @@ add_subdirectory(src/plugins)
5project(gltfview) 5project(gltfview)
6 6
7add_executable(gltfview 7add_executable(gltfview
8 src/game.c 8 src/game.c)
9 src/main.c)
10 9
11target_include_directories(gltfview PRIVATE 10target_include_directories(gltfview PRIVATE
12 src/) 11 src/)
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. 38static const int WIDTH = 1350;
32static const char* DEFAULT_PLUGIN = "texture_view"; 39static const int HEIGHT = 900;
40static const int MAX_FPS = 60;
33 41
34bool game_new(Game* game, int argc, const char** argv) { 42void app_end(Game* game);
35 assert(game); 43
44bool 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
96cleanup: 118cleanup:
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
102void game_end(Game* game) { 124void 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
115void game_update(Game* game, double t, double dt) { 137void 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
132void game_render(const Game* game) { 154void 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
152void game_set_viewport(Game* game, int width, int height) { 174void 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
188GFX_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
12typedef struct Plugin Plugin;
13typedef struct PluginEngine PluginEngine; 6typedef struct PluginEngine PluginEngine;
14 7typedef struct Plugin Plugin;
15/// The delta time the game should be updated with. 8typedef struct Gfx Gfx;
16static const double game_dt = 1.0 / 60.0; 9typedef struct Scene Scene;
10typedef struct SceneCamera SceneCamera;
17 11
18/// Game state. 12/// Game state.
19typedef struct { 13typedef 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
29bool game_new(Game*, int argc, const char** argv);
30
31void game_end(Game*);
32
33void game_update(Game*, double t, double dt);
34
35void game_render(const Game*);
36
37void 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
8static 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
22static void shutdown(void* app_state) {
23 assert(app_state);
24 Game* game = (Game*)(app_state);
25 game_end(game);
26}
27
28static 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
34static void render(void* app_state) {
35 assert(app_state);
36 Game* game = (Game*)(app_state);
37 game_render(game);
38}
39
40static 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
46int 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}