aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-26 08:32:08 +0100
committerNicholas Rodrigues Lordello <[email protected]>2026-10-26 08:42:53 +0100
commit4957ca8373273721017fc8106b52d5bc6b2a0180 (patch)
tree6372e708f63b61927b93dec2e70162546947c7fe /src/game.c
parentbbc8cadc76aa6419a318d8aeccfb0c25b7a30115 (diff)
Implement Audio Device and Remaining ASM Consts
We abstract over SDL's audio subsystem and implement an audio device that fits into PICO-8's model. We fixed the implementation for the PICO-8 window focus and key buffer and can now actually boot into PICO-8 games with working sounds and controls! There are a few issues that remain: 1. There is no "exit" option in the pause menu, I think it thinks that we are on the BBS, and maybe there is someway to fix this? 2. The clipboard textarea is not implemented at all; we should keep a text buffer in `pw_input` and interact with SDL's clipboard system 3. Multicart's don't always work; in particular Undune II tries to download from the BBS, I would like to shim this somehow to support both `LOAD("FOO.P8")` and `LOAD("#FOO")` forms transparently.
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/game.c b/src/game.c
index d8dd2cd..14f7126 100644
--- a/src/game.c
+++ b/src/game.c
@@ -26,6 +26,11 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode,
goto error;
}
+ game->audio = pw_audio_init();
+ if (!game->audio) {
+ goto error;
+ }
+
game->input = pw_input_init(window);
if (!game->input) {
goto error;
@@ -45,6 +50,9 @@ error:
if (game->fb) {
pw_fb_free(game->fb);
}
+ if (game->audio) {
+ pw_audio_free(game->audio);
+ }
if (game->input) {
pw_input_free(game->input);
}
@@ -62,12 +70,13 @@ void pw_game_free(struct pw_game *game) {
return;
}
+ pw_fb_free(game->fb);
+ pw_audio_free(game->audio);
+ pw_input_free(game->input);
+ pw_env_free(game->env);
if (game->cartdat) {
pw_cart_data_free(game->cartdat);
}
- pw_input_free(game->input);
- pw_fb_free(game->fb);
- pw_env_free(game->env);
pw_fs_free(game->userdat);
SDL_free(game);
}
@@ -93,6 +102,7 @@ bool pw_game_start(struct pw_game *game, struct pw_players *players,
if (!pw_env_link(game->env, (struct pw_env_ctx){
.fb = game->fb,
+ .audio = game->audio,
.input = game->input,
.cartdat = game->cartdat,
.userdat = game->userdat,
@@ -173,7 +183,21 @@ bool pw_game_iterate(struct pw_game *game) {
}
w2c_pico8_dynCall_v(&game->instance, game->env->data.mainloop);
- w2c_pico8_0x5Fmix_0(&game->instance, 42, 1337);
+
+ // TODO(nlordell): we need to improve error handling here, right now
+ // it just silently swallows errors.
+ if (pw_audio_ready(game->audio)) {
+ uint32_t len;
+ int16_t *data = pw_audio_lock(game->audio, &len);
+ if (!data || !len) {
+ goto mixdone;
+ }
+
+ uint32_t samples = w2c_pico8_0x5Fmix_0(&game->instance, len, len);
+ wasm_rt_memory_load16v(&game->env->memory, samples, (uint16_t *)data, len);
+ pw_audio_unlock(game->audio);
+ }
+mixdone:
wasm_rt_trap_unregister();