diff options
Diffstat (limited to 'src/input.c')
| -rw-r--r-- | src/input.c | 37 |
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; } |
