aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-15 09:05:16 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-15 09:05:16 +0200
commitf97bb735065392d639462e90d9abd1bb5f738c61 (patch)
tree98b89f059fcd12aed31f94cf0bf4f55ece0a4f7f /src
parentb5cc8d759a09ad2754efb671dc1af12d52fc542e (diff)
wire up SDL events
Diffstat (limited to 'src')
-rw-r--r--src/game.c144
-rw-r--r--src/game.h5
-rw-r--r--src/input.c132
-rw-r--r--src/input.h36
4 files changed, 292 insertions, 25 deletions
diff --git a/src/game.c b/src/game.c
index 9d9f946..c29f0d3 100644
--- a/src/game.c
+++ b/src/game.c
@@ -1,6 +1,7 @@
#include <math.h>
#include <SDL3/SDL_log.h>
+#include <SDL3/SDL_timer.h>
#include "game.h"
#include "input.h"
@@ -58,13 +59,15 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
native_raw_get_arg(int32_t, bpp, args);
native_raw_get_arg(int32_t, flags, args);
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+
if (w != 128 || h != 128 || bpp != 32 || flags != 0) {
+ SDL_LogWarn(0, "invalid PICO-8 surface parameters");
+ game->error.set = true;
native_raw_set_return(0);
return;
}
- struct pw_game *game = wasm_runtime_get_user_data(exec);
-
// We need to allocate 60 bytes for the surface structure, 44 bytes for the
// pixel format, and `PW_FB_SIZE` bytes for the pixel data.
uint32_t surface;
@@ -74,6 +77,7 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
uint32_t margs[] = {size};
if (!wasm_runtime_call_wasm(exec, game->malloc, 1, margs) || !margs[0]) {
SDL_LogWarn(0, "failed to allocate surface");
+ game->error.set = true;
native_raw_set_return(0);
return;
}
@@ -82,6 +86,7 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
memory = emrt_memory(exec, surface, size);
if (!memory) {
SDL_LogWarn(0, "invalid surface allocation");
+ game->error.set = true;
native_raw_set_return(0);
return;
}
@@ -140,8 +145,8 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
static void emrt_get_mouse_state(wasm_exec_env_t exec, uint64_t *args) {
native_raw_return_type(int32_t, args);
- native_raw_get_arg(int32_t, x, args);
- native_raw_get_arg(int32_t, y, args);
+ native_raw_get_arg(uint32_t, x, args);
+ native_raw_get_arg(uint32_t, y, args);
struct pw_game *game = wasm_runtime_get_user_data(exec);
@@ -166,10 +171,14 @@ static void emrt_get_mouse_state(wasm_exec_env_t exec, uint64_t *args) {
static void emrt_lock_surface(wasm_exec_env_t exec, uint64_t *args) {
native_raw_return_type(int32_t, args);
- native_raw_get_arg(int32_t, surface, args);
+ native_raw_get_arg(uint32_t, surface, args);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
uint8_t *memory = emrt_memory(exec, surface, 60);
if (!memory) {
+ SDL_LogWarn(0, "invalid surface specified for locking");
+ game->error.set = true;
native_raw_set_return(-1);
return;
}
@@ -183,12 +192,14 @@ static void emrt_lock_surface(wasm_exec_env_t exec, uint64_t *args) {
}
static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
- native_raw_get_arg(int32_t, surface, args);
+ native_raw_get_arg(uint32_t, surface, args);
struct pw_game *game = wasm_runtime_get_user_data(exec);
uint8_t *memory = emrt_memory(exec, surface, 60);
if (!memory) {
+ SDL_LogWarn(0, "invalid surface specified for locking");
+ game->error.set = true;
return;
}
@@ -203,12 +214,111 @@ static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
uint8_t *buffer = emrt_memory(exec, pixels, PW_FB_SIZE);
if (!buffer) {
+ SDL_LogWarn(0, "invalid surface pixel buffer");
+ game->error.set = true;
return;
}
if (!pw_fb_blit(game->fb, buffer)) {
SDL_LogWarn(0, "failed to blit surface to framebuffer: %s", SDL_GetError());
+ game->error.set = true;
+ }
+}
+
+static uint32_t truncated_ticks() {
+ return (uint32_t)(SDL_GetTicks() & 0xffffffff);
+}
+
+static void emrt_get_ticks(wasm_exec_env_t exec, uint64_t *args) {
+ (void)exec;
+ native_raw_return_type(uint32_t, args);
+ native_raw_set_return(truncated_ticks());
+}
+
+static void emrt_get_error(wasm_exec_env_t exec, uint64_t *args) {
+ native_raw_return_type(uint32_t, args);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+
+ uint32_t error = game->error.ptr;
+ if (!game->error.set) {
+ // Just point to the `\0` at the end of the error message, this avoids
+ // requiring a separate allocation for the cleared error.
+ error = game->error.ptr;
}
+
+ native_raw_set_return(error);
+}
+
+static void emrt_clear_error(wasm_exec_env_t exec, uint64_t *args) {
+ (void)args;
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+ game->error.set = false;
+}
+
+#define EMRT_KEYDOWN 0x300
+#define EMRT_KEYUP 0x301
+#define EMRT_MOUSEMOTION 0x400
+
+static void emrt_poll_event(wasm_exec_env_t exec, uint64_t *args) {
+ native_raw_return_type(int32_t, args);
+ native_raw_get_arg(uint32_t, event, args);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+
+ bool remaining;
+ union pw_input_event input;
+ if (event) {
+ remaining = pw_input_poll(game->input, &input);
+ } else {
+ remaining = pw_input_poll(game->input, NULL);
+ goto exit;
+ }
+
+ uint8_t *memory = emrt_memory(exec, event, 36);
+ if (!memory) {
+ SDL_LogWarn(0, "invalid event address for polling");
+ game->error.set = true;
+ goto exit;
+ }
+
+ if (remaining) {
+ void *cursor = memory;
+
+ switch (input.type) {
+ case PW_INPUT_EVENT_KEY:
+ if (input.key.pressed) {
+ emrt_write(&cursor, uint32_t, EMRT_KEYDOWN);
+ } else {
+ emrt_write(&cursor, uint32_t, EMRT_KEYUP);
+ }
+ emrt_write(&cursor, uint32_t, 1); // windowID
+ emrt_write(&cursor, uint8_t, (uint8_t)input.key.pressed);
+ emrt_write(&cursor, uint8_t, (uint8_t)input.key.repeat);
+ emrt_write(&cursor, uint8_t, 0); // padding2
+ emrt_write(&cursor, uint8_t, 0); // padding3
+ emrt_write(&cursor, uint32_t, input.key.scancode);
+ emrt_write(&cursor, int32_t, input.key.keycode);
+ emrt_write(&cursor, uint16_t, (uint16_t)input.key.mod);
+ emrt_write(&cursor, uint16_t, 0); // keysym.(padding)
+ emrt_write(&cursor, uint32_t, 0); // keysym.unicode (deprecated)
+ break;
+ case PW_INPUT_EVENT_MOUSE:
+ emrt_write(&cursor, uint32_t, EMRT_MOUSEMOTION);
+ emrt_write(&cursor, uint32_t, truncated_ticks());
+ emrt_write(&cursor, uint32_t, 1); // windowID
+ emrt_write(&cursor, uint32_t, 1); // which
+ emrt_write(&cursor, uint32_t, (uint32_t)input.mouse.button);
+ emrt_write(&cursor, int32_t, input.mouse.x);
+ emrt_write(&cursor, int32_t, input.mouse.y);
+ emrt_write(&cursor, int32_t, input.mouse.xrel);
+ emrt_write(&cursor, int32_t, input.mouse.yrel);
+ break;
+ };
+ };
+
+exit:
+ native_raw_set_return((int32_t)remaining);
}
struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode) {
@@ -248,8 +358,12 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode) {
struct NativeSymbol env[] = {
{"_emscripten_set_main_loop", emrt_set_main_loop, "(iii)", NULL},
+ {"_SDL_ClearError", emrt_clear_error, "()", NULL},
+ {"_SDL_GetError", emrt_get_error, "()i", NULL},
{"_SDL_GetMouseState", emrt_get_mouse_state, "(ii)i", NULL},
+ {"_SDL_GetTicks", emrt_get_ticks, "()i", NULL},
{"_SDL_LockSurface", emrt_lock_surface, "(i)i", NULL},
+ {"_SDL_PollEvent", emrt_poll_event, "(i)i", NULL},
{"_SDL_SetVideoMode", emrt_set_video_mode, "(iiii)i", NULL},
{"_SDL_UnlockSurface", emrt_unlock_surface, "(i)", NULL},
};
@@ -400,6 +514,24 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
SDL_memcpy(target, value, sizeof(value));
}
+ {
+ char const value[] = "unspecified error";
+ uint32_t args[] = {(uint32_t)sizeof(value)};
+ if (!wasm_runtime_call_wasm(game->exec, malloc, 1, args)) {
+ return SDL_SetError("error allocating SDL error string: %s",
+ wasm_runtime_get_exception(game->instance));
+ }
+
+ game->error.ptr = args[0];
+ game->error.len = sizeof(value) - 1;
+ char *target = emrt_memory(game->exec, game->error.ptr, game->error.len);
+ if (!target) {
+ return SDL_SetError("invalid SDL error string address");
+ }
+
+ SDL_memcpy(target, value, sizeof(value));
+ }
+
uint32_t argv;
{
uint32_t const value[] = {arg0, 0};
diff --git a/src/game.h b/src/game.h
index 4c21c20..2ad449d 100644
--- a/src/game.h
+++ b/src/game.h
@@ -26,6 +26,11 @@ struct pw_game {
wasm_function_inst_t dyncall;
wasm_function_inst_t mix;
int32_t mainfn;
+ struct {
+ bool set;
+ uint32_t ptr;
+ uint32_t len;
+ } error;
};
/**
diff --git a/src/input.c b/src/input.c
index fb3e55a..412cf88 100644
--- a/src/input.c
+++ b/src/input.c
@@ -125,7 +125,7 @@ static bool handle_button(struct pw_input *input, struct pw_players *players,
return true;
}
-static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
+static bool enqueue_item(struct pw_keyboard_queue *queue, int32_t item) {
size_t next = (queue->end + 1) % PW_KEYBOARD_BUFFER_SIZE;
if (next == queue->start) {
return false;
@@ -136,6 +136,16 @@ static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
return true;
}
+static bool dequeue_item(struct pw_keyboard_queue *queue, int32_t *item) {
+ if (queue->start == queue->end) {
+ return false;
+ }
+
+ *item = queue->items[queue->start];
+ queue->start = (queue->start + 1) % PW_KEYBOARD_BUFFER_SIZE;
+ return true;
+}
+
static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
switch (sc) {
case SDL_SCANCODE_TAB:
@@ -167,22 +177,36 @@ static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
return (int8_t)keycode;
}
+// SDL has a `SDL_SCANCODE_COUNT` of 512, so we can mask the scan code with
+// `512 - 1 = 0x1ff`.
+#define PW_SCANCODE_MASK 0x1ff
+#define PW_SCANCODE_PRESSED 0x80000000
+#define PW_SCANCODE_REPEAT 0x40000000
+#define PW_SCANCODE_MOD_SHIFT 9
+
static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) {
- if (event->scancode < 0 || event->scancode > INT16_MAX) {
+ if (event->scancode < 0 || event->scancode > PW_SCANCODE_MASK) {
return false;
}
- int16_t scancode = (int16_t)event->scancode;
- if (!event->down) {
- scancode = -scancode;
+ uint32_t sc = (uint32_t)event->scancode;
+ if (event->down) {
+ sc |= PW_SCANCODE_PRESSED;
+ }
+ if (event->repeat) {
+ sc |= PW_SCANCODE_REPEAT;
}
- if (!enqueue_item(&input->keyboard.scancodes, scancode)) {
+ sc |= (uint32_t)event->mod << PW_SCANCODE_MOD_SHIFT;
+
+ if (!enqueue_item(&input->keyboard.scancodes, (int32_t)sc)) {
SDL_LogDebug(0, "keyboard event queue full, dropping scancode");
}
- int16_t key = (int16_t)scancode_to_p8scii(event->scancode, event->mod);
- if (!enqueue_item(&input->keyboard.text, key)) {
- SDL_LogDebug(0, "keyboard text queue full, dropping key");
+ if (event->down) {
+ int8_t key = scancode_to_p8scii(event->scancode, event->mod);
+ if (!enqueue_item(&input->keyboard.text, (int32_t)key)) {
+ SDL_LogDebug(0, "keyboard text queue full, dropping key %d", key);
+ }
}
return true;
@@ -217,28 +241,57 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players,
}
}
-static void update_keymod(struct pw_input *input) {
- input->keyboard.mod = PW_KEYMOD_NONE;
+static uint32_t convert_scancode(SDL_Scancode scancode) {
+ // Some of the scancodes have slightly different numbering.
+ switch (scancode) {
+ // TODO: remap scancodes that are different in case we run into any issues.
+ default:
+ return scancode;
+ }
+}
+
+static int32_t convert_keycode(SDL_Scancode scancode, SDL_Keymod mod) {
+ SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, mod, true);
+ if (keycode & SDLK_EXTENDED_MASK) {
+ // Not supported in older SDL versions.
+ return 0;
+ } else if (keycode & SDLK_SCANCODE_MASK) {
+ // Scancode keys are encoded differently by Emscripten, so imitate that
+ // here:
+ return (int32_t)(keycode & ~SDLK_SCANCODE_MASK) + 1024;
+ } else {
+ return (int32_t)keycode;
+ }
+}
+
+static enum pw_keymod convert_keymod_state(SDL_Keymod state) {
+ enum pw_keymod mod = PW_KEYMOD_NONE;
- SDL_Keymod state = SDL_GetModState();
if (state & SDL_KMOD_LSHIFT) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ mod |= PW_KEYMOD_LSHIFT;
}
if (state & SDL_KMOD_RSHIFT) {
- input->keyboard.mod |= PW_KEYMOD_RSHIFT;
+ mod |= PW_KEYMOD_RSHIFT;
}
if (state & SDL_KMOD_LCTRL) {
- input->keyboard.mod |= PW_KEYMOD_LCTRL;
+ mod |= PW_KEYMOD_LCTRL;
}
if (state & SDL_KMOD_RCTRL) {
- input->keyboard.mod |= PW_KEYMOD_RCTRL;
+ mod |= PW_KEYMOD_RCTRL;
}
if (state & SDL_KMOD_LALT) {
- input->keyboard.mod |= PW_KEYMOD_LALT;
+ mod |= PW_KEYMOD_LALT;
}
if (state & SDL_KMOD_RALT) {
- input->keyboard.mod |= PW_KEYMOD_RALT;
+ mod |= PW_KEYMOD_RALT;
}
+
+ return mod;
+}
+
+static void update_keymod(struct pw_input *input) {
+ SDL_Keymod state = SDL_GetModState();
+ input->keyboard.mod = convert_keymod_state(state);
}
static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
@@ -327,3 +380,46 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input,
}
return input->mouse.button;
}
+
+bool pw_input_poll(struct pw_input *input, union pw_input_event *event) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return false;
+ }
+
+ 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 item;
+ if (dequeue_item(&input->keyboard.scancodes, &item)) {
+ uint32_t sc = (uint32_t)item;
+ SDL_Scancode scancode = (SDL_Scancode)(sc & PW_SCANCODE_MASK);
+ SDL_Keymod mod = (SDL_Keymod)(sc >> PW_SCANCODE_MOD_SHIFT);
+
+ event->type = PW_INPUT_EVENT_KEY;
+ event->key.pressed = sc & PW_SCANCODE_PRESSED;
+ event->key.repeat = sc & PW_SCANCODE_REPEAT;
+ event->key.scancode = convert_scancode(scancode);
+ event->key.keycode = convert_keycode(scancode, mod);
+ event->key.mod = convert_keymod_state(mod);
+
+ return true;
+ }
+
+ if (input->mouse.xrel || input->mouse.yrel) {
+ 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;
+
+ input->mouse.xrel = input->mouse.yrel = 0;
+ return true;
+ }
+
+ return false;
+}
diff --git a/src/input.h b/src/input.h
index 089c4be..4456ece 100644
--- a/src/input.h
+++ b/src/input.h
@@ -42,7 +42,7 @@ enum pw_keymod {
* A queue used for storing queued key events and text input.
*/
struct pw_keyboard_queue {
- int16_t items[PW_KEYBOARD_BUFFER_SIZE];
+ int32_t items[PW_KEYBOARD_BUFFER_SIZE];
size_t start;
size_t end;
};
@@ -79,6 +79,35 @@ struct pw_mouse {
};
/**
+ * A PICO-8 input event.
+ *
+ * This is only a subset of SDL events that the PICO-8 engine uses.
+ */
+union pw_input_event {
+ enum pw_input_event_type {
+ PW_INPUT_EVENT_KEY,
+ PW_INPUT_EVENT_MOUSE,
+ } type;
+
+ struct pw_input_event_key {
+ enum pw_input_event_type type;
+ bool pressed;
+ bool repeat;
+ uint32_t scancode;
+ uint32_t keycode;
+ enum pw_keymod mod;
+ } key;
+ struct pw_input_event_mouse {
+ enum pw_input_event_type type;
+ enum pw_mouse_button button;
+ int32_t x;
+ int32_t y;
+ int32_t xrel;
+ int32_t yrel;
+ } mouse;
+};
+
+/**
* PICO-8 input.
*/
struct pw_input {
@@ -124,4 +153,9 @@ enum pw_keymod pw_input_keymod(struct pw_input *input);
enum pw_mouse_button pw_input_mouse(struct pw_input *input,
struct pw_coordinates *pos);
+/**
+ * Polls for the next input event for the PICO-8 engine.
+ */
+bool pw_input_poll(struct pw_input *input, union pw_input_event *event);
+
#endif