From fc883e0b0449509ba2e1c5d14d187feee098ab34 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 18 Jan 2024 19:33:18 -0800 Subject: Simplify game callbacks. --- gltfview/src/game.c | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'gltfview/src/game.c') 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 @@ #define _GNU_SOURCE 200112L // For readlink() #include "game.h" + #include "plugins/plugin.h" +#include +#include #include +#include #include +#include #include +#include #include #include @@ -21,6 +27,7 @@ #include #include #include +#include #include @@ -28,18 +35,32 @@ #undef _GNU_SOURCE -// Plugin to load if no plugin is provided. -static const char* DEFAULT_PLUGIN = "texture_view"; +static const int WIDTH = 1350; +static const int HEIGHT = 900; +static const int MAX_FPS = 60; -bool game_new(Game* game, int argc, const char** argv) { - assert(game); +void app_end(Game* game); + +bool app_init(const GfxAppDesc* desc, void** app_state) { + assert(desc); + + if (desc->argc <= 1) { + LOGE("Usage: %s [plugin args]", desc->argv[0]); + return false; + } + + Game* game = calloc(1, sizeof(Game)); + if (!game) { + LOGE("Failed to allocate game state"); + return false; + } - // Syntax: game [plugin] + // Syntax: game [plugin args] // - // Here we consume the [plugin] arg so that plugins receive the remainder + // Here we consume the arg so that plugins receive the remainder // args starting from 0. - game->argc = argc - 1; - game->argv = argv + 1; + game->argc = desc->argc - 1; + game->argv = desc->argv + 1; char exe_path_buf[NAME_MAX] = {0}; 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) { goto cleanup; } - const char* plugin = argc > 1 ? argv[1] : DEFAULT_PLUGIN; + const char* plugin = desc->argv[1]; if (!(game->plugin = load_plugin(game->plugin_engine, plugin))) { goto cleanup; } @@ -91,15 +112,16 @@ bool game_new(Game* game, int argc, const char** argv) { } } + *app_state = game; return true; cleanup: LOGE("Gfx error: %s", get_error()); - game_end(game); + app_end(game); return false; } -void game_end(Game* game) { +void app_end(Game* game) { assert(game); if (game->gfx) { gfx_destroy(&game->gfx); @@ -112,7 +134,7 @@ void game_end(Game* game) { } } -void game_update(Game* game, double t, double dt) { +void app_update(Game* game, double t, double dt) { plugin_engine_update(game->plugin_engine); if (plugin_reloaded(game->plugin) && plugin_resolve(game->plugin, plugin_init, "init")) { @@ -129,7 +151,7 @@ void game_update(Game* game, double t, double dt) { } } -void game_render(const Game* game) { +void app_render(const Game* game) { RenderBackend* render_backend = gfx_get_render_backend(game->gfx); Renderer* renderer = gfx_get_renderer(game->gfx); @@ -149,7 +171,7 @@ void game_render(const Game* game) { gfx_end_frame(render_backend); } -void game_set_viewport(Game* game, int width, int height) { +void app_resize(Game* game, int width, int height) { RenderBackend* render_backend = gfx_get_render_backend(game->gfx); gfx_set_viewport(render_backend, width, height); @@ -162,3 +184,5 @@ void game_set_viewport(Game* game, int width, int height) { Camera* camera = gfx_get_camera_camera(game->camera); camera->projection = projection; } + +GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS); -- cgit v1.2.3