aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-24 12:26:16 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-24 12:26:16 +0200
commitf43e306bf050b9f96be4b22382cacd96d86b5239 (patch)
treedb7e20c158a5f70869fdef9ce2db6f6a96b2cf75 /src/input.c
parent02da08e9d84371c558da6347210c00e3966f2dc5 (diff)
Improve Mouse Position Handling
After some playing around with devkit mode, I noticed two things: 1. The mouse pointer can be outside of the range [0. 128) in case the window is larger than the PICO-8 screen, but it does not move to positions that would be outside of the window 2. Under pointer lock, relative positions continue to be reported even when the mouse is stuck to the edge of the window This means our implementation was slightly wrong, as it it did not properly handle the second case in a few ways: - We would always round to integer relative mouse mouse motions, meaning we would incorrectly approximate in case we collected multiple frames of relative mouse motion - We would compute the relative motion based on the mouse position, which is incorrect under pointer lock (as the position doesn't change but PICO-8 still needs relative mouse motion) - We would reset relative mouse motion every frame, meaning that we would not correctly accumulate relative motion in case the host's refresh rate was faster than PICO-8's (which seems to only run its frame logic at 30 FPS). We fix that here.
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/input.c b/src/input.c
index faf4758..e082393 100644
--- a/src/input.c
+++ b/src/input.c
@@ -361,8 +361,13 @@ static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
float x, y;
SDL_MouseButtonFlags flags = SDL_GetMouseState(&x, &y);
- struct pw_coordinates new;
- if (!pw_fb_coordinates(fb, x, y, &new)) {
+ float xrel, yrel;
+ SDL_GetRelativeMouseState(&xrel, &yrel);
+
+ struct pw_coordinates pos;
+ struct pw_coordinates rel;
+ if (!pw_fb_coordinates(fb, x, y, &pos) ||
+ !pw_fb_coordinates(fb, xrel, yrel, &rel)) {
return false;
}
@@ -377,10 +382,16 @@ static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
input->mouse.button |= PW_MOUSE_BUTTON_RIGHT;
}
- input->mouse.xrel = (int32_t)new.x - (int32_t)input->mouse.pos.x;
- input->mouse.yrel = (int32_t)new.y - (int32_t)input->mouse.pos.y;
- input->mouse.pos.x = new.x;
- input->mouse.pos.y = new.y;
+ // PICO-8 computes coordinates based on the mouse window position (so,
+ // when you scale the PICO-8 window such that there are black borders,
+ // mouse movements within those borders correspond to positions
+ // outside of `[0, 128)`. Since we run PICO-8 pretending that it has
+ // an exact 128x128 window, clamp the mouse position to that.
+ 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;
return true;
}
@@ -483,7 +494,7 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) {
if (!event) {
// Just check whether or not there is queued event and return.
return input->keyboard.scancodes.start != input->keyboard.scancodes.end ||
- input->mouse.xrel || input->mouse.yrel;
+ (int32_t)input->mouse.rel.x || (int32_t)input->mouse.rel.y;
}
uint32_t sc;
@@ -501,15 +512,15 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) {
return true;
}
- if (input->mouse.xrel || input->mouse.yrel) {
+ if ((int32_t)input->mouse.rel.x || (int32_t)input->mouse.rel.y) {
event->type = PW_INPUT_EVENT_MOUSE;
event->mouse.button = input->mouse.button;
- event->mouse.x = input->mouse.pos.x;
- event->mouse.y = input->mouse.pos.y;
- event->mouse.xrel = input->mouse.xrel;
- event->mouse.yrel = input->mouse.yrel;
+ event->mouse.x = (int32_t)input->mouse.pos.x;
+ event->mouse.y = (int32_t)input->mouse.pos.y;
+ event->mouse.xrel = (int32_t)input->mouse.rel.x;
+ event->mouse.yrel = (int32_t)input->mouse.rel.y;
- input->mouse.xrel = input->mouse.yrel = 0;
+ SDL_zerop(&input->mouse.rel);
return true;
}