From 6ce281aa019bef35c81261e328e33948907472c6 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 29 Jun 2025 09:30:38 -0700 Subject: Tidy camera control --- src/plugins/viewer.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/plugins/viewer.c b/src/plugins/viewer.c index 1a27f8f..84fcca4 100644 --- a/src/plugins/viewer.c +++ b/src/plugins/viewer.c @@ -51,6 +51,8 @@ typedef struct CameraCommand { bool CameraMoveRight : 1; bool CameraMoveForward : 1; bool CameraMoveBackward : 1; + bool CameraIsRotating : 1; // When true, subsequent mouse movements cause the + // camera to rotate. } CameraCommand; typedef struct CameraController { @@ -58,8 +60,6 @@ typedef struct CameraController { R mouse_sensitivity; // Controls the degree with which mouse movements // rotate the camera. vec2 prev_mouse_position; // Mouse position in the previous frame. - bool rotating; // When true, subsequent mouse movements cause the - // camera to rotate. } CameraController; typedef struct State { @@ -197,6 +197,16 @@ void shutdown(Game* game, State* state) { } } +static CameraCommand make_camera_command_from_input() { + return (CameraCommand){ + .CameraMoveLeft = gfx_app_is_key_pressed(KeyA), + .CameraMoveRight = gfx_app_is_key_pressed(KeyD), + .CameraMoveForward = gfx_app_is_key_pressed(KeyW), + .CameraMoveBackward = gfx_app_is_key_pressed(KeyS), + .CameraIsRotating = gfx_app_is_mouse_button_pressed(LMB), + }; +} + static void update_camera( CameraController* controller, R dt, vec2 mouse_position, CameraCommand command, Spatial3* camera) { @@ -214,7 +224,7 @@ static void update_camera( spatial3_move_forwards(camera, translation.y); // Rotation. - if (controller->rotating) { + if (command.CameraIsRotating) { const vec2 mouse_delta = vec2_sub(mouse_position, controller->prev_mouse_position); @@ -239,15 +249,7 @@ void update(Game* game, State* state, double t, double dt) { gfx_app_get_mouse_position(&mouse_x, &mouse_y); const vec2 mouse_position = {(R)mouse_x, (R)mouse_y}; - const CameraCommand camera_command = (CameraCommand){ - .CameraMoveLeft = gfx_app_is_key_pressed(KeyA), - .CameraMoveRight = gfx_app_is_key_pressed(KeyD), - .CameraMoveForward = gfx_app_is_key_pressed(KeyW), - .CameraMoveBackward = gfx_app_is_key_pressed(KeyS), - }; - - state->camera_controller.rotating = gfx_app_is_mouse_button_pressed(LMB); - + const CameraCommand camera_command = make_camera_command_from_input(); update_camera( &state->camera_controller, (R)dt, mouse_position, camera_command, &gfx_get_camera_camera(state->camera)->spatial); -- cgit v1.2.3