aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fb.c11
-rw-r--r--src/fb.h3
-rw-r--r--src/game.c50
-rw-r--r--src/picow.c5
4 files changed, 38 insertions, 31 deletions
diff --git a/src/fb.c b/src/fb.c
index e46cdce..a50ff03 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -10,10 +10,11 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer,
SDL_Texture *texture = NULL;
struct pw_framebuffer *fb = NULL;
- // Note that we use a different pixel format here, this is to ensure we match
- // the PICO-8 engine whose format is endian-dependant.
+ // Web Assembly guarantees little-endian semantics, so the pixel data we get
+ // will _always_ be RGBA in **byte** sequence. Use the appropriate single bit
+ // count format, which has a stable byte order regardless of our endianness.
texture =
- SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
+ SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT);
if (!texture) {
goto error;
@@ -116,9 +117,9 @@ bool pw_fb_blit(struct pw_framebuffer *fb, uint8_t const pixels[PW_FB_SIZE]) {
return false;
}
- bool ok = pitch == sizeof(uint32_t[PW_FB_WIDTH]);
+ bool ok = pitch == PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL;
if (ok) {
- SDL_memcpy(buffer, pixels, sizeof(uint32_t[PW_FB_WIDTH * PW_FB_HEIGHT]));
+ SDL_memcpy(buffer, pixels, PW_FB_SIZE);
}
SDL_UnlockTexture(fb->texture);
diff --git a/src/fb.h b/src/fb.h
index 0ac503c..7656921 100644
--- a/src/fb.h
+++ b/src/fb.h
@@ -7,7 +7,8 @@
#define PW_FB_WIDTH 128
#define PW_FB_HEIGHT 128
-#define PW_FB_SIZE (128 * 128 * 4)
+#define PW_FB_BYTES_PER_PIXEL 4
+#define PW_FB_SIZE (PW_FB_WIDTH * PW_FB_HEIGHT * PW_FB_BYTES_PER_PIXEL)
/**
* A PICO-8 framebuffer.
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]) {
diff --git a/src/picow.c b/src/picow.c
index d6358e3..bc26b44 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -1,4 +1,3 @@
-#include "SDL3/SDL_render.h"
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
@@ -21,7 +20,7 @@ static SDL_AppResult failf(char const *fmt, ...) {
}
char const *err = SDL_GetError();
- if (err) {
+ if (err && *err) {
SDL_LogCritical(0, "%s: %s", msg, err);
} else {
SDL_LogCritical(0, "%s", msg);
@@ -84,7 +83,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return failf("failed to configure minimum window size");
}
if (!SDL_SetRenderVSync(state->renderer, 1)) {
- return failf("failed to enable V-sync");
+ SDL_LogWarn(0, "failed to enable V-sync: %s", SDL_GetError());
}
state->players = pw_players_init();