aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c246
1 files changed, 246 insertions, 0 deletions
diff --git a/src/env.c b/src/env.c
new file mode 100644
index 0000000..71858e3
--- /dev/null
+++ b/src/env.c
@@ -0,0 +1,246 @@
+#include <SDL3/SDL_error.h>
+#include <SDL3/SDL_stdinc.h>
+#include <SDL3/SDL_timer.h>
+
+#include "env.h"
+
+struct w2c_env *pw_env_init(size_t memory) {
+ if (memory < 24 || memory > 128) {
+ SDL_InvalidParamError("memory");
+ return NULL;
+ }
+ size_t pages = ((memory << 20) + wasm2c_pico8_pagesize_env_memory - 1) /
+ wasm2c_pico8_pagesize_env_memory;
+ size_t memory_size = pages * wasm2c_pico8_pagesize_env_memory;
+ struct w2c_env *env = SDL_malloc(sizeof(*env) + memory_size);
+ if (!env) {
+ return NULL;
+ }
+
+ env->memory.data = (uint8_t *)env + sizeof(*env);
+ env->memory.data_end = env->memory.data + memory_size;
+ env->memory.size = memory_size;
+ SDL_zerop(&env->context);
+
+ return env;
+}
+
+void pw_env_free(struct w2c_env *env) {
+ if (!env) {
+ SDL_InvalidParamError("env");
+ return;
+ }
+
+ SDL_free(env);
+}
+
+bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
+ if (!env) {
+ return SDL_InvalidParamError("env");
+ }
+ if (!context.fb || !context.input || !context.carts || !context.instance) {
+ return SDL_InvalidParamError("context");
+ }
+
+ env->context = context;
+ env->main_loop = 0;
+ return true;
+}
+
+/**
+ * Convenience macro for writing to wasm memory and moving the wasm pointer
+ * forward for that amount.
+ */
+#define MEM_WRITE(n, e, a, v) \
+ do { \
+ wasm_rt_memory_store##n##_unchecked(&e->memory, a, v); \
+ a += (n / 8); \
+ } while (0)
+
+static uint32_t env_ticks(void) {
+ return (uint32_t)(SDL_GetTicks() & UINT32_MAX);
+}
+
+struct wasm_rt_memory_t *w2c_env_memory(struct w2c_env *env) {
+ return &env->memory;
+}
+
+uint32_t w2c_env_0x5FSDL_GetMouseState(struct w2c_env *env, uint32_t x,
+ uint32_t y) {
+ struct pw_coordinates pos;
+ enum pw_mouse_button button = pw_input_mouse(env->context.input, &pos);
+
+ if (x) {
+ wasm_rt_memory_store32(&env->memory, x, pos.x);
+ }
+ if (y) {
+ wasm_rt_memory_store32(&env->memory, y, pos.y);
+ }
+ return (uint32_t)button;
+}
+
+/* import: 'env' '_SDL_LockSurface' */
+uint32_t w2c_env_0x5FSDL_LockSurface(struct w2c_env *env, uint32_t surface) {
+ if (!surface) {
+ SDL_InvalidParamError("surface");
+ return (uint32_t)(-1);
+ }
+
+ wasm_rt_memory_store32(&env->memory, surface + 28, 1);
+ return 0;
+}
+
+#define ENV_KEYDOWN 0x300
+#define ENV_KEYUP 0x301
+#define ENV_MOUSEMOTION 0x400
+
+uint32_t w2c_env_0x5FSDL_PollEvent(struct w2c_env *env, uint32_t event) {
+ if (!event) {
+ return (uint32_t)pw_input_poll(env->context.input, NULL);
+ }
+
+ union pw_input_event input;
+ bool remaining = pw_input_poll(env->context.input, &input);
+ if (remaining) {
+ wasm_rt_memory_check(&env->memory, event, 36);
+ switch (input.type) {
+ case PW_INPUT_EVENT_KEY:
+ if (input.key.pressed) {
+ MEM_WRITE(32, env, event, ENV_KEYDOWN);
+ } else {
+ MEM_WRITE(32, env, event, ENV_KEYUP);
+ }
+ MEM_WRITE(32, env, event, 1); // windowID
+ MEM_WRITE(8, env, event, (uint8_t)input.key.pressed);
+ MEM_WRITE(8, env, event, (uint8_t)input.key.repeat);
+ MEM_WRITE(8, env, event, 0); // padding2
+ MEM_WRITE(8, env, event, 0); // padding3
+ MEM_WRITE(32, env, event, input.key.scancode);
+ MEM_WRITE(32, env, event, input.key.keycode);
+ MEM_WRITE(16, env, event, (uint16_t)input.key.mod);
+ MEM_WRITE(16, env, event, 0); // keysym.{padding}
+ MEM_WRITE(32, env, event, 0); // keysym.unicode (deprecated)
+ break;
+ case PW_INPUT_EVENT_MOUSE:
+ MEM_WRITE(32, env, event, ENV_MOUSEMOTION);
+ MEM_WRITE(32, env, event, env_ticks());
+ MEM_WRITE(32, env, event, 1); // windowID
+ MEM_WRITE(32, env, event, 1); // which
+ MEM_WRITE(32, env, event, (uint32_t)input.mouse.button);
+ MEM_WRITE(32, env, event, input.mouse.x);
+ MEM_WRITE(32, env, event, input.mouse.y);
+ MEM_WRITE(32, env, event, input.mouse.xrel);
+ MEM_WRITE(32, env, event, input.mouse.yrel);
+ break;
+ };
+ }
+
+ return (uint32_t)remaining;
+}
+
+// PICO-8 engine is compiled with the Emscripten built-in SDL1.3 library, which
+// has a different pixel format encoding than SDL3 which is used on the host.
+// The pixel format used by the engine is:
+#define ENV_PIXELFORMAT_ABGR8888 0x86762004
+
+uint32_t w2c_env_0x5FSDL_SetVideoMode(struct w2c_env *env, uint32_t w,
+ uint32_t h, uint32_t bpp,
+ uint32_t flags) {
+ if (w != PW_FB_WIDTH || h != PW_FB_HEIGHT || bpp != 32 || flags != 0) {
+ SDL_SetError("unsupported video mode");
+ return 0;
+ }
+
+ uint32_t result =
+ w2c_pico8_0x5Fmalloc(env->context.instance, 60 + 44 + PW_FB_SIZE);
+ if (!result) {
+ SDL_SetError("failed to allocate surface");
+ return 0;
+ }
+
+ uint32_t surface = result;
+ uint32_t format = surface + 60;
+ uint32_t pixels = format + 44;
+ wasm_rt_memory_check(&env->memory, pixels, PW_FB_SIZE);
+
+ {
+ MEM_WRITE(32, env, surface, flags);
+ MEM_WRITE(32, env, surface, format);
+ MEM_WRITE(32, env, surface, w);
+ MEM_WRITE(32, env, surface, h);
+ MEM_WRITE(32, env, surface, (w * bpp) / 8);
+ MEM_WRITE(32, env, surface, pixels);
+ MEM_WRITE(32, env, surface, 0); // userdata
+ MEM_WRITE(32, env, surface, 0); // locked
+ MEM_WRITE(32, env, surface, 0); // lock_data
+ MEM_WRITE(32, env, surface, 0); // clip_rect.x
+ MEM_WRITE(32, env, surface, 0); // clip_rect.y
+ MEM_WRITE(32, env, surface, w); // clip_rect.w
+ MEM_WRITE(32, env, surface, h); // clip_rect.h
+ MEM_WRITE(32, env, surface, 0); // map
+ MEM_WRITE(32, env, surface, 1); // refcount
+ }
+ {
+ MEM_WRITE(32, env, format, ENV_PIXELFORMAT_ABGR8888);
+ MEM_WRITE(32, env, format, 0); // palette
+ MEM_WRITE(8, env, format, (uint8_t)bpp);
+ MEM_WRITE(8, env, format, (uint8_t)bpp / 8);
+ MEM_WRITE(16, env, format, 0); // padding
+ MEM_WRITE(32, env, format, 0x000000ff); // rmask
+ MEM_WRITE(32, env, format, 0x0000ff00); // gmask
+ MEM_WRITE(32, env, format, 0x00ff0000); // bmask
+ MEM_WRITE(32, env, format, 0xff000000); // amask
+ MEM_WRITE(8, env, format, 0); // rloss
+ MEM_WRITE(8, env, format, 0); // gloss
+ MEM_WRITE(8, env, format, 0); // bloss
+ MEM_WRITE(8, env, format, 0); // aloss
+ MEM_WRITE(8, env, format, 0); // rshift
+ MEM_WRITE(8, env, format, 8); // gshift
+ MEM_WRITE(8, env, format, 16); // bshift
+ MEM_WRITE(8, env, format, 24); // ashift
+ MEM_WRITE(32, env, format, 1); // refcount
+ MEM_WRITE(32, env, format, 0); // next
+ }
+ {
+ wasm_rt_memory_set(&env->memory, pixels, PW_FB_SIZE, 0);
+ }
+
+ return result;
+}
+
+void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) {
+ if (!surface) {
+ SDL_InvalidParamError("surface");
+ return;
+ }
+
+ wasm_rt_memory_check(&env->memory, surface, 60);
+
+ // Always clear the locked flag as unlocking should never fail.
+ wasm_rt_memory_store32_unchecked(&env->memory, surface + 28, 0);
+
+ // Now copy over the screen from wasm memory to our framebuffer. Note that
+ // we bounds check _before_ locking the framebuffer, this prevents OOB access
+ // to trap whitout properly unlocking it.
+ uint32_t pixels = wasm_rt_memory_load32_unchecked(&env->memory, surface + 20);
+ wasm_rt_memory_check(&env->memory, pixels, PW_FB_SIZE);
+ uint8_t *buffer = pw_fb_lock(env->context.fb);
+ if (!buffer) {
+ SDL_SetError("unable to lock framebuffer");
+ return;
+ }
+ wasm_rt_memory_load_unchecked(&env->memory, buffer, pixels, PW_FB_SIZE);
+ pw_fb_unlock(env->context.fb);
+}
+
+/* import: 'env' '_emscripten_set_main_loop' */
+void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
+ uint32_t fps,
+ uint32_t simulate_infinite_loop) {
+ env->main_loop = func;
+
+ // Ignore the fps and simulate infinite loop parameters, they are always 0
+ // for PICO-8 anyway.
+ (void)fps;
+ (void)simulate_infinite_loop;
+}