Allow systems without sound support

This commit is contained in:
Xavier Del Campo Romero 2022-05-22 19:29:25 +02:00
parent 354034b06b
commit dea345f412
4 changed files with 32 additions and 11 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;
}