aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-10 00:16:57 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-10 00:16:57 +0200
commit70d2db07ec4dd5034c6d888258a3fd945c67e4d4 (patch)
tree9f0e4d22d1b5fecb0a5cfaf3554c673a8a655dcc /src/game.c
parent0a98b48593e4ad561c7732387a688797ad5ee1ce (diff)
Separate Scene Logic
We now have two separate "scenes": - cart selection - game In their own modules. Instead of implementing some scene management logic, we just hardcode the scene logic directly in the `picow` main module - I don't forsee more scenes and there is no need to over-engineer at this point.
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c293
1 files changed, 293 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..72d5046
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,293 @@
+#include <SDL3/SDL_log.h>
+
+#include "game.h"
+#include "input.h"
+#include "sysroot.h"
+
+static void *wasm_malloc(unsigned int size) { return SDL_malloc((size_t)size); }
+
+static void *wasm_realloc(void *ptr, unsigned int size) {
+ return SDL_realloc(ptr, (size_t)size);
+}
+
+static void wasm_free(void *ptr) { SDL_free(ptr); }
+
+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);
+ native_raw_get_arg(int32_t, simulate_infinite_loop, args);
+
+ struct pw_game *game = wasm_runtime_get_user_data(exec);
+ SDL_LogDebug(0, "setting main loop function: %d", func);
+ game->mainfn = func;
+
+ // Ignore the fps and simulate infinite loop parameters, they are always 0
+ // for PICO-8 anyway.
+ (void)fps;
+ (void)simulate_infinite_loop;
+}
+
+struct pw_game *pw_game_init(SDL_Renderer *renderer) {
+ if (!renderer) {
+ SDL_InvalidParamError("renderer");
+ return NULL;
+ }
+
+ bool wasm_init = false;
+ struct pw_game *game = SDL_malloc(sizeof(*game));
+ if (!game) {
+ return NULL;
+ }
+ SDL_zerop(game);
+
+ game->fb = pw_fb_init(renderer);
+ if (!game->fb) {
+ goto error;
+ }
+
+ game->input = pw_input_init();
+ if (!game->input) {
+ goto error;
+ }
+
+ struct RuntimeInitArgs init = {
+ .mem_alloc_type = Alloc_With_Allocator,
+ .mem_alloc_option.allocator.malloc_func = &wasm_malloc,
+ .mem_alloc_option.allocator.realloc_func = &wasm_realloc,
+ .mem_alloc_option.allocator.free_func = &wasm_free,
+ };
+ if (!wasm_runtime_full_init(&init)) {
+ SDL_SetError("unspecified error initializing WebAssembly runtime");
+ goto error;
+ }
+ wasm_init = true;
+
+ struct NativeSymbol env[] = {
+ {"_emscripten_set_main_loop", emrt_set_main_loop, "(iii)", NULL},
+ };
+ if (!wasm_runtime_register_natives_raw("env", env, SDL_arraysize(env))) {
+ SDL_SetError("unspecified error registering host functions");
+ goto error;
+ }
+
+ char err[1024];
+ game->module = wasm_runtime_load(stub_pico8_wasm.data, stub_pico8_wasm.size,
+ err, sizeof err);
+ if (game->module == NULL) {
+ SDL_SetError("%s", err);
+ goto error;
+ }
+
+ struct NativeGlobal genv[] = {};
+ if (!wasm_runtime_link_globals(game->module, "env", genv,
+ SDL_arraysize(genv)) ||
+ !wasm_runtime_link_globals(game->module, "global", genv,
+ SDL_arraysize(genv))) {
+ SDL_SetError("unspecified error registiering host globals");
+ goto error;
+ }
+
+ return game;
+
+error:
+ if (game->fb) {
+ pw_fb_free(game->fb);
+ }
+ if (game->input) {
+ pw_input_free(game->input);
+ }
+ if (game->module) {
+ wasm_runtime_unload(game->module);
+ }
+ if (wasm_init) {
+ wasm_runtime_destroy();
+ }
+ SDL_free(game);
+ return NULL;
+}
+
+void pw_game_free(struct pw_game *game) {
+ if (!game) {
+ SDL_InvalidParamError("game");
+ return;
+ }
+
+ if (game->instance) {
+ wasm_runtime_deinstantiate(game->instance);
+ }
+ wasm_runtime_unload(game->module);
+ wasm_runtime_destroy();
+
+ if (game->cart) {
+ pw_multicart_free(game->cart);
+ }
+ pw_input_free(game->input);
+ pw_fb_free(game->fb);
+}
+
+bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
+ if (!game) {
+ SDL_InvalidParamError("game");
+ return false;
+ }
+ if (!cart) {
+ SDL_InvalidParamError("cart");
+ return false;
+ }
+
+ char err[1024];
+
+ if (game->cart) {
+ pw_multicart_free(game->cart);
+ }
+ game->cart = cart;
+
+ if (game->instance) {
+ wasm_runtime_deinstantiate(game->instance);
+ }
+ game->instance =
+ wasm_runtime_instantiate(game->module, 1024, 0, err, sizeof err);
+ if (!game->instance) {
+ return SDL_SetError("%s", err);
+ }
+
+ wasm_function_inst_t ctor = wasm_runtime_lookup_function(
+ game->instance, "___emscripten_environ_constructor");
+ wasm_function_inst_t start =
+ wasm_runtime_lookup_function(game->instance, "_main");
+ wasm_function_inst_t stackalloc =
+ wasm_runtime_lookup_function(game->instance, "stackAlloc");
+ 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) {
+ return SDL_SetError("could not required exports");
+ }
+
+ wasm_exec_env_t exec = wasm_runtime_create_exec_env(game->instance, 1024);
+ if (exec == NULL) {
+ return SDL_SetError("failed to create execution environment");
+ }
+ wasm_runtime_set_user_data(exec, game);
+
+ if (!wasm_runtime_call_wasm(exec, ctor, 0, NULL)) {
+ return SDL_SetError("error calling main");
+ }
+
+ uint32_t arg0;
+ {
+ char const value[] = "./pico8";
+ uint32_t args[] = {(uint32_t)sizeof(value)};
+ if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ return SDL_SetError("error allocating argument 0");
+ }
+
+ arg0 = args[0];
+ char *target = wasm_runtime_addr_app_to_native(game->instance, arg0);
+ if (!target) {
+ return SDL_SetError("invalid argument 0 address");
+ }
+
+ SDL_memcpy(target, value, sizeof(value));
+ }
+
+ uint32_t argv;
+ {
+ uint32_t const value[] = {arg0, 0};
+ uint32_t args[] = {(uint32_t)sizeof(value)};
+ if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ return SDL_SetError("error allocating arguments array");
+ }
+
+ argv = args[0];
+ char *target = wasm_runtime_addr_app_to_native(game->instance, argv);
+ if (!target) {
+ return SDL_SetError("invalid arguments array address");
+ }
+
+ SDL_memcpy(target, value, sizeof(value));
+ }
+
+ game->mainfn = 0;
+ {
+ uint32_t args[] = {1, argv};
+ if (!wasm_runtime_call_wasm(exec, start, 2, args)) {
+ return SDL_SetError("error calling main");
+ }
+ if (args[0]) {
+ return SDL_SetError("main returned exit code");
+ }
+ }
+ if (!game->mainfn) {
+ return SDL_SetError("main loop function not set");
+ }
+
+ game->exec = exec;
+ game->dyncall = dyncall;
+ game->mix = mix;
+ return true;
+}
+
+bool pw_game_event(struct pw_game *game, struct pw_players *players,
+ SDL_Event *event) {
+ if (!game) {
+ SDL_InvalidParamError("game");
+ return false;
+ }
+
+ return pw_input_event(game->input, players, event);
+}
+
+bool pw_game_iterate(struct pw_game *game) {
+ if (!game) {
+ SDL_InvalidParamError("game");
+ return false;
+ }
+
+ pw_fb_iterate(game->fb);
+ pw_input_iterate(game->input, game->fb);
+
+ {
+ uint32_t args[] = {game->mainfn};
+ if (!wasm_runtime_call_wasm(game->exec, game->dyncall, 1, args)) {
+ return SDL_SetError("error calling main loop function");
+ }
+ }
+ {
+ uint32_t args[] = {42, 1337};
+ if (!wasm_runtime_call_wasm(game->exec, game->mix, 2, args)) {
+ return SDL_SetError("error calling audio mixer");
+ }
+ if (!args[0]) {
+ return SDL_SetError("audio mixing failed %d %d", args[0], args[1]);
+ }
+ }
+ {
+ // 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);
+}