aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-05-22 19:29:25 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-05-24 22:20:02 +0200
commitdea345f412652151c256f20df0793e47673dff2b (patch)
treeaf9220bd5f7c4f9fa00b03969abd3c5d9a4912d7 /src
parent354034b06b6f772ed92c3f26f8e8c680aa93fa8d (diff)
downloadrts-dea345f412652151c256f20df0793e47673dff2b.tar.gz
Allow systems without sound support
Diffstat (limited to 'src')
-rw-r--r--src/container/src/container.c2
-rw-r--r--src/sfx/inc/sfx.h3
-rw-r--r--src/sfx/ps1/src/sound.c2
-rw-r--r--src/sfx/sdl-1.2/src/sound.c36
4 files changed, 32 insertions, 11 deletions
diff --git a/src/container/src/container.c b/src/container/src/container.c
index 5c568a9..a6b8733 100644
--- a/src/container/src/container.c
+++ b/src/container/src/container.c
@@ -67,7 +67,7 @@ static int read_file_contents(const struct container *const el, FILE *const f,
break;
case CONTAINER_TYPE_SOUND:
- if (sfx_sound_from_fp(el->data.sound, f))
+ if (sfx_sound_from_fp(el->data.sound, f, sz))
goto end;
break;
diff --git a/src/sfx/inc/sfx.h b/src/sfx/inc/sfx.h
index b8fe44f..ea290d3 100644
--- a/src/sfx/inc/sfx.h
+++ b/src/sfx/inc/sfx.h
@@ -2,6 +2,7 @@
#define SFX_H
#include <sfx/port.h>
+#include <stddef.h>
#include <stdio.h>
#ifdef __cplusplus
@@ -10,7 +11,7 @@ extern "C"
#endif
int sfx_init(void);
-int sfx_sound_from_fp(struct sound *s, FILE *f);
+int sfx_sound_from_fp(struct sound *s, FILE *f, size_t sz);
int sfx_play(const struct sound *s);
void sfx_free(struct sound *s);
void sfx_deinit(void);
diff --git a/src/sfx/ps1/src/sound.c b/src/sfx/ps1/src/sound.c
index e0377c7..cdb4193 100644
--- a/src/sfx/ps1/src/sound.c
+++ b/src/sfx/ps1/src/sound.c
@@ -171,7 +171,7 @@ int sfx_play(const struct sound *const s)
return 0;
}
-int sfx_sound_from_fp(struct sound *const s, FILE *const f)
+int sfx_sound_from_fp(struct sound *const s, FILE *const f, const size_t sz)
{
struct vag_header h;
diff --git a/src/sfx/sdl-1.2/src/sound.c b/src/sfx/sdl-1.2/src/sound.c
index e3e9a8b..d2ee40a 100644
--- a/src/sfx/sdl-1.2/src/sound.c
+++ b/src/sfx/sdl-1.2/src/sound.c
@@ -3,8 +3,12 @@
#include <header.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
+#include <stdbool.h>
+#include <stddef.h>
#include <stdio.h>
+static bool subsystem_init, mixer_init;
+
void sfx_free(struct sound *const s)
{
if (s && s->chunk)
@@ -13,7 +17,7 @@ void sfx_free(struct sound *const s)
int sfx_play(const struct sound *const s)
{
- if (Mix_PlayChannel(-1, s->chunk, s->loop ? -1 : 0) < 0)
+ if (mixer_init && Mix_PlayChannel(-1, s->chunk, s->loop ? -1 : 0) < 0)
return -1;
return 0;
@@ -50,39 +54,55 @@ static int load_header(struct sound *const s, FILE *const f)
return 0;
}
-int sfx_sound_from_fp(struct sound *const s, FILE *const f)
+int sfx_sound_from_fp(struct sound *const s, FILE *const f, const size_t sz)
{
- if (load_header(s, f) || load_data(s, f))
- return -1;
+ if (mixer_init)
+ {
+ if (load_header(s, f) || load_data(s, f))
+ return -1;
+ }
+ else
+ /* Skip the file. */
+ fseek(f, sz, SEEK_CUR);
return 0;
}
void sfx_deinit(void)
{
- Mix_CloseAudio();
- SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ if (mixer_init)
+ Mix_CloseAudio();
+
+ if (subsystem_init)
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
int sfx_init(void)
{
enum {CHUNK_SZ = 4096};
+ /* Some older systems might not support sound at all. */
if (SDL_InitSubSystem(SDL_INIT_AUDIO))
{
fprintf(stderr, "SDL_InitSubSystem: %s\n", SDL_GetError());
goto failure;
}
- else if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,
+ else
+ subsystem_init = true;
+
+ if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,
MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, CHUNK_SZ))
{
fprintf(stderr, "Mix_OpenAudio: %s\n", SDL_GetError());
goto failure;
}
+ else
+ mixer_init = true;
return 0;
failure:
sfx_deinit();
- return -1;
+ fprintf(stderr, "Running without audio support\n");
+ return 0;
}