aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-12 00:13:15 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-12 00:13:15 +0200
commitc6f5cca39dad3f395e8b479f6106feb10e710695 (patch)
treede7bf2cb65cbc3e745ba38e62c748180ee9095a5 /src/game.c
parent88aedb9414800d2e0002430d2d260ea77a60ea8c (diff)
Implement Partial Emscripten SDL API
Start implementing the surface-specific SDL API so that we can get our stub PICO-8 engine to draw a mouse crosshairs to screen. The Emscripten runtime still needs its own file, and we need to convert everything over to `wasm2c`.
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c239
1 files changed, 202 insertions, 37 deletions
diff --git a/src/game.c b/src/game.c
index 0827f84..af87116 100644
--- a/src/game.c
+++ b/src/game.c
@@ -14,6 +14,23 @@ static void *wasm_realloc(void *ptr, unsigned int size) {
static void wasm_free(void *ptr) { SDL_free(ptr); }
+static void *emrt_memory(wasm_exec_env_t exec, uint32_t offset, uint32_t size) {
+ wasm_module_inst_t instance = wasm_runtime_get_module_inst(exec);
+ if (!instance || !wasm_runtime_validate_app_addr(instance, offset, size)) {
+ return NULL;
+ }
+
+ return wasm_runtime_addr_app_to_native(instance, offset);
+}
+
+#define emrt_write(m, t, v) \
+ do { \
+ uint8_t **emrt__m = (uint8_t **)(m); \
+ t emrt__v = (v); \
+ SDL_memcpy(*emrt__m, &emrt__v, sizeof(emrt__v)); \
+ *emrt__m += sizeof(emrt__v); \
+ } while (0)
+
static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) {
native_raw_get_arg(int32_t, func, args);
native_raw_get_arg(int32_t, fps, args);
@@ -29,6 +46,165 @@ static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) {
(void)simulate_infinite_loop;
}
+static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
+ native_raw_return_type(int32_t, args);
+ native_raw_get_arg(int32_t, w, args);
+ native_raw_get_arg(int32_t, h, args);
+ native_raw_get_arg(int32_t, bpp, args);
+ native_raw_get_arg(int32_t, flags, args);
+
+ if (w != 128 || h != 128 || bpp != 32 || flags != 0) {
+ 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;
+ uint8_t *memory;
+ {
+ uint32_t size = 60 + 44 + PW_FB_SIZE;
+ uint32_t args[] = {size};
+ if (!wasm_runtime_call_wasm(exec, game->malloc, 1, args) || !args[0]) {
+ SDL_LogWarn(0, "failed to allocate surface");
+ native_raw_set_return(0);
+ return;
+ }
+
+ surface = args[0];
+ memory = emrt_memory(exec, surface, size);
+ if (!memory) {
+ SDL_LogWarn(0, "invalid surface allocation");
+ native_raw_set_return(0);
+ return;
+ }
+ }
+
+ // surface
+ {
+ void *cursor = memory;
+ emrt_write(&cursor, int32_t, flags);
+ emrt_write(&cursor, uint32_t, surface + 60);
+ emrt_write(&cursor, int32_t, w);
+ emrt_write(&cursor, int32_t, h);
+ emrt_write(&cursor, int32_t, (w * bpp) / 8);
+ emrt_write(&cursor, uint32_t, surface + 60 + 44);
+ emrt_write(&cursor, uint32_t, 0); // userdata
+ emrt_write(&cursor, uint32_t, 0); // locked
+ emrt_write(&cursor, uint32_t, 0); // lock_data
+ emrt_write(&cursor, uint32_t, 0); // clip_rect.x
+ emrt_write(&cursor, uint32_t, 0); // clip_rect.y
+ emrt_write(&cursor, uint32_t, w); // clip_rect.w
+ emrt_write(&cursor, uint32_t, h); // clip_rect.h
+ emrt_write(&cursor, uint32_t, 0); // map
+ emrt_write(&cursor, uint32_t, 1); // refcount
+ };
+ // pixel format
+ {
+ void *cursor = memory + 60;
+ emrt_write(&cursor, uint32_t, 0x86462004); // RGBA8888
+ emrt_write(&cursor, uint32_t, 0); // palette
+ emrt_write(&cursor, int8_t, bpp);
+ emrt_write(&cursor, int8_t, bpp / 8);
+ emrt_write(&cursor, uint16_t, 0); // padding
+ emrt_write(&cursor, uint32_t, 0x000000ff); // rmask
+ emrt_write(&cursor, uint32_t, 0x0000ff00); // gmask
+ emrt_write(&cursor, uint32_t, 0x00ff0000); // bmask
+ emrt_write(&cursor, uint32_t, 0xff000000); // amask
+ emrt_write(&cursor, uint8_t, 0); // rloss
+ emrt_write(&cursor, uint8_t, 0); // gloss
+ emrt_write(&cursor, uint8_t, 0); // bloss
+ emrt_write(&cursor, uint8_t, 0); // aloss
+ emrt_write(&cursor, uint8_t, 0); // rshift
+ emrt_write(&cursor, uint8_t, 0); // gshift
+ emrt_write(&cursor, uint8_t, 0); // bshift
+ emrt_write(&cursor, uint8_t, 0); // ashift
+ emrt_write(&cursor, int32_t, 1); // refcount
+ emrt_write(&cursor, uint32_t, 0); // next
+ }
+ // pixels
+ {
+ void *cursor = memory + 60 + 44;
+ SDL_memset(cursor, 0, PW_FB_SIZE);
+ }
+
+ native_raw_set_return(surface);
+}
+
+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);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+
+ struct pw_coordinates pos;
+ enum pw_mouse_button button = pw_input_mouse(game->input, &pos);
+
+ if (x) {
+ uint8_t *memory = emrt_memory(exec, x, sizeof(int32_t));
+ if (memory) {
+ emrt_write(&memory, int32_t, pos.x);
+ }
+ }
+ if (y) {
+ uint8_t *memory = emrt_memory(exec, y, sizeof(int32_t));
+ if (memory) {
+ emrt_write(&memory, int32_t, pos.y);
+ }
+ }
+
+ native_raw_set_return(button);
+}
+
+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);
+
+ uint8_t *memory = emrt_memory(exec, surface, 60);
+ if (!memory) {
+ native_raw_set_return(-1);
+ return;
+ }
+
+ {
+ void *cursor = memory + 28;
+ emrt_write(&cursor, uint32_t, 1); // locked
+ };
+
+ native_raw_set_return(0);
+}
+
+static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
+ native_raw_get_arg(int32_t, surface, args);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+
+ uint8_t *memory = emrt_memory(exec, surface, 60);
+ if (!memory) {
+ return;
+ }
+
+ uint32_t pixels;
+ SDL_memcpy(&pixels, memory + 20, sizeof(pixels));
+
+ uint8_t *buffer = emrt_memory(exec, pixels, PW_FB_SIZE);
+ if (!buffer) {
+ return;
+ }
+
+ if (!pw_fb_blit(game->fb, buffer)) {
+ return;
+ }
+
+ {
+ void *cursor = memory + 28;
+ emrt_write(&cursor, uint32_t, 0); // locked
+ };
+}
+
struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode) {
if (!renderer) {
SDL_InvalidParamError("renderer");
@@ -66,6 +242,10 @@ 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_GetMouseState", emrt_get_mouse_state, "(ii)i", NULL},
+ {"_SDL_LockSurface", emrt_lock_surface, "(i)i", NULL},
+ {"_SDL_SetVideoMode", emrt_set_video_mode, "(iiii)i", NULL},
+ {"_SDL_UnlockSurface", emrt_unlock_surface, "(i)", NULL},
};
if (!wasm_runtime_register_natives_raw("env", env, SDL_arraysize(env))) {
SDL_SetError("unspecified error registering host functions");
@@ -155,6 +335,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
}
game->instance = NULL;
game->exec = NULL;
+ game->malloc = NULL;
game->dyncall = NULL;
game->mix = NULL;
@@ -171,11 +352,13 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
wasm_runtime_lookup_function(game->instance, "_main");
wasm_function_inst_t stackalloc =
wasm_runtime_lookup_function(game->instance, "stackAlloc");
+ wasm_function_inst_t malloc =
+ wasm_runtime_lookup_function(game->instance, "_malloc");
wasm_function_inst_t dyncall =
wasm_runtime_lookup_function(game->instance, "dynCall_v");
wasm_function_inst_t mix =
wasm_runtime_lookup_function(game->instance, "_mix_0");
- if (!ctor || !start || !stackalloc || !dyncall || !mix) {
+ if (!ctor || !start || !stackalloc || !malloc || !dyncall || !mix) {
return SDL_SetError("could not find required exports");
}
@@ -185,8 +368,12 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
}
wasm_runtime_set_user_data(game->exec, game);
+ game->malloc = malloc;
+ game->dyncall = dyncall;
+ game->mix = mix;
if (!wasm_runtime_call_wasm(game->exec, ctor, 0, NULL)) {
- return SDL_SetError("error calling main");
+ return SDL_SetError("error calling constructory: %s",
+ wasm_runtime_get_exception(game->instance));
}
uint32_t arg0;
@@ -194,11 +381,12 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
char const value[] = "./pico8";
uint32_t args[] = {(uint32_t)sizeof(value)};
if (!wasm_runtime_call_wasm(game->exec, stackalloc, 1, args)) {
- return SDL_SetError("error allocating argument 0");
+ return SDL_SetError("error allocating argument 0: %s",
+ wasm_runtime_get_exception(game->instance));
}
arg0 = args[0];
- char *target = wasm_runtime_addr_app_to_native(game->instance, arg0);
+ char *target = emrt_memory(game->exec, arg0, sizeof(value));
if (!target) {
return SDL_SetError("invalid argument 0 address");
}
@@ -211,11 +399,12 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
uint32_t const value[] = {arg0, 0};
uint32_t args[] = {(uint32_t)sizeof(value)};
if (!wasm_runtime_call_wasm(game->exec, stackalloc, 1, args)) {
- return SDL_SetError("error allocating arguments array");
+ return SDL_SetError("error allocating arguments array: %s",
+ wasm_runtime_get_exception(game->instance));
}
argv = args[0];
- char *target = wasm_runtime_addr_app_to_native(game->instance, argv);
+ char *target = emrt_memory(game->exec, argv, sizeof(value));
if (!target) {
return SDL_SetError("invalid arguments array address");
}
@@ -227,7 +416,8 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
{
uint32_t args[] = {1, argv};
if (!wasm_runtime_call_wasm(game->exec, start, 2, args)) {
- return SDL_SetError("error calling main");
+ return SDL_SetError("error calling main: %s",
+ wasm_runtime_get_exception(game->instance));
}
if (args[0]) {
return SDL_SetError("main returned exit code");
@@ -237,8 +427,6 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
return SDL_SetError("main loop function not set");
}
- game->dyncall = dyncall;
- game->mix = mix;
return true;
}
@@ -264,43 +452,20 @@ bool pw_game_iterate(struct pw_game *game) {
{
uint32_t args[] = {game->mainfn};
if (!wasm_runtime_call_wasm(game->exec, game->dyncall, 1, args)) {
- return SDL_SetError("error calling main loop function");
+ return SDL_SetError("error calling main loop function: %s",
+ wasm_runtime_get_exception(game->instance));
}
}
{
uint32_t args[] = {42, 1337};
if (!wasm_runtime_call_wasm(game->exec, game->mix, 2, args)) {
- return SDL_SetError("error calling audio mixer");
+ return SDL_SetError("error calling audio mixery: %s",
+ wasm_runtime_get_exception(game->instance));
}
if (!args[0]) {
return SDL_SetError("audio mixing failed");
}
}
- {
- // TODO: pretend that our WASM is modifying some backing buffer.
- uint32_t r = (uint32_t)pw_input_btn(game->input, 0) << 1;
- uint32_t g = (uint32_t)pw_input_btn(game->input, 1) << 9;
- uint32_t px = r | g | 0xff2f0000;
- for (int x = 0; x < 128; x++) {
- for (int y = 0; y < 128; y++) {
- int i = x + y * 128;
- if (x == game->input->mouse.pos.x && y == game->input->mouse.pos.y) {
- game->fbbuf[i] = 0xff000000;
- if (game->input->mouse.button & PW_MOUSE_BUTTON_LEFT) {
- game->fbbuf[i] |= 0x0000ff;
- }
- if (game->input->mouse.button & PW_MOUSE_BUTTON_MIDDLE) {
- game->fbbuf[i] |= 0x00ff00;
- }
- if (game->input->mouse.button & PW_MOUSE_BUTTON_RIGHT) {
- game->fbbuf[i] |= 0xff0000;
- }
- } else {
- game->fbbuf[i] = px;
- }
- }
- }
- }
- return pw_fb_blit(game->fb, game->fbbuf) && pw_fb_render(game->fb);
+ return pw_fb_render(game->fb);
}