aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/audio.c24
-rw-r--r--src/config.c1
-rw-r--r--src/env.c10
-rw-r--r--src/fb.c14
-rw-r--r--src/fb.h2
-rw-r--r--src/fs.c1
-rw-r--r--src/input.c29
-rw-r--r--src/input.h1
-rw-r--r--src/wasmrt.c4
-rw-r--r--src/wasmrt.h1
10 files changed, 54 insertions, 33 deletions
diff --git a/src/audio.c b/src/audio.c
index cbe5be3..b2ba701 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -3,6 +3,11 @@
#include "audio.h"
struct pw_audio *pw_audio_init(void) {
+ struct pw_audio *audio = SDL_calloc(1, sizeof(*audio));
+ if (!audio) {
+ return NULL;
+ }
+
// PICO-8 exposes a `_mix(len0, len1)` function for copying audio data
// from an internal buffer to the host's audio context. From the
// Emscripten bundle, we see a few things:
@@ -20,19 +25,12 @@ struct pw_audio *pw_audio_init(void) {
.channels = 1,
.freq = 22050,
};
- SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(
- SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
- if (!stream) {
- return NULL;
- }
-
- struct pw_audio *audio = SDL_calloc(1, sizeof(*audio));
- if (!audio) {
- SDL_DestroyAudioStream(stream);
- return NULL;
+ audio->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
+ &spec, NULL, NULL);
+ if (!audio->stream) {
+ SDL_LogWarn(0, "starting without audio");
}
- audio->stream = stream;
return audio;
}
@@ -196,8 +194,8 @@ bool pw_audio_unlock(struct pw_audio *audio) {
return SDL_InvalidParamError("audio");
}
- size_t i = audio->lock / PW_AUDIO_BUFFER_MAX;
- size_t len = audio->lock % PW_AUDIO_BUFFER_MAX;
+ size_t i = (audio->lock - 1) / PW_AUDIO_BUFFER_MAX;
+ size_t len = (audio->lock - 1) % PW_AUDIO_BUFFER_MAX + 1;
if (!SDL_LockAudioStream(audio->stream)) {
return false;
diff --git a/src/config.c b/src/config.c
index 91f45b8..c16f525 100644
--- a/src/config.c
+++ b/src/config.c
@@ -206,6 +206,7 @@ struct pw_config *pw_config_load(int argc, char **argv) {
config->mode = PW_SCALE_MODE_PIXEL_PERFECT;
config->memory = 128;
config->logp = SDL_GetLogPriority(0);
+ config->debug = false;
// Read default values from the `config.ini` file before parsing command line
// arguments, as they take priority.
diff --git a/src/env.c b/src/env.c
index 4882026..bac97e0 100644
--- a/src/env.c
+++ b/src/env.c
@@ -631,15 +631,11 @@ uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t which,
}
uint32_t flags = env_varg(env, ap, 1);
- SDL_Log("OPEN: %s", path);
-
int32_t fd;
if (!pw_fs_open(env->context.cart->storage, path, flags, &fd)) {
- SDL_Log("ERR: %d", -pw_fs_errno(env->context.cart->storage));
return -pw_fs_errno(env->context.cart->storage);
}
- SDL_Log("FD: %d", fd);
return fd;
}
@@ -660,8 +656,6 @@ uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t which,
int32_t fd = (int32_t)env_varg(env, ap, 0);
- SDL_Log("CLOSE %d", fd);
-
if (!pw_fs_close(env->context.cart->storage, fd)) {
return -pw_fs_errno(env->context.cart->storage);
}
@@ -769,12 +763,12 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
case 35: {
struct pw_coordinates pos;
pw_input_mouse(env->context.input, &pos);
- return pos.x;
+ return (int32_t)pos.x;
}
case 36: {
struct pw_coordinates pos;
pw_input_mouse(env->context.input, &pos);
- return pos.y;
+ return (int32_t)pos.y;
}
case 37:
return (uint32_t)pw_input_key(env->context.input, NULL);
diff --git a/src/fb.c b/src/fb.c
index 4e351e8..f73948a 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -74,7 +74,7 @@ static float project_component(float r, float start, float size, float dim) {
}
bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
- struct pw_coordinates *coords) {
+ bool relative, struct pw_coordinates *coords) {
if (!fb) {
return SDL_InvalidParamError("fb");
}
@@ -93,6 +93,14 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
return false;
}
+ float sx, sy;
+ if (relative) {
+ sx = sy = 0.;
+ } else {
+ sx = fb->target.x;
+ sy = fb->target.y;
+ }
+
float rx, ry;
if (!SDL_RenderCoordinatesFromWindow(renderer, wx, wy, &rx, &ry)) {
return false;
@@ -101,8 +109,8 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
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);
+ coords->x = project_component(rx, sx, fb->target.w, PW_FB_WIDTH);
+ coords->y = project_component(ry, sy, fb->target.h, PW_FB_HEIGHT);
return true;
}
diff --git a/src/fb.h b/src/fb.h
index 7e90208..93548ca 100644
--- a/src/fb.h
+++ b/src/fb.h
@@ -50,7 +50,7 @@ bool pw_fb_iterate(struct pw_framebuffer *fb);
* 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);
+ bool relative, struct pw_coordinates *coords);
/**
* Locks the framebuffer for writing.
diff --git a/src/fs.c b/src/fs.c
index 6553810..ca44675 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -183,6 +183,7 @@ struct pw_fs *pw_fs_init(char const *path) {
}
pw_tar_free(tar);
+ tar = NULL;
} else if (path) {
SDL_LogWarn(0, "could not open cart storage: %s", SDL_GetError());
} else {
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);
}
diff --git a/src/input.h b/src/input.h
index 261b9f3..d3b996a 100644
--- a/src/input.h
+++ b/src/input.h
@@ -9,6 +9,7 @@
#define PW_AXIS_THRESHOLD 9830
#define PW_TRIGGER_THRESHOLD 16384
#define PW_KEYBOARD_BUFFER_SIZE 256
+#define PW_MOUSE_RELATIVE_SCALE 4.0
/**
* PICO-8 window flags.
diff --git a/src/wasmrt.c b/src/wasmrt.c
index c75d897..2fda952 100644
--- a/src/wasmrt.c
+++ b/src/wasmrt.c
@@ -1,9 +1,7 @@
#include <stdlib.h>
-#include <SDL3/SDL_endian.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_log.h>
-#include <SDL3/SDL_stdinc.h>
#include "wasmrt.h"
@@ -164,7 +162,7 @@ void wasm_rt_memory_load16v_unchecked(struct wasm_rt_memory_t const *mem,
SDL_memcpy(values, mem->data + addr, sizeof(*values) * len);
#else
for (uint32_t i = 0; i < len; i++) {
- SDL_memcpy(values + i, mem->data_end - addr - sizeof(*values) * i,
+ SDL_memcpy(values + i, mem->data_end - addr - sizeof(*values) * (i + 1),
sizeof(*values));
}
#endif
diff --git a/src/wasmrt.h b/src/wasmrt.h
index ca1efdd..b0dcddc 100644
--- a/src/wasmrt.h
+++ b/src/wasmrt.h
@@ -3,6 +3,7 @@
#include <setjmp.h>
+#include <SDL3/SDL_endian.h>
#include <SDL3/SDL_stdinc.h>
#if SDL_HAS_BUILTIN(__builtin_expect)