From 4ef8cf8b3d9ad5235aa858a68f70a8aaf2b356da Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Mon, 3 Jul 2023 21:40:42 +0100 Subject: Sound queue This is to queue sounds and play them when we update the screen, so the sounds are synced with the action. It is required because the MikMod_Update function updates on the clock int, and is not synced with the VGA's vsync. --- src/game.c | 4 +++- src/pickup.c | 6 +++--- src/player.c | 8 ++++---- src/sound.c | 27 +++++++++++++++++++++++++++ src/sound.h | 3 +++ 5 files changed, 40 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/game.c b/src/game.c index da476dd..a89ff66 100644 --- a/src/game.c +++ b/src/game.c @@ -87,7 +87,7 @@ static void hud_render() sprintf(b, "%02d", time); put_text(172, 4, b, time > 10 ? 1 : 15); if (!gameover && time <= 10) - sound_play_efx(EFX_TIME); + sound_queue_efx(EFX_TIME); } if (hud & HUD_STAGE) @@ -193,7 +193,9 @@ void run_game() player_draw(); wait_vsync(); + blit_update(); + sound_play_queue(); if (gameover) { diff --git a/src/pickup.c b/src/pickup.c index 7036891..91e44b2 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -136,7 +136,7 @@ void pickup_wait_update(Entity *e) if (e->counter-- == 0) { e->update = pickup_in_update; - sound_play_efx(EFX_WARP); + sound_queue_efx(EFX_WARP); } } @@ -174,7 +174,7 @@ void pickup_update(Entity *e) if (e->counter++ == MAX_TTL) { effect_out_init(e); - sound_play_efx(EFX_WARP); + sound_queue_efx(EFX_WARP); return; } @@ -208,6 +208,6 @@ void pickup_update(Entity *e) break; } e->used = 0; - sound_play_efx(EFX_PICKUP); + sound_queue_efx(EFX_PICKUP); } } diff --git a/src/player.c b/src/player.c index 8d80923..c07cc34 100644 --- a/src/player.c +++ b/src/player.c @@ -165,7 +165,7 @@ void player_update() momentum = 0; frame = FRAME_JUMPING; gravity = GRAVITY_UP; - sound_play_efx(EFX_JUMP); + sound_queue_efx(EFX_JUMP); } } else @@ -278,7 +278,7 @@ void player_update() if (map_update_gold(x + (dir == DIR_LEFT ? 7 : 8), y + 15)) { add_score(10); - sound_play_efx(EFX_GOLD); + sound_queue_efx(EFX_GOLD); } } @@ -325,13 +325,13 @@ void player_hit() if (use_pickaxe()) { invuln = INVULN_TIME; - sound_play_efx(EFX_HIT); + sound_queue_efx(EFX_HIT); } else { dying = 1; frame = FRAME_DYING; gravity = GRAVITY_UP; - sound_play_efx(EFX_DEATH); + sound_queue_efx(EFX_DEATH); } } diff --git a/src/sound.c b/src/sound.c index f57c133..4a608cf 100644 --- a/src/sound.c +++ b/src/sound.c @@ -29,6 +29,11 @@ static uint32_t voice = 0; static MODULE *music = NULL; +#define MAX_QUEUE 4 + +static uint8_t queue[MAX_QUEUE]; +static uint8_t queue_top; + uint8_t sound_init() { MikMod_RegisterAllDrivers(); @@ -61,6 +66,8 @@ uint8_t sound_init() Player_Start(music); MikMod_EnableOutput(); + queue_top = 0; + return 1; } @@ -73,7 +80,9 @@ void sound_update() if (++divider == 10) { divider = 0; + MikMod_Lock(); MikMod_Update(); + MikMod_Unlock(); } } @@ -114,3 +123,21 @@ void sound_play_efx(uint8_t efxno) cur = efxno; voice = Sample_Play(efx[cur].s, 0, 0); } + +void sound_queue_efx(uint8_t efxno) +{ + if (queue_top >= MAX_QUEUE) + return; + + queue[queue_top++] = efxno; +} + +void sound_play_queue() +{ + while (queue_top > 0) + sound_play_efx(queue[--queue_top]); + + MikMod_Lock(); + MikMod_Update(); + MikMod_Unlock(); +} diff --git a/src/sound.h b/src/sound.h index 506531b..b51a8ba 100644 --- a/src/sound.h +++ b/src/sound.h @@ -18,6 +18,9 @@ void sound_update(); void sound_music_pattern(uint16_t pat); void sound_play_efx(uint8_t efxno); +void sound_queue_efx(uint8_t efxno); +void sound_play_queue(); + void sound_mute(); void sound_unmute(); -- cgit v1.2.3