2018-02-14 16:04:40 +01:00
|
|
|
#include <SDL2/SDL_mixer.h>
|
|
|
|
#include "mixer.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static Mix_Music *music = NULL;
|
|
|
|
static Mix_Chunk *effects[LAST_EFFECT];
|
|
|
|
|
|
|
|
static Mix_Chunk*
|
|
|
|
load_effect(char *path)
|
|
|
|
{
|
|
|
|
Mix_Chunk *effect = Mix_LoadWAV(path);
|
|
|
|
if (effect == NULL)
|
2018-02-15 00:02:23 +01:00
|
|
|
fatal("Failed to load effect: %s", Mix_GetError());
|
2018-02-14 16:04:40 +01:00
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_effects(void)
|
|
|
|
{
|
|
|
|
effects[CLICK] = load_effect("assets/Sounds/FX/click.wav");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mixer_init(void)
|
|
|
|
{
|
|
|
|
if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) == -1) {
|
2018-02-15 00:02:23 +01:00
|
|
|
fatal("Failed to load sound: %s", Mix_GetError());
|
2018-02-14 16:04:40 +01:00
|
|
|
}
|
|
|
|
load_effects();
|
2018-02-15 00:02:23 +01:00
|
|
|
music = Mix_LoadMUS("assets/Sounds/Music/fantasy-forest-battle.ogg");
|
|
|
|
if (music == NULL)
|
|
|
|
fatal("Failed to load music: %s", Mix_GetError());
|
2018-02-14 16:04:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mixer_play_effect(Fx fx)
|
|
|
|
{
|
|
|
|
if (Mix_PlayChannel( -1, effects[fx], 0) == -1)
|
|
|
|
error("Unable to play sound: %u", (unsigned int) fx);
|
|
|
|
}
|
|
|
|
|
2018-02-15 00:02:23 +01:00
|
|
|
void
|
|
|
|
mixer_play_music(void)
|
|
|
|
{
|
|
|
|
if (Mix_PlayingMusic())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Mix_PlayMusic(music, -1) == -1)
|
|
|
|
fatal("Failed to play music");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mixer_stop_music(void)
|
|
|
|
{
|
|
|
|
if (Mix_PlayingMusic())
|
|
|
|
Mix_HaltMusic();
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:04:40 +01:00
|
|
|
void
|
|
|
|
mixer_close(void)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < LAST_EFFECT; ++i) {
|
|
|
|
Mix_FreeChunk(effects[i]);
|
|
|
|
}
|
|
|
|
if (music)
|
|
|
|
Mix_FreeMusic(music);
|
|
|
|
Mix_CloseAudio();
|
|
|
|
}
|