diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-28 15:28:33 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-28 15:28:33 +0100 |
| commit | 756750924357cae5ea9df48af3f55e17a2fd90e2 (patch) | |
| tree | 7283448ffbe14bfcb924b2fb513dd4ee2a957e69 /src/input.c | |
| parent | 85db6937aa2da1605d299e9b3327f10d2dfc889e (diff) | |
Fix Relative Mouse Motion
We do some fixes, namely fixing relative mouse motion which worked
very differently compared to mouse motion outside of pointer lock.
This appears to be something built in to PICO-8 itself (as evident by
keeping track of `STAT(32-33,38-39)` under pointer lock: PICO-8 scales
relative mouse motion by a factor of `.25` before adding to the
absolute mouse position.
Additionally, we support running PICO-w on systems with no audio
playback.
Diffstat (limited to 'src/input.c')
| -rw-r--r-- | src/input.c | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/input.c b/src/input.c index 93aed4e..945c798 100644 --- a/src/input.c +++ b/src/input.c @@ -366,8 +366,8 @@ static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) { struct pw_coordinates pos; struct pw_coordinates rel; - if (!pw_fb_coordinates(fb, x, y, &pos) || - !pw_fb_coordinates(fb, xrel, yrel, &rel)) { + if (!pw_fb_coordinates(fb, x, y, false, &pos) || + !pw_fb_coordinates(fb, xrel, yrel, true, &rel)) { return false; } @@ -390,8 +390,12 @@ static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) { input->mouse.pos.x = SDL_clamp(pos.x, 0, PW_FB_WIDTH - 1); input->mouse.pos.y = SDL_clamp(pos.y, 0, PW_FB_HEIGHT - 1); - input->mouse.rel.x += rel.x; - input->mouse.rel.y += rel.y; + // PICO-8 reports relative mouse motion in different units than the + // absolute mouse positions. In paricular, it seems to expect a scale of + // 4: under pointer lock, PICO-8 will increase `STAT(32)` by 1 for every + // `STAT(38)` moved by 4. Convert the units. + input->mouse.rel.x += rel.x * PW_MOUSE_RELATIVE_SCALE; + input->mouse.rel.y += rel.y * PW_MOUSE_RELATIVE_SCALE; return true; } @@ -555,7 +559,14 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { event->mouse.xrel = (int32_t)input->mouse.rel.x; event->mouse.yrel = (int32_t)input->mouse.rel.y; - SDL_zerop(&input->mouse.rel); + // Perserve inter-frame fractional mouse movements. This matters + // because the FB pixels are very big, so moving your mouse slowly + // (less than a single pixel per frame) would effectively cause it + // to not move over time. + float unused; + input->mouse.rel.x = SDL_modff(input->mouse.rel.x, &unused); + input->mouse.rel.y = SDL_modff(input->mouse.rel.y, &unused); + return true; } @@ -563,6 +574,14 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { } bool pw_input_set_pointer_lock(struct pw_input *input, bool lock) { + // TODO(nlordell): Unfortunately, we don't have access to PICO-8's exact + // mouse position. Since it compute it internally based on relative mouse + // movement, and because of different clamping and rounding semantics, it + // is possible (i.e. likely) that we "jump" positions by a few coordinates + // when exiting pointer lock mode, as it will start reading the system's + // mouse coordinates which will disagree with its internal values. If we + // did have PICO-8's mouse coordinates somehow, we should first warp the + // mouse with `SDL_WarpMouseInWindow` to fix the jump. return SDL_SetWindowRelativeMouseMode(input->window, lock); } |
