From d130c2c7314cf4884f444db16d7717b7356b54ff Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 8 Feb 2024 07:58:22 -0800 Subject: Add window width and height to game. --- game/src/game.c | 10 ++++++++++ game/src/game.h | 2 ++ game/src/plugins/gltf_view.c | 16 +++++++++++++++- game/src/plugins/plugin.h | 2 +- game/src/plugins/texture_view.c | 3 --- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/game/src/game.c b/game/src/game.c index 64be4f3..c720656 100644 --- a/game/src/game.c +++ b/game/src/game.c @@ -197,6 +197,10 @@ void app_update(Game* game, double t, double dt) { shutdown_plugin(game); const bool result = init_plugin(game); assert(result); // TODO: handle error better. + + // Trigger a resize just like the initial resize that occurs when the gfx + // application starts. + resize_plugin(game, game->width, game->height); } update_plugin(game, t, dt); @@ -210,6 +214,12 @@ void app_render(const Game* game) { } void app_resize(Game* game, int width, int height) { + game->width = width; + game->height = height; + + RenderBackend* render_backend = gfx_get_render_backend(game->gfx); + gfx_set_viewport(render_backend, width, height); + resize_plugin(game, width, height); } diff --git a/game/src/game.h b/game/src/game.h index 93a5e39..579ba3c 100644 --- a/game/src/game.h +++ b/game/src/game.h @@ -16,4 +16,6 @@ typedef struct { PluginEngine* plugin_engine; Plugin* plugin; Gfx* gfx; + int width; + int height; } Game; diff --git a/game/src/plugins/gltf_view.c b/game/src/plugins/gltf_view.c index c19d1b8..4ffdd1e 100644 --- a/game/src/plugins/gltf_view.c +++ b/game/src/plugins/gltf_view.c @@ -169,7 +169,7 @@ static void render_bounding_boxes(ImmRenderer* imm, const SceneNode* node) { const SceneObject* obj = gfx_get_node_object(node); const aabb3 box = gfx_calc_object_aabb(obj); gfx_imm_set_model_matrix(imm, &model); - gfx_imm_draw_aabb(imm, box); + gfx_imm_draw_aabb3(imm, box); } // Render children's boxes. @@ -194,3 +194,17 @@ void render(const Game* game, const State* state) { render_bounding_boxes(imm, gfx_get_scene_root(state->scene)); gfx_imm_end(imm); } + +void resize(Game* game, State* state, int width, int height) { + assert(game); + assert(state); + + const R fovy = 90 * TO_RAD; + const R aspect = (R)width / (R)height; + const R near = 0.1; + const R far = 1000; + const mat4 projection = mat4_perspective(fovy, aspect, near, far); + + Camera* camera = gfx_get_camera_camera(state->camera); + camera->projection = projection; +} diff --git a/game/src/plugins/plugin.h b/game/src/plugins/plugin.h index a2632cd..f7219c6 100644 --- a/game/src/plugins/plugin.h +++ b/game/src/plugins/plugin.h @@ -41,7 +41,7 @@ void update(Game*, State*, double t, double dt); void render(const Game*, const State*); /// Called when the game's window is resized. -void resize(Game* game, State* state, int width, int height); +void resize(Game*, State*, int width, int height); // Signatures for the plugin's exposed functions. typedef bool (*plugin_init)(Game*, State**); diff --git a/game/src/plugins/texture_view.c b/game/src/plugins/texture_view.c index b424158..31bf23e 100644 --- a/game/src/plugins/texture_view.c +++ b/game/src/plugins/texture_view.c @@ -133,9 +133,6 @@ void resize(Game* game, State* state, int width, int height) { assert(game); assert(state); - RenderBackend* render_backend = gfx_get_render_backend(game->gfx); - gfx_set_viewport(render_backend, width, height); - const R fovy = 90 * TO_RAD; const R aspect = (R)width / (R)height; const R near = 0.1; -- cgit v1.2.3