From 131a454b62687224a2e0a98c05a456729cc00761 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 20 Jan 2019 23:55:01 -0500 Subject: add sound map --- src/sdl/main.c | 16 ++++++++-------- src/sdl/sounds.c | 44 +++++++++++++++++++++++++++++++++++++++++--- src/sdl/sounds.h | 14 +++++++++++++- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/sdl/main.c b/src/sdl/main.c index 53f2377..244d5ab 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -175,7 +175,7 @@ bump( const Uint32 ticks ) { draw_ctx->bump_ticks = ticks; - sound_play(sounds, ASSET_SOUND_HIT_1_WAV); + sound_play(sounds, SOUND_BUMP); } int main(int argc, char *argv[]) { @@ -308,7 +308,7 @@ int main(int argc, char *argv[]) { if (sok_ctx_move(&ctx, (sok_dir_t) action.data)) { if (sok_ctx_is_done(&ctx)) { // play sound - sound_play(sounds, ASSET_SOUND_POWERUP_3_WAV); + sound_play(sounds, SOUND_LEVEL_DONE); } } else { // move failed @@ -320,7 +320,7 @@ int main(int argc, char *argv[]) { case ACTION_UNDO: if (sok_ctx_undo(&ctx)) { // play sound - sound_play(sounds, ASSET_SOUND_HIT_1_WAV); + sound_play(sounds, SOUND_UNDO); } else { bump(&draw_ctx, sounds, ticks); warn("undo failed"); @@ -330,7 +330,7 @@ int main(int argc, char *argv[]) { case ACTION_NEXT: if (sok_ctx_is_done(&ctx)) { // play sound - sound_play(sounds, ASSET_SOUND_POWERUP_1_WAV); + sound_play(sounds, SOUND_LEVEL_NEXT); // advance level level_num++; @@ -345,7 +345,7 @@ int main(int argc, char *argv[]) { break; case ACTION_RESET: // play sound - sound_play(sounds, ASSET_SOUND_UNDO_0_WAV); + sound_play(sounds, SOUND_LEVEL_RESET); // reset level if (!sok_ctx_set_level(&ctx, draw_ctx.level->data)) { @@ -356,7 +356,7 @@ int main(int argc, char *argv[]) { case ACTION_WARP: if (warp_buf_get(&warp_buf, &level_num)) { // play sound - sound_play(sounds, ASSET_SOUND_POWERUP_1_WAV); + sound_play(sounds, SOUND_LEVEL_WARP); // load level set_level(&draw_ctx, &ctx, level_num); @@ -411,7 +411,7 @@ int main(int argc, char *argv[]) { break; case ACTION_SOLVE_CANCEL: SDL_Log("solve cancelled by user"); - sound_play(sounds, ASSET_SOUND_HIT_2_WAV); + sound_play(sounds, SOUND_SOLVE_CANCEL); draw_ctx.state = GAME_STATE_PLAY; solve_cancel(draw_ctx.solve); @@ -422,7 +422,7 @@ int main(int argc, char *argv[]) { break; case ACTION_SOLVE_EVENT_DONE: SDL_Log("solve done"); - sound_play(sounds, ASSET_SOUND_POWERUP_4_WAV); + sound_play(sounds, SOUND_SOLVE_DONE); draw_ctx.state = GAME_STATE_PLAY; // TODO: handle success solve_fini(draw_ctx.solve, solve_on_done, &ctx); diff --git a/src/sdl/sounds.c b/src/sdl/sounds.c index f110b26..2c8c3f1 100644 --- a/src/sdl/sounds.c +++ b/src/sdl/sounds.c @@ -5,6 +5,21 @@ #define SOUND_OFS(asset_id) ((asset_id) - ASSET_SOUND_FIRST - 1) +static const struct { + sound_t sound; + asset_id_t asset_id; +} SOUND_MAP[] = { + { SOUND_BUMP, ASSET_SOUND_HIT_1_WAV }, + { SOUND_LEVEL_DONE, ASSET_SOUND_POWERUP_3_WAV }, + { SOUND_UNDO, ASSET_SOUND_HIT_1_WAV }, + { SOUND_LEVEL_NEXT, ASSET_SOUND_POWERUP_1_WAV }, + { SOUND_LEVEL_RESET, ASSET_SOUND_UNDO_0_WAV }, + { SOUND_LEVEL_WARP, ASSET_SOUND_POWERUP_1_WAV }, + { SOUND_SOLVE_CANCEL, ASSET_SOUND_HIT_2_WAV }, + { SOUND_SOLVE_DONE, ASSET_SOUND_POWERUP_4_WAV }, + { SOUND_LAST, ASSET_LAST }, +}; + void sounds_init( Mix_Chunk ** const sounds @@ -30,14 +45,37 @@ sounds_init( } } +static asset_id_t +get_asset_id( + const sound_t sound_id +) { + for (size_t i = 0; SOUND_MAP[i].sound != SOUND_LAST; i++) { + if (SOUND_MAP[i].sound == sound_id) { + return SOUND_MAP[i].asset_id; + } + } + + // return failure + return ASSET_LAST; +} + void sound_play( Mix_Chunk ** const sounds, - const asset_id_t id + const sound_t sound_id ) { - // check ID + // get asset ID + const asset_id_t id = get_asset_id(sound_id); + + if (id == ASSET_LAST) { + // no sound to play, return + return; + } + + // check asset ID bounds if (id <= ASSET_SOUND_FIRST || id >= ASSET_SOUND_LAST) { - die("invalid sound asset id: %u", id); + warn("invalid sound asset id: %u", id); + return; } // play sound diff --git a/src/sdl/sounds.h b/src/sdl/sounds.h index 3b74aff..38d6a14 100644 --- a/src/sdl/sounds.h +++ b/src/sdl/sounds.h @@ -4,7 +4,19 @@ #include #include "assets.h" +typedef enum { + SOUND_BUMP, + SOUND_LEVEL_DONE, + SOUND_UNDO, + SOUND_LEVEL_NEXT, + SOUND_LEVEL_RESET, + SOUND_LEVEL_WARP, + SOUND_SOLVE_CANCEL, + SOUND_SOLVE_DONE, + SOUND_LAST, +} sound_t; + void sounds_init(Mix_Chunk ** const); -void sound_play(Mix_Chunk ** const sounds, const asset_id_t); +void sound_play(Mix_Chunk ** const sounds, const sound_t); #endif /* SOUNDS_H */ -- cgit v1.2.3