aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-12 20:31:10 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-12 20:31:10 +0200
commite06434fd89345fdc4e931f6fa3904626d75097db (patch)
tree75287102b1e1ec85db4cdb7d634da4b5fee6f9a7 /src/game.c
parentc6f5cca39dad3f395e8b479f6106feb10e710695 (diff)
Fix Byte Order for Surface Pixels
I went down a deep rabbithole to understand the pixel order, and it turns out that the Emscripten library code is setting the wrong `format` value for what it actually uses, which threw me off. We now set the correct pixel format tag, and handle pixel byte ordering correctly on the host. Some other minor fixes.
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/src/game.c b/src/game.c
index af87116..2505e2a 100644
--- a/src/game.c
+++ b/src/game.c
@@ -46,6 +46,11 @@ static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) {
(void)simulate_infinite_loop;
}
+// 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 EMRT_PIXELFORMAT_ABGR8888 0x86762004
+
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);
@@ -66,14 +71,14 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
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]) {
+ uint32_t margs[] = {size};
+ if (!wasm_runtime_call_wasm(exec, game->malloc, 1, margs) || !margs[0]) {
SDL_LogWarn(0, "failed to allocate surface");
native_raw_set_return(0);
return;
}
- surface = args[0];
+ surface = margs[0];
memory = emrt_memory(exec, surface, size);
if (!memory) {
SDL_LogWarn(0, "invalid surface allocation");
@@ -94,20 +99,20 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
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, int32_t, 0); // clip_rect.x
+ emrt_write(&cursor, int32_t, 0); // clip_rect.y
+ emrt_write(&cursor, int32_t, w); // clip_rect.w
+ emrt_write(&cursor, int32_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, uint32_t, EMRT_PIXELFORMAT_ABGR8888);
+ emrt_write(&cursor, uint32_t, 0); // palette
+ emrt_write(&cursor, int8_t, (int8_t)bpp);
+ emrt_write(&cursor, int8_t, (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
@@ -118,9 +123,9 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
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, uint8_t, 8); // gshift
+ emrt_write(&cursor, uint8_t, 16); // bshift
+ emrt_write(&cursor, uint8_t, 24); // ashift
emrt_write(&cursor, int32_t, 1); // refcount
emrt_write(&cursor, uint32_t, 0); // next
}
@@ -187,6 +192,12 @@ static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
return;
}
+ // Always clear the locked flag as unlocking should never fail.
+ {
+ void *cursor = memory + 28;
+ emrt_write(&cursor, uint32_t, 0); // locked
+ };
+
uint32_t pixels;
SDL_memcpy(&pixels, memory + 20, sizeof(pixels));
@@ -196,13 +207,8 @@ static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
}
if (!pw_fb_blit(game->fb, buffer)) {
- return;
+ SDL_LogWarn(0, "failed to blit surface to framebuffer: %s", SDL_GetError());
}
-
- {
- 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) {
@@ -372,7 +378,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
game->dyncall = dyncall;
game->mix = mix;
if (!wasm_runtime_call_wasm(game->exec, ctor, 0, NULL)) {
- return SDL_SetError("error calling constructory: %s",
+ return SDL_SetError("error calling constructor: %s",
wasm_runtime_get_exception(game->instance));
}
@@ -459,7 +465,7 @@ bool pw_game_iterate(struct pw_game *game) {
{
uint32_t args[] = {42, 1337};
if (!wasm_runtime_call_wasm(game->exec, game->mix, 2, args)) {
- return SDL_SetError("error calling audio mixery: %s",
+ return SDL_SetError("error calling audio mixer: %s",
wasm_runtime_get_exception(game->instance));
}
if (!args[0]) {