aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fb.c9
-rw-r--r--src/fb.h4
-rw-r--r--src/game.c239
-rw-r--r--src/game.h4
-rw-r--r--src/input.c6
-rw-r--r--src/input.h3
-rw-r--r--src/picow.c4
7 files changed, 222 insertions, 47 deletions
diff --git a/src/fb.c b/src/fb.c
index f52f0c0..e46cdce 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -13,7 +13,7 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer,
// Note that we use a different pixel format here, this is to ensure we match
// the PICO-8 engine whose format is endian-dependant.
texture =
- SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
+ SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT);
if (!texture) {
goto error;
@@ -95,12 +95,15 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
return true;
}
-bool pw_fb_blit(struct pw_framebuffer *fb,
- uint32_t const pixels[PW_FB_WIDTH * PW_FB_HEIGHT]) {
+bool pw_fb_blit(struct pw_framebuffer *fb, uint8_t const pixels[PW_FB_SIZE]) {
if (!fb) {
SDL_InvalidParamError("fb");
return false;
}
+ if (!pixels) {
+ SDL_InvalidParamError("pixels");
+ return false;
+ }
SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
if (!renderer) {
diff --git a/src/fb.h b/src/fb.h
index 7a7409a..0ac503c 100644
--- a/src/fb.h
+++ b/src/fb.h
@@ -7,6 +7,7 @@
#define PW_FB_WIDTH 128
#define PW_FB_HEIGHT 128
+#define PW_FB_SIZE (128 * 128 * 4)
/**
* A PICO-8 framebuffer.
@@ -50,8 +51,7 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
/**
* Blits the pixel data to the framebuffer.
*/
-bool pw_fb_blit(struct pw_framebuffer *fb,
- uint32_t const pixels[PW_FB_WIDTH * PW_FB_HEIGHT]);
+bool pw_fb_blit(struct pw_framebuffer *fb, uint8_t const pixels[PW_FB_SIZE]);
/**
* Renders the the framebuffer.
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);
}
diff --git a/src/game.h b/src/game.h
index 7b38d0b..7047cd7 100644
--- a/src/game.h
+++ b/src/game.h
@@ -22,12 +22,10 @@ struct pw_game {
wasm_module_t module;
wasm_module_inst_t instance;
wasm_exec_env_t exec;
+ wasm_function_inst_t malloc;
wasm_function_inst_t dyncall;
wasm_function_inst_t mix;
int32_t mainfn;
-
- // TODO: Hack until the backing buffer lives in WASM memory.
- uint32_t fbbuf[PW_FB_WIDTH * PW_FB_HEIGHT];
};
/**
diff --git a/src/input.c b/src/input.c
index db491dd..fb3e55a 100644
--- a/src/input.c
+++ b/src/input.c
@@ -315,11 +315,15 @@ enum pw_keymod pw_input_keymod(struct pw_input *input) {
return input->keyboard.mod;
}
-enum pw_mouse_button pw_input_mouse(struct pw_input *input) {
+enum pw_mouse_button pw_input_mouse(struct pw_input *input,
+ struct pw_coordinates *pos) {
if (!input) {
SDL_InvalidParamError("input");
return PW_MOUSE_BUTTON_NONE;
}
+ if (pos) {
+ SDL_memcpy(pos, &input->mouse.pos, sizeof(*pos));
+ }
return input->mouse.button;
}
diff --git a/src/input.h b/src/input.h
index 13811a6..089c4be 100644
--- a/src/input.h
+++ b/src/input.h
@@ -121,6 +121,7 @@ enum pw_keymod pw_input_keymod(struct pw_input *input);
/**
* Gets the current mouse button state.
*/
-enum pw_mouse_button pw_input_mouse(struct pw_input *input);
+enum pw_mouse_button pw_input_mouse(struct pw_input *input,
+ struct pw_coordinates *pos);
#endif
diff --git a/src/picow.c b/src/picow.c
index e61607d..d6358e3 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -1,3 +1,4 @@
+#include "SDL3/SDL_render.h"
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
@@ -82,6 +83,9 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
if (!SDL_SetWindowMinimumSize(state->window, PW_PNG_WIDTH, PW_PNG_HEIGHT)) {
return failf("failed to configure minimum window size");
}
+ if (!SDL_SetRenderVSync(state->renderer, 1)) {
+ return failf("failed to enable V-sync");
+ }
state->players = pw_players_init();
if (!state->players) {