From 4212e57d06afac8a19b09fdebc24bad10b78f1ac Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 19 Jan 2024 07:21:45 -0800 Subject: Plugin refactor, moving scene from game to plugin. --- gltfview/src/plugins/gltf_view.c | 77 +++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 24 deletions(-) (limited to 'gltfview/src/plugins/gltf_view.c') diff --git a/gltfview/src/plugins/gltf_view.c b/gltfview/src/plugins/gltf_view.c index c3457a8..c19d1b8 100644 --- a/gltfview/src/plugins/gltf_view.c +++ b/gltfview/src/plugins/gltf_view.c @@ -1,6 +1,7 @@ -#include "gltf_view.h" +#include "plugin.h" #include +#include #include #include #include @@ -23,6 +24,11 @@ static const char* GIRL = #define DEFAULT_SCENE_FILE GIRL +struct State { + Scene* scene; + SceneCamera* camera; +}; + /// Load the skyquad texture. static Texture* load_environment_map(RenderBackend* render_backend) { return gfx_load_texture( @@ -57,15 +63,17 @@ static SceneNode* load_skyquad(RenderBackend* render_backend, SceneNode* root) { } /// Load the 3D scene. -static SceneNode* load_scene(Game* game, const char* scene_filepath) { +static SceneNode* load_scene( + Game* game, State* state, const char* scene_filepath) { assert(game); assert(game->gfx); - assert(game->scene); + assert(state); + assert(state->scene); - SceneNode* root = gfx_get_scene_root(game->scene); + SceneNode* root = gfx_get_scene_root(state->scene); RenderBackend* render_backend = gfx_get_render_backend(game->gfx); - Camera* camera = gfx_get_camera_camera(game->camera); + Camera* camera = gfx_get_camera_camera(state->camera); spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2)); SceneNode* sky_light_node = load_skyquad(render_backend, root); @@ -85,16 +93,20 @@ static SceneNode* load_scene(Game* game, const char* scene_filepath) { return scene_node; } -State* init(Game* game) { +bool init(Game* game, State** pp_state) { assert(game); State* state = calloc(1, sizeof(State)); - return state; -} + if (!state) { + goto cleanup; + } -bool boot(State* state, Game* game) { - assert(state); - assert(game); + if (!(state->scene = gfx_make_scene())) { + goto cleanup; + } + if (!(state->camera = gfx_make_camera())) { + goto cleanup; + } const int argc = game->argc; const char** argv = game->argv; @@ -102,27 +114,44 @@ bool boot(State* state, Game* game) { // Usage: const char* scene_filepath = argc > 1 ? argv[1] : DEFAULT_SCENE_FILE; - SceneNode* node = load_scene(game, scene_filepath); + SceneNode* node = load_scene(game, state, scene_filepath); if (!node) { - return false; + goto cleanup; } Anima* anima = gfx_get_node_anima(node); gfx_play_animation( anima, &(AnimationPlaySettings){.name = "Walk", .loop = true}); + *pp_state = state; return true; + +cleanup: + shutdown(game, state); + if (state) { + free(state); + } + return false; } -void update(State* state, Game* game, double t, double dt) { - assert(state); +void shutdown(Game* game, State* state) { + assert(game); + if (state) { + gfx_destroy_camera(&state->camera); + gfx_destroy_scene(&state->scene); + // State freed by plugin engine. + } +} + +void update(Game* game, State* state, double t, double dt) { assert(game); - assert(game->scene); - assert(game->camera); + assert(state); + assert(state->scene); + assert(state->camera); - gfx_animate_scene(game->scene, (R)t); + gfx_animate_scene(state->scene, (R)t); const vec3 orbit_point = vec3_make(0, 2, 0); - Camera* camera = gfx_get_camera_camera(game->camera); + Camera* camera = gfx_get_camera_camera(state->camera); spatial3_orbit( &camera->spatial, orbit_point, /*radius=*/2.5, @@ -150,18 +179,18 @@ static void render_bounding_boxes(ImmRenderer* imm, const SceneNode* node) { } } -void render(State* state, const Game* game) { +void render(const Game* game, const State* state) { assert(state); assert(game); assert(game->gfx); - assert(game->scene); - assert(game->camera); + assert(state->scene); + assert(state->camera); ImmRenderer* imm = gfx_get_imm_renderer(game->gfx); assert(imm); gfx_imm_start(imm); - gfx_imm_set_camera(imm, gfx_get_camera_camera(game->camera)); + gfx_imm_set_camera(imm, gfx_get_camera_camera(state->camera)); gfx_imm_set_colour(imm, vec4_make(0.2, 0.2, 1.0, 0.3)); - render_bounding_boxes(imm, gfx_get_scene_root(game->scene)); + render_bounding_boxes(imm, gfx_get_scene_root(state->scene)); gfx_imm_end(imm); } -- cgit v1.2.3