aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-16 17:18:48 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-16 17:18:48 -0500
commite09c3840d46767c4c5d1e62303e80a5845696afc (patch)
tree67b28f8b2c092c291ad7d2fafa0d44e198bff636 /src/sdl/main.c
parentdf812e3dcd0af5ba932264d7ca1e0131a4a43019 (diff)
downloadsok-e09c3840d46767c4c5d1e62303e80a5845696afc.tar.bz2
sok-e09c3840d46767c4c5d1e62303e80a5845696afc.zip
add src/sdl/sounds.[hc], assets/wavs, and set default sounds
Diffstat (limited to 'src/sdl/main.c')
-rw-r--r--src/sdl/main.c62
1 files changed, 57 insertions, 5 deletions
diff --git a/src/sdl/main.c b/src/sdl/main.c
index 123b1d7..40896e2 100644
--- a/src/sdl/main.c
+++ b/src/sdl/main.c
@@ -3,6 +3,7 @@
#include <string.h> // strerror
#include <errno.h> // errno
#include <SDL.h>
+#include <SDL_mixer.h>
#include "../levels/levels.h"
#include "../core/sok.h"
#include "util.h"
@@ -13,6 +14,7 @@
#include "log-renderer-info.h"
#include "assets.h"
#include "solve.h"
+#include "sounds.h"
#define WINDOW_TITLE "Pablotron Sokoban"
@@ -100,6 +102,16 @@ 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, ASSET_SOUND_HIT_1_WAV);
+}
+
int main(int argc, char *argv[]) {
size_t level_num = (argc > 1) ? atoi(argv[1]) : 0,
zoom = 0;
@@ -123,6 +135,22 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
+ // init SDL_mixer
+ if (Mix_Init(0)) {
+ die("Mix_Init(): %s", Mix_GetError());
+ }
+
+ // register SDL_mixer exit handler
+ if (atexit(Mix_Quit)) {
+ die("atexit(Mix_Quit): %s", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ // open audio
+ if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024)) {
+ die("Mix_OpenAudio(): %s", Mix_GetError());
+ }
+
// init TTF
if (TTF_Init()) {
die("TTF_Init(): %s", TTF_GetError());
@@ -149,6 +177,10 @@ 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) {
@@ -202,33 +234,48 @@ int main(int argc, char *argv[]) {
done = true;
break;
case ACTION_MOVE:
- if (!sok_ctx_move(&ctx, (sok_dir_t) action.data)) {
- draw_ctx.bump_ticks = ticks;
+ 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);
+ }
+ } else {
+ // move failed
+ bump(&draw_ctx, sounds, ticks);
warn("move %s failed", SOK_DIR_TO_STR((sok_dir_t) action.data));
}
break;
case ACTION_UNDO:
- if (!sok_ctx_undo(&ctx)) {
- draw_ctx.bump_ticks = ticks;
+ if (sok_ctx_undo(&ctx)) {
+ // play sound
+ sound_play(sounds, ASSET_SOUND_HIT_1_WAV);
+ } else {
+ bump(&draw_ctx, sounds, ticks);
warn("undo failed");
}
break;
case ACTION_NEXT:
if (sok_ctx_is_done(&ctx)) {
+ // play sound
+ sound_play(sounds, ASSET_SOUND_JUMP_0_WAV);
+
// advance level
level_num++;
// set level
set_level(&draw_ctx, &ctx, level_num);
} else {
- draw_ctx.bump_ticks = ticks;
+ bump(&draw_ctx, sounds, ticks);
warn("cannot advance to next level");
}
break;
case ACTION_RESET:
+ // play sound
+ sound_play(sounds, ASSET_SOUND_UNDO_0_WAV);
+
// reset level
if (!sok_ctx_set_level(&ctx, draw_ctx.level->data)) {
die("Couldn't load level %d", (int) level_num);
@@ -237,6 +284,9 @@ int main(int argc, char *argv[]) {
break;
case ACTION_WARP:
if (warp_buf_get(&warp_buf, &level_num)) {
+ // play sound
+ sound_play(sounds, ASSET_SOUND_JUMP_0_WAV);
+
// load level
set_level(&draw_ctx, &ctx, level_num);
@@ -282,6 +332,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);
draw_ctx.state = GAME_STATE_PLAY;
solve_cancel(draw_ctx.solve);
@@ -292,6 +343,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);
draw_ctx.state = GAME_STATE_PLAY;
// TODO: handle success
solve_fini(draw_ctx.solve, solve_on_done, &ctx);