diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/env.c | 75 | ||||
| -rw-r--r-- | src/game.c | 15 | ||||
| -rw-r--r-- | src/input.c | 54 | ||||
| -rw-r--r-- | src/input.h | 31 |
4 files changed, 155 insertions, 20 deletions
@@ -146,6 +146,14 @@ static uint32_t env_strndup(struct w2c_env *env, char const *s, uint32_t sz) { return ptr; } +static uint32_t env_strload(struct w2c_env *env, uint32_t addr, uint32_t len, + char *buf, uint32_t sz) { + len = SDL_min(len, sz - 1); + wasm_rt_memory_load(&env->memory, addr, buf, len); + buf[len + 1] = '\0'; + return len; +} + static uint32_t env_ticks(void) { return (uint32_t)(SDL_GetTicks() & UINT32_MAX); } @@ -572,7 +580,6 @@ void w2c_env_0x5Fabort(struct w2c_env *env) { uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, uint32_t code) { - uint32_t i; switch (code) { case 0: // Whether or not there is embedded cart data. Since we _only_ run with @@ -581,11 +588,16 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, case 1: return (uint32_t)env->context.cartdat->count; case 2: - if ((i = env->data.cartpos / PW_ROM_SIZE) < env->context.cartdat->count) { - uint32_t offset = env->data.cartpos++ % PW_ROM_SIZE; - return env->context.cartdat->carts[i].rom[offset]; - } else { - return 0; + // Gets a byte from the embedded ROM data, note that we need to convert + // the byte position to a cart index and offset within that cart. + { + uint32_t i = env->data.cartpos / PW_ROM_SIZE; + if (i < env->context.cartdat->count) { + uint32_t offset = env->data.cartpos++ % PW_ROM_SIZE; + return env->context.cartdat->carts[i].rom[offset]; + } else { + return 0; + } } case 4: if (env->data.cartname.index < env->context.cartdat->count && @@ -629,6 +641,10 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // Used for writting drag and dropped PICO-8 carts. We do not support this // as a PICO-8 player, so pretend like there are none. return 0; + case 25: + // `FS.syncfs`: we need to implement a filesystem first, so lets just leave + // this unimplemented for now. + env_unimplemented("FS.syncfs"); default: env_badasm("i", code); } @@ -636,13 +652,26 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t code, uint32_t a) { - (void)env; - (void)a; + switch (code) { case 3: env->data.cartname.index = a; env->data.cartname.pos = 0; return 3; + case 22: + // Allocate the hostname, just use "localhost". AFAICT, the parameter is + // always 0 and not used for anything. + { + char localhost[] = "localhost"; + return env_strndup(env, localhost, sizeof(localhost)); + } + case 24: + // GPIO, we implement it in case some cart uses the GPIO values as an + // extra place to store data, or communicate across carts. + if (a >= PW_ENV_GPIO_SIZE) { + return 0; + } + return (uint32_t)env->data.gpio[a]; default: env_badasm("ii", code); } @@ -651,10 +680,22 @@ uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t code, uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env, uint32_t code, uint32_t a, uint32_t b) { - (void)env; - (void)a; - (void)b; switch (code) { + case 21: + // System messages, does not appear to be used. + { + char msg[256]; + env_strload(env, a, b, msg, sizeof(msg)); + SDL_LogDebug(0, "system message: %s", msg); + return 0; + } + case 23: + // GPIO, we implement it in case some cart uses the GPIO values as an + // extra place to store data, or communicate across carts. + if (a < PW_ENV_GPIO_SIZE) { + env->data.gpio[a] = (uint8_t)(b & 0xff); + } + return 0; default: env_badasm("iii", code); } @@ -666,14 +707,16 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env, uint32_t d) { (void)c; (void)d; - char name[256] = {0}; switch (code) { case 17: // PICO-8 can ask the browser to download a file for you - it looks like // for supporting exports. We ignore them. - wasm_rt_memory_load(&env->memory, a, name, SDL_min(b, sizeof(name) - 1)); - SDL_LogDebug(0, "ignoring attempt to download %s", name); - return 0; + { + char name[256]; + env_strload(env, a, b, name, sizeof(name)); + SDL_LogDebug(0, "ignoring attempt to download %s", name); + return 0; + } default: env_badasm("iiiii", code); } @@ -714,7 +757,7 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii( switch (code) { case 10: // Sync PICO-8 state, we only care about requesting pointer lock (so we - // can try to capture the mouse) or that shutdown is requested (so we can + // can try to lock the pointer) or that shutdown is requested (so we can // exit back to the cart selectio menu. env->data.state.pointerlock = f; env->data.state.shutdown = h; @@ -5,7 +5,8 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode, size_t memory) { - if (!renderer) { + SDL_Window *window; + if (!renderer || !(window = SDL_GetRenderWindow(renderer))) { SDL_InvalidParamError("renderer"); return NULL; } @@ -25,7 +26,7 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode, goto error; } - game->input = pw_input_init(); + game->input = pw_input_init(window); if (!game->input) { goto error; } @@ -161,9 +162,19 @@ bool pw_game_iterate(struct pw_game *game) { goto shutdown; } + if (!pw_input_set_pointer_lock(game->input, + game->env->data.state.pointerlock)) { + SDL_LogWarn(0, "failed to lock pointer: %s", SDL_GetError()); + game->env->data.state.pointerlock = pw_input_get_pointer_lock(game->input); + } + return true; shutdown: + if (!pw_input_set_pointer_lock(game->input, false)) { + SDL_LogWarn(0, "failed to reset pointer lock: %s", SDL_GetError()); + } + // TODO(nlordell): cleanly transition back to cart selection menu. return false; } diff --git a/src/input.c b/src/input.c index 66c8b2d..9e48729 100644 --- a/src/input.c +++ b/src/input.c @@ -1,14 +1,22 @@ #include <SDL3/SDL_log.h> +#include <SDL3/SDL_mouse.h> #include "input.h" -struct pw_input *pw_input_init(void) { +struct pw_input *pw_input_init(SDL_Window *window) { + if (!window) { + SDL_InvalidParamError("window"); + return NULL; + } + struct pw_input *input = SDL_malloc(sizeof(*input)); if (!input) { return NULL; } SDL_zerop(input); + input->window = window; + return input; } @@ -218,6 +226,19 @@ static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) { return true; } +static int32_t saturating_add(int32_t a, int32_t b) { + int64_t result = (int64_t)a + (int64_t)b; + return (int32_t)SDL_clamp(result, INT32_MIN, INT32_MAX); +} + +static bool handle_wheel(struct pw_input *input, SDL_MouseWheelEvent *wheel) { + input->mouse.wheel.dx = + saturating_add(input->mouse.wheel.dx, wheel->integer_x); + input->mouse.wheel.dy = + saturating_add(input->mouse.wheel.dy, wheel->integer_y); + return true; +} + bool pw_input_event(struct pw_input *input, struct pw_players *players, SDL_Event *event) { if (!input) { @@ -245,6 +266,8 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players, case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_UP: return handle_key(input, &event->key); + case SDL_EVENT_MOUSE_WHEEL: + return handle_wheel(input, &event->wheel); default: return true; } @@ -400,6 +423,27 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input, return input->mouse.button; } +void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy) { + static struct pw_mouse_wheel zero = {0}; + + struct pw_mouse_wheel *w; + if (input) { + w = &input->mouse.wheel; + } else { + SDL_InvalidParamError("input"); + w = &zero; + } + + if (dx) { + *dx = w->dx; + w->dx = 0; + } + if (dy) { + *dy = w->dy; + w->dy = 0; + } +} + bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { if (!input) { SDL_InvalidParamError("input"); @@ -442,3 +486,11 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { return false; } + +bool pw_input_set_pointer_lock(struct pw_input *input, bool lock) { + return SDL_SetWindowRelativeMouseMode(input->window, lock); +} + +bool pw_input_get_pointer_lock(struct pw_input *input) { + return SDL_GetWindowRelativeMouseMode(input->window); +} diff --git a/src/input.h b/src/input.h index a4927a3..d266977 100644 --- a/src/input.h +++ b/src/input.h @@ -69,10 +69,19 @@ enum pw_mouse_button { }; /** + * PICO-8 mouse wheel motion. + */ +struct pw_mouse_wheel { + int32_t dx; + int32_t dy; +}; + +/** * The PICO-8 mouse state. */ struct pw_mouse { enum pw_mouse_button button; + struct pw_mouse_wheel wheel; struct pw_coordinates pos; int32_t xrel; int32_t yrel; @@ -111,6 +120,7 @@ union pw_input_event { * PICO-8 input. */ struct pw_input { + SDL_Window *window; size_t nplayers; enum pw_button players[PW_PLAYERS_MAX]; struct pw_keyboard keyboard; @@ -120,7 +130,7 @@ struct pw_input { /** * Allocates and initializes the PICO-8 input state. */ -struct pw_input *pw_input_init(void); +struct pw_input *pw_input_init(SDL_Window *window); /** * Cleanup and free the PICO-8 input state. @@ -160,8 +170,27 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input, struct pw_coordinates *pos); /** + * Gets the accumulated mouse wheel change, resetting the accumulator. + * + * Can specify `NULL` to ignore coordinate. + */ +void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy); + +/** * Polls for the next input event for the PICO-8 engine. */ bool pw_input_poll(struct pw_input *input, union pw_input_event *event); +/** + * Sets the mouse pointer lock. + * + * Returns `false` in case of an error. + */ +bool pw_input_set_pointer_lock(struct pw_input *input, bool lock); + +/** + * Gets whether or not the pointer is currently locked. + */ +bool pw_input_get_pointer_lock(struct pw_input *input); + #endif |
