breakhack/src/mixer.c

171 lines
4.7 KiB
C
Raw Normal View History

2018-02-16 18:11:26 +01:00
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2018-02-21 00:29:21 +01:00
#include <SDL_mixer.h>
#include "mixer.h"
#include "util.h"
#include "io_util.h"
2018-03-17 00:04:26 +01:00
#include "settings.h"
#include "random.h"
static Mix_Chunk *effects[LAST_EFFECT];
static Mix_Music *current_song = NULL;
static Music loaded_song = LAST_MUSIC;
static char *music[LAST_MUSIC] = {
"Sounds/Music/fantasy-game-background-looping.ogg", // GAME_MUSIC0
"Sounds/Music/bog-creatures-on-the-move-looping.ogg", // GAME_MUSIC1
2018-03-01 06:47:16 +01:00
"Sounds/Music/fantascape-looping.ogg", // GAME_MUSIC2
"Sounds/Music/fantasy-forest-battle.ogg" // MENU_MUSIC
};
2018-02-15 14:00:59 +01:00
static Mix_Music*
load_song(char *path)
{
Mix_Music *m = Mix_LoadMUS_RW(io_load_rwops(path), true);
2018-02-15 14:00:59 +01:00
if (m == NULL)
fatal("Failed to load music: %s", Mix_GetError());
return m;
}
static Mix_Chunk*
load_effect(char *path)
{
Mix_Chunk *effect = Mix_LoadWAV_RW(io_load_rwops(path), true);
if (effect == NULL)
2018-02-15 00:02:23 +01:00
fatal("Failed to load effect: %s", Mix_GetError());
return effect;
}
static void
load_effects(void)
{
effects[CLICK] = load_effect("Sounds/FX/click.wav");
effects[SWING0] = load_effect("Sounds/FX/swing.wav");
effects[SWING1] = load_effect("Sounds/FX/swing2.wav");
effects[SWING2] = load_effect("Sounds/FX/swing3.wav");
2018-03-01 19:46:23 +01:00
effects[SWOOSH] = load_effect("Sounds/FX/swoosh.wav");
2018-03-01 06:47:16 +01:00
effects[TRIPPLE_SWING] = load_effect("Sounds/FX/tripple_swing.wav");
effects[SWORD_HIT] = load_effect("Sounds/FX/sword_hit.wav");
2018-03-01 06:47:16 +01:00
effects[DOUBLE_SWORD_HIT] = load_effect("Sounds/FX/double_sword_hit.wav");
effects[TRIPPLE_SWORD_HIT] = load_effect("Sounds/FX/tripple_sword_hit.wav");
effects[BONK] = load_effect("Sounds/FX/bonk.wav");
effects[DEATH] = load_effect("Sounds/FX/death.wav");
effects[COIN] = load_effect("Sounds/FX/coin.wav");
effects[BOTTLE] = load_effect("Sounds/FX/bottle.wav");
effects[BUBBLE0] = load_effect("Sounds/FX/bubble.wav");
effects[BUBBLE1] = load_effect("Sounds/FX/bubble2.wav");
effects[BUBBLE2] = load_effect("Sounds/FX/bubble3.wav");
effects[EAT] = load_effect("Sounds/FX/eat.wav");
effects[LEVEL_UP] = load_effect("Sounds/FX/level_up.wav");
effects[NEXT_LEVEL] = load_effect("Sounds/FX/next_level.wav");
effects[SPLAT] = load_effect("Sounds/FX/splat.wav");
effects[PLAYER_HIT0] = load_effect("Sounds/FX/fistpunch_vocal_01.wav");
effects[PLAYER_HIT1] = load_effect("Sounds/FX/fistpunch_vocal_02.wav");
effects[PLAYER_HIT2] = load_effect("Sounds/FX/fistpunch_vocal_03.wav");
effects[DAGGER_PICKUP] = load_effect("Sounds/FX/dagger_pickup.wav");
2018-05-16 15:52:39 +02:00
effects[FALL0] = load_effect("Sounds/FX/fall0.wav");
effects[FALL1] = load_effect("Sounds/FX/fall1.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());
}
load_effects();
}
2018-02-20 10:45:54 +01:00
bool
mixer_toggle_sound(void)
{
2018-03-17 00:04:26 +01:00
Settings *settings = settings_get();
settings->sound_enabled = !settings->sound_enabled;
return settings->sound_enabled;
2018-02-20 10:45:54 +01:00
}
bool
2018-03-17 09:03:29 +01:00
mixer_toggle_music(GameState *state)
2018-02-20 10:45:54 +01:00
{
2018-03-17 00:04:26 +01:00
Settings *settings = settings_get();
settings->music_enabled = !settings->music_enabled;
2018-02-20 10:45:54 +01:00
2018-03-17 00:04:26 +01:00
if (Mix_PlayingMusic() && !settings->music_enabled)
2018-02-20 10:45:54 +01:00
Mix_PauseMusic();
else if (Mix_PausedMusic())
Mix_ResumeMusic();
2018-03-17 09:03:29 +01:00
else {
if (*state == MENU)
mixer_play_music(MENU_MUSIC);
else
mixer_play_music(GAME_MUSIC0 + get_random(2));
}
2018-02-20 10:45:54 +01:00
2018-03-17 00:04:26 +01:00
return settings->music_enabled;
2018-02-20 10:45:54 +01:00
}
void
mixer_play_effect(Fx fx)
{
2018-03-17 00:04:26 +01:00
if (!settings_get()->sound_enabled)
2018-02-20 10:45:54 +01:00
return;
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
2018-02-15 14:00:59 +01:00
mixer_play_music(Music mus)
2018-02-15 00:02:23 +01:00
{
2018-03-17 00:04:26 +01:00
if (!settings_get()->music_enabled)
2018-02-20 10:45:54 +01:00
return;
if (mus != loaded_song) {
if (current_song)
Mix_FreeMusic(current_song);
current_song = load_song(music[mus]);
loaded_song = mus;
}
2018-02-15 00:02:23 +01:00
if (Mix_PlayingMusic())
2018-02-15 14:00:59 +01:00
mixer_stop_music();
2018-02-15 00:02:23 +01:00
if (Mix_PlayMusic(current_song, -1) == -1)
2018-02-15 00:02:23 +01:00
fatal("Failed to play music");
}
void
mixer_stop_music(void)
{
if (Mix_PlayingMusic())
Mix_HaltMusic();
}
void
mixer_close(void)
{
2018-02-15 14:00:59 +01:00
for (size_t i = 0; i < LAST_EFFECT; ++i)
Mix_FreeChunk(effects[i]);
if (current_song)
Mix_FreeMusic(current_song);
Mix_CloseAudio();
}