aboutsummaryrefslogtreecommitdiff
path: root/src/sfx/sdl-1.2
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-07-03 00:49:03 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:20 +0200
commit6b9f686913efc3725b2690033cd4f398e07076ba (patch)
treee9aa91a6b9f617d78123ebe7ad272fc42a60d306 /src/sfx/sdl-1.2
parentc9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff)
downloadjancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz
Add project source code
Diffstat (limited to 'src/sfx/sdl-1.2')
-rw-r--r--src/sfx/sdl-1.2/inc/sfx/port.h11
-rw-r--r--src/sfx/sdl-1.2/src/sound.c71
2 files changed, 82 insertions, 0 deletions
diff --git a/src/sfx/sdl-1.2/inc/sfx/port.h b/src/sfx/sdl-1.2/inc/sfx/port.h
new file mode 100644
index 0000000..d8418f7
--- /dev/null
+++ b/src/sfx/sdl-1.2/inc/sfx/port.h
@@ -0,0 +1,11 @@
+#ifndef SFX_SDL1_2_H
+#define SFX_SDL1_2_H
+
+#include <SDL/SDL_mixer.h>
+
+struct sound
+{
+ Mix_Chunk *chunk;
+};
+
+#endif /* SFX_SDL1_2_H */
diff --git a/src/sfx/sdl-1.2/src/sound.c b/src/sfx/sdl-1.2/src/sound.c
new file mode 100644
index 0000000..e843923
--- /dev/null
+++ b/src/sfx/sdl-1.2/src/sound.c
@@ -0,0 +1,71 @@
+#include <sfx.h>
+#include <sfx/port.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_mixer.h>
+#include <stdio.h>
+
+void sfx_free(struct sound *const s)
+{
+ if (s && s->chunk)
+ Mix_FreeChunk(s->chunk);
+}
+
+int sfx_play(const struct sound *const s)
+{
+ if (Mix_PlayChannel(-1, s->chunk, 0) < 0)
+ return -1;
+
+ return 0;
+}
+
+int sfx_sound_from_fp(struct sound *const s, FILE *const f)
+{
+ int ret = -1;
+ SDL_RWops *const ops = SDL_RWFromFP(f, 0);
+
+ if (!ops)
+ {
+ fprintf(stderr, "SDL_RWFromFP: %s\n", SDL_GetError());
+ goto end;
+ }
+ else if (!(s->chunk = Mix_LoadWAV_RW(ops, 0)))
+ {
+ fprintf(stderr, "Mix_LoadWAV_RW: %s\n", SDL_GetError());
+ goto end;
+ }
+
+ ret = 0;
+
+end:
+ SDL_FreeRW(ops);
+ return ret;
+}
+
+void sfx_deinit(void)
+{
+ Mix_CloseAudio();
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
+}
+
+int sfx_init(void)
+{
+ enum {CHUNK_SZ = 4096};
+
+ if (SDL_InitSubSystem(SDL_INIT_AUDIO))
+ {
+ fprintf(stderr, "SDL_InitSubSystem: %s\n", SDL_GetError());
+ goto failure;
+ }
+ else if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,
+ MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, CHUNK_SZ))
+ {
+ fprintf(stderr, "Mix_OpenAudio: %s\n", SDL_GetError());
+ goto failure;
+ }
+
+ return 0;
+
+failure:
+ sfx_deinit();
+ return -1;
+}