summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-06-29 09:30:38 -0700
committer3gg <3gg@shellblade.net>2025-06-29 09:30:38 -0700
commit6ce281aa019bef35c81261e328e33948907472c6 (patch)
tree68593f596cbc89758a93ecdf78b78bf8dd34f8b3 /src
parent03d94f3762ab576ba0675abcaefde888a9da2c3d (diff)
Tidy camera control
Diffstat (limited to 'src')
-rw-r--r--src/plugins/viewer.c26
1 files 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 {
51 bool CameraMoveRight : 1; 51 bool CameraMoveRight : 1;
52 bool CameraMoveForward : 1; 52 bool CameraMoveForward : 1;
53 bool CameraMoveBackward : 1; 53 bool CameraMoveBackward : 1;
54 bool CameraIsRotating : 1; // When true, subsequent mouse movements cause the
55 // camera to rotate.
54} CameraCommand; 56} CameraCommand;
55 57
56typedef struct CameraController { 58typedef struct CameraController {
@@ -58,8 +60,6 @@ typedef struct CameraController {
58 R mouse_sensitivity; // Controls the degree with which mouse movements 60 R mouse_sensitivity; // Controls the degree with which mouse movements
59 // rotate the camera. 61 // rotate the camera.
60 vec2 prev_mouse_position; // Mouse position in the previous frame. 62 vec2 prev_mouse_position; // Mouse position in the previous frame.
61 bool rotating; // When true, subsequent mouse movements cause the
62 // camera to rotate.
63} CameraController; 63} CameraController;
64 64
65typedef struct State { 65typedef struct State {
@@ -197,6 +197,16 @@ void shutdown(Game* game, State* state) {
197 } 197 }
198} 198}
199 199
200static CameraCommand make_camera_command_from_input() {
201 return (CameraCommand){
202 .CameraMoveLeft = gfx_app_is_key_pressed(KeyA),
203 .CameraMoveRight = gfx_app_is_key_pressed(KeyD),
204 .CameraMoveForward = gfx_app_is_key_pressed(KeyW),
205 .CameraMoveBackward = gfx_app_is_key_pressed(KeyS),
206 .CameraIsRotating = gfx_app_is_mouse_button_pressed(LMB),
207 };
208}
209
200static void update_camera( 210static void update_camera(
201 CameraController* controller, R dt, vec2 mouse_position, 211 CameraController* controller, R dt, vec2 mouse_position,
202 CameraCommand command, Spatial3* camera) { 212 CameraCommand command, Spatial3* camera) {
@@ -214,7 +224,7 @@ static void update_camera(
214 spatial3_move_forwards(camera, translation.y); 224 spatial3_move_forwards(camera, translation.y);
215 225
216 // Rotation. 226 // Rotation.
217 if (controller->rotating) { 227 if (command.CameraIsRotating) {
218 const vec2 mouse_delta = 228 const vec2 mouse_delta =
219 vec2_sub(mouse_position, controller->prev_mouse_position); 229 vec2_sub(mouse_position, controller->prev_mouse_position);
220 230
@@ -239,15 +249,7 @@ void update(Game* game, State* state, double t, double dt) {
239 gfx_app_get_mouse_position(&mouse_x, &mouse_y); 249 gfx_app_get_mouse_position(&mouse_x, &mouse_y);
240 const vec2 mouse_position = {(R)mouse_x, (R)mouse_y}; 250 const vec2 mouse_position = {(R)mouse_x, (R)mouse_y};
241 251
242 const CameraCommand camera_command = (CameraCommand){ 252 const CameraCommand camera_command = make_camera_command_from_input();
243 .CameraMoveLeft = gfx_app_is_key_pressed(KeyA),
244 .CameraMoveRight = gfx_app_is_key_pressed(KeyD),
245 .CameraMoveForward = gfx_app_is_key_pressed(KeyW),
246 .CameraMoveBackward = gfx_app_is_key_pressed(KeyS),
247 };
248
249 state->camera_controller.rotating = gfx_app_is_mouse_button_pressed(LMB);
250
251 update_camera( 253 update_camera(
252 &state->camera_controller, (R)dt, mouse_position, camera_command, 254 &state->camera_controller, (R)dt, mouse_position, camera_command,
253 &gfx_get_camera_camera(state->camera)->spatial); 255 &gfx_get_camera_camera(state->camera)->spatial);