aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/env.c4
-rw-r--r--src/fb.c36
-rw-r--r--src/fb.h9
-rw-r--r--src/input.c37
-rw-r--r--src/input.h3
-rw-r--r--src/scale.c26
-rw-r--r--src/scale.h2
7 files changed, 76 insertions, 41 deletions
diff --git a/src/env.c b/src/env.c
index fcf096a..934acac 100644
--- a/src/env.c
+++ b/src/env.c
@@ -287,10 +287,10 @@ uint32_t w2c_env_0x5FSDL_GetMouseState(struct w2c_env *env, uint32_t x,
enum pw_mouse_button button = pw_input_mouse(env->context.input, &pos);
if (x) {
- wasm_rt_memory_store32(&env->memory, x, pos.x);
+ wasm_rt_memory_store32(&env->memory, x, (int32_t)pos.x);
}
if (y) {
- wasm_rt_memory_store32(&env->memory, y, pos.y);
+ wasm_rt_memory_store32(&env->memory, y, (int32_t)pos.y);
}
return (uint32_t)button;
}
diff --git a/src/fb.c b/src/fb.c
index 5b7ce4b..4e351e8 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -1,5 +1,7 @@
#include "fb.h"
+static bool is_normal(float f) { return !SDL_isinff(f) && !SDL_isnanf(f); }
+
struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer,
enum pw_scale_mode mode) {
if (!renderer) {
@@ -56,30 +58,34 @@ void pw_fb_free(struct pw_framebuffer *fb) {
bool pw_fb_iterate(struct pw_framebuffer *fb) {
if (!fb) {
- SDL_InvalidParamError("fb");
- return false;
+ return SDL_InvalidParamError("fb");
}
return pw_scale(fb->mode, fb->texture, &fb->target);
}
-static uint8_t project_component(float r, float start, float size,
- uint8_t dim) {
- if (SDL_isnan(r) || SDL_isnan(start) || SDL_isnan(size) ||
- SDL_fabsf(size) < 1.) {
- return 0;
+static float project_component(float r, float start, float size, float dim) {
+ if (!(int32_t)size) {
+ return 0.;
}
- float dimf = (float)dim;
- float df = SDL_floorf(((r - start) * dimf) / size);
- return (uint8_t)SDL_clamp(df, 0.0, (float)(dim - 1));
+ float f = ((r - start) * dim) / size;
+ return SDL_clamp(f, -PW_MAX_SAFE_INT, PW_MAX_SAFE_INT);
}
bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
struct pw_coordinates *coords) {
if (!fb) {
- SDL_InvalidParamError("fb");
- return false;
+ return SDL_InvalidParamError("fb");
+ }
+ if (!is_normal(wx)) {
+ return SDL_InvalidParamError("wx");
+ }
+ if (!is_normal(wy)) {
+ return SDL_InvalidParamError("wy");
+ }
+ if (!coords) {
+ return SDL_InvalidParamError("coords");
}
SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
@@ -91,6 +97,9 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
if (!SDL_RenderCoordinatesFromWindow(renderer, wx, wy, &rx, &ry)) {
return false;
}
+ if (!is_normal(rx) || !is_normal(ry)) {
+ return SDL_SetError("unreasonable render coordinates");
+ }
coords->x = project_component(rx, fb->target.x, fb->target.w, PW_FB_WIDTH);
coords->y = project_component(ry, fb->target.y, fb->target.h, PW_FB_HEIGHT);
@@ -129,8 +138,7 @@ void pw_fb_unlock(struct pw_framebuffer *fb) {
bool pw_fb_render(struct pw_framebuffer *fb) {
if (!fb) {
- SDL_InvalidParamError("fb");
- return false;
+ return SDL_InvalidParamError("fb");
}
SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
diff --git a/src/fb.h b/src/fb.h
index 591f6a4..7e90208 100644
--- a/src/fb.h
+++ b/src/fb.h
@@ -23,8 +23,8 @@ struct pw_framebuffer {
* Framebuffer coordinates.
*/
struct pw_coordinates {
- uint8_t x;
- uint8_t y;
+ float x;
+ float y;
};
/**
@@ -44,7 +44,10 @@ void pw_fb_free(struct pw_framebuffer *fb);
bool pw_fb_iterate(struct pw_framebuffer *fb);
/**
- * Projects window coordinates
+ * Projects window coordinates to framebuffer coordinates.
+ *
+ * The resulting coordinate components are guaranteed to fit in a 32-bit
+ * integer, meaning it is safe to cast to `int32_t`.
*/
bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
struct pw_coordinates *coords);
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;
}
diff --git a/src/input.h b/src/input.h
index 94125ba..d521ce0 100644
--- a/src/input.h
+++ b/src/input.h
@@ -83,8 +83,7 @@ struct pw_mouse {
enum pw_mouse_button button;
struct pw_mouse_wheel wheel;
struct pw_coordinates pos;
- int32_t xrel;
- int32_t yrel;
+ struct pw_coordinates rel;
};
/**
diff --git a/src/scale.c b/src/scale.c
index f435395..637db86 100644
--- a/src/scale.c
+++ b/src/scale.c
@@ -1,15 +1,23 @@
#include "scale.h"
+static bool is_safei(int i) {
+ return i >= -PW_MAX_SAFE_INT && i <= PW_MAX_SAFE_INT;
+}
+
+static bool is_safef(float f) {
+ return f >= -PW_MAX_SAFE_INT && f <= PW_MAX_SAFE_INT && (int32_t)f != 0;
+}
+
static SDL_ScaleMode scale_pixel_perfect(float twf, float thf, int rw, int rh,
SDL_FRect *target) {
- int tw = (int)twf;
- int th = (int)thf;
- int scale = SDL_min(rw / tw, rh / th);
+ int32_t tw = (int32_t)twf;
+ int32_t th = (int32_t)thf;
+ int32_t scale = SDL_min(rw / tw, rh / th);
- int w = scale * tw;
- int h = scale * th;
- int x = (rw - w) / 2;
- int y = (rh - h) / 2;
+ int32_t w = scale * tw;
+ int32_t h = scale * th;
+ int32_t x = (rw - w) / 2;
+ int32_t y = (rh - h) / 2;
target->w = (float)w;
target->h = (float)h;
@@ -92,6 +100,10 @@ bool pw_scale(enum pw_scale_mode mode, SDL_Texture *texture,
return false;
}
+ if (!is_safef(twf) || !is_safef(thf) || !is_safei(rw) || !is_safei(rh)) {
+ return SDL_SetError("unreasonable texture or render sizes");
+ }
+
SDL_FRect t;
SDL_ScaleMode m = scalefn(twf, thf, rw, rh, &t);
if (!SDL_SetTextureScaleMode(texture, m)) {
diff --git a/src/scale.h b/src/scale.h
index 6294905..14c4d88 100644
--- a/src/scale.h
+++ b/src/scale.h
@@ -3,6 +3,8 @@
#include <SDL3/SDL_render.h>
+#define PW_MAX_SAFE_INT 16777215
+
/**
* Scaling mode to use for textures.
*/