aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/sounds.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-16 17:22:42 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-16 17:22:42 -0500
commit0fd2a97e6f169468b3a48f996ba35cd70210baa0 (patch)
treea1df62f9225cd35ba6b947ce82031ad60b0a1fef /src/sdl/sounds.c
parente09c3840d46767c4c5d1e62303e80a5845696afc (diff)
downloadsok-0fd2a97e6f169468b3a48f996ba35cd70210baa0.tar.bz2
sok-0fd2a97e6f169468b3a48f996ba35cd70210baa0.zip
add src/sdl/sounds.[hc], assets/wavs, and set default sounds
Diffstat (limited to 'src/sdl/sounds.c')
-rw-r--r--src/sdl/sounds.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/sdl/sounds.c b/src/sdl/sounds.c
new file mode 100644
index 0000000..956229c
--- /dev/null
+++ b/src/sdl/sounds.c
@@ -0,0 +1,47 @@
+#include <SDL.h>
+#include "util.h"
+#include "assets.h"
+#include "sounds.h"
+
+#define SOUND_OFS(asset_id) ((asset_id) - ASSET_SOUND_FIRST - 1)
+
+void
+sounds_init(
+ Mix_Chunk ** const sounds
+) {
+ for (asset_id_t id = ASSET_SOUND_FIRST + 1; id < ASSET_SOUND_LAST; id++) {
+ // get asset
+ const asset_t * const asset = asset_get(id);
+
+ // create rwops
+ SDL_RWops *rw = SDL_RWFromConstMem(asset->buf, asset->len);
+ if (!rw) {
+ die("SDL_RWFromConstMem(): %s", SDL_GetError());
+ }
+
+ // load chunk
+ Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1);
+ if (!chunk) {
+ die("Mix_LoadWAV_RW(): %s", Mix_GetError());
+ }
+
+ // add to list of sounds
+ sounds[SOUND_OFS(id)] = chunk;
+ }
+}
+
+void
+sound_play(
+ Mix_Chunk ** const sounds,
+ const asset_id_t id
+) {
+ // check ID
+ if (id <= ASSET_SOUND_FIRST || id >= ASSET_SOUND_LAST) {
+ die("invalid sound asset id: %u", id);
+ }
+
+ // play sound
+ if (Mix_PlayChannel(-1, sounds[SOUND_OFS(id)], 0)) {
+ die("Mix_PlayChannel(): %s", Mix_GetError());
+ }
+}