aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c122
1 files changed, 121 insertions, 1 deletions
diff --git a/src/env.c b/src/env.c
index de2bf9d..0113665 100644
--- a/src/env.c
+++ b/src/env.c
@@ -116,11 +116,51 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
return true;
}
-bool pw_env_shutdown(struct w2c_env *env) {
+bool pw_env_start(struct w2c_env *env) {
if (!env) {
return SDL_InvalidParamError("env");
}
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ return SDL_SetError("PICO-8 startup execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
+ w2c_pico8_0x5F_0x5Femscripten_environ_constructor(env->context.instance);
+
+ char const *args[] = {"./picow", "-run"};
+ uint32_t argv = w2c_pico8_stackAlloc(
+ env->context.instance, (SDL_arraysize(args) + 1) * sizeof(uint32_t));
+ wasm_rt_memory_store32(&env->memory,
+ argv + SDL_arraysize(args) * sizeof(uint32_t), 0);
+ for (size_t i = 0; i < SDL_arraysize(args); ++i) {
+ uint32_t sz = (uint32_t)SDL_strlen(args[i]) + 1;
+ uint32_t arg = w2c_pico8_stackAlloc(env->context.instance, sz);
+ wasm_rt_memory_store(&env->memory, arg, args[i], sz);
+ wasm_rt_memory_store32(&env->memory, argv + i * sizeof(arg), arg);
+ }
+
+ uint32_t status =
+ w2c_pico8_0x5Fmain(env->context.instance, SDL_arraysize(args), argv);
+
+ wasm_rt_trap_unregister();
+
+ if (status) {
+ return SDL_SetError("main returned exit code");
+ }
+ if (!env->data.mainloop) {
+ return SDL_SetError("cannot continue without main loop set");
+ }
+
+ return true;
+}
+
+static bool env_shutdown(struct w2c_env *env) {
if (env->data.state.shutdown) {
return true;
}
@@ -143,6 +183,86 @@ bool pw_env_shutdown(struct w2c_env *env) {
return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value;
}
+bool pw_env_iterate(struct w2c_env *env, bool *shutdown) {
+ if (!env || env_shutdown(env)) {
+ return SDL_InvalidParamError("env");
+ }
+ if (!shutdown) {
+ return SDL_InvalidParamError("shutdown");
+ }
+
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ // Make sure to reset any leftover invocation frames in case of
+ // a trap, as they point to invalid stack addresses.
+ env->data.frame = NULL;
+
+ // In case we trapped for an `_exit` call, treat it as a shutdown
+ // request instead of a trap.
+ if (env_shutdown(env)) {
+ *shutdown = true;
+ return true;
+ }
+
+ return SDL_SetError("PICO-8 iteration execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
+ w2c_pico8_dynCall_v(env->context.instance, env->data.mainloop);
+
+ if (pw_audio_ready(env->context.audio)) {
+ uint32_t len;
+ int16_t *data = pw_audio_lock(env->context.audio, &len);
+ if (!data) {
+ SDL_LogWarn(0, "unable to lock audio device: %s", SDL_GetError());
+ goto mixdone;
+ }
+ if (!len) {
+ goto mixdone;
+ }
+
+ uint32_t samples = w2c_pico8_0x5Fmix_0(env->context.instance, len, len);
+ wasm_rt_memory_load16v(&env->memory, samples, (uint16_t *)data, len);
+ if (!pw_audio_unlock(env->context.audio)) {
+ SDL_LogWarn(0, "unable to unlock audio device: %s", SDL_GetError());
+ goto mixdone;
+ }
+ }
+mixdone:
+
+ wasm_rt_trap_unregister();
+
+ *shutdown = env_shutdown(env);
+
+ bool pointerlock;
+ bool fssync;
+ if (*shutdown) {
+ // Always reset pointer lock and sync storage on shutdown.
+ pointerlock = false;
+ fssync = true;
+ } else {
+ pointerlock = env->data.state.pointerlock;
+ env->data.state.pointerlock = false;
+ fssync = env->data.state.fssync;
+ env->data.state.fssync = false;
+ }
+ if (!pw_input_set_pointer_lock(env->context.input, pointerlock)) {
+ SDL_LogWarn(0, "failed to set pointer lock: %s", SDL_GetError());
+ }
+ if (fssync && !pw_fs_sync(env->context.cart->storage)) {
+ SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError());
+ }
+ if (*shutdown && !pw_audio_stop(env->context.audio)) {
+ SDL_LogWarn(0, "failed to stop audio: %s", SDL_GetError());
+ }
+
+ return true;
+}
+
/**
* Convenience macro for writing to wasm memory and moving the wasm pointer
* forward for that amount.