From 4588b0ef57fb3fd8689cd1d241be9b00307baa1f Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 21 Jan 2019 08:51:45 -0500 Subject: add missing sound events, clean up sound emission --- src/sdl/main.c | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'src/sdl/main.c') diff --git a/src/sdl/main.c b/src/sdl/main.c index 244d5ab..83ea51e 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -171,11 +171,9 @@ solve_on_done( static void bump( draw_ctx_t * const draw_ctx, - Mix_Chunk ** sounds, const Uint32 ticks ) { draw_ctx->bump_ticks = ticks; - sound_play(sounds, SOUND_BUMP); } int main(int argc, char *argv[]) { @@ -228,6 +226,10 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } + // load sounds + Mix_Chunk *sounds[ASSET_SOUND_LAST]; + sounds_init(sounds); + // create window SDL_Window *win = SDL_CreateWindow( WINDOW_TITLE, @@ -243,10 +245,6 @@ int main(int argc, char *argv[]) { die("SDL_CreateWindow(): %s", SDL_GetError()); } - // load sounds - Mix_Chunk *sounds[ASSET_SOUND_LAST]; - sounds_init(sounds); - // create renderer SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (!renderer) { @@ -306,23 +304,25 @@ int main(int argc, char *argv[]) { break; case ACTION_MOVE: if (sok_ctx_move(&ctx, (sok_dir_t) action.data)) { - if (sok_ctx_is_done(&ctx)) { - // play sound - sound_play(sounds, SOUND_LEVEL_DONE); - } + // move success + const bool is_done = sok_ctx_is_done(&ctx); + sound_play(sounds, is_done ? SOUND_LEVEL_DONE : SOUND_MOVE); } else { // move failed - bump(&draw_ctx, sounds, ticks); + sound_play(sounds, SOUND_MOVE_FAILED); + bump(&draw_ctx, ticks); warn("move %s failed", SOK_DIR_TO_STR((sok_dir_t) action.data)); } break; case ACTION_UNDO: if (sok_ctx_undo(&ctx)) { - // play sound + // undo success sound_play(sounds, SOUND_UNDO); } else { - bump(&draw_ctx, sounds, ticks); + // undo failed + sound_play(sounds, SOUND_UNDO_FAILED); + bump(&draw_ctx, ticks); warn("undo failed"); } @@ -338,7 +338,9 @@ int main(int argc, char *argv[]) { // set level set_level(&draw_ctx, &ctx, level_num); } else { - bump(&draw_ctx, sounds, ticks); + // next failed + sound_play(sounds, SOUND_LEVEL_NEXT_FAILED); + bump(&draw_ctx, ticks); warn("cannot advance to next level"); } @@ -375,6 +377,10 @@ int main(int argc, char *argv[]) { break; case ACTION_SOLVE: + // play sound + sound_play(sounds, SOUND_SOLVE_START); + + // queue solve draw_ctx.state = GAME_STATE_SOLVE; draw_ctx.solve_num_steps = 0; draw_ctx.solve = solve(&ctx, solve_event_type); @@ -410,8 +416,11 @@ int main(int argc, char *argv[]) { break; case ACTION_SOLVE_CANCEL: - SDL_Log("solve cancelled by user"); + // play sound sound_play(sounds, SOUND_SOLVE_CANCEL); + + // cancel solve + SDL_Log("solve cancelled by user"); draw_ctx.state = GAME_STATE_PLAY; solve_cancel(draw_ctx.solve); @@ -421,15 +430,21 @@ int main(int argc, char *argv[]) { break; case ACTION_SOLVE_EVENT_DONE: - SDL_Log("solve done"); + // play sound sound_play(sounds, SOUND_SOLVE_DONE); + + // handle solve done + SDL_Log("solve done"); draw_ctx.state = GAME_STATE_PLAY; - // TODO: handle success solve_fini(draw_ctx.solve, solve_on_done, &ctx); draw_ctx.solve = NULL; break; case ACTION_SOLVE_EVENT_FAIL: + // play sound + sound_play(sounds, SOUND_SOLVE_FAILED); + + // handle solve fail SDL_Log("solve fail"); draw_ctx.state = GAME_STATE_PLAY; solve_fini(draw_ctx.solve, NULL, NULL); -- cgit v1.2.3