134 lines
3.1 KiB
C++
134 lines
3.1 KiB
C++
#include "audio.h"
|
|
#include <SDL3/SDL_audio.h> // for SDL_AudioFormat
|
|
#include <SDL3/SDL_error.h> // for SDL_GetError
|
|
#include <SDL3/SDL_init.h> // for SDL_Init, SDL_INIT_AUDIO
|
|
#include <SDL3/SDL_log.h> // for SDL_LogInfo, SDL_LogCategory, SDL_LogError
|
|
#include <algorithm> // for clamp
|
|
#include "jail_audio.h" // for JA_FadeOutMusic, JA_Init, JA_PauseMusic
|
|
#include "options.h" // for Options, OptionsAudio, options, OptionsM...
|
|
struct JA_Music_t;
|
|
struct JA_Sound_t;
|
|
|
|
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
|
Audio *Audio::audio_ = nullptr;
|
|
|
|
// [SINGLETON] Crearemos el objeto asset con esta función estática
|
|
void Audio::init()
|
|
{
|
|
Audio::audio_ = new Audio();
|
|
}
|
|
|
|
// [SINGLETON] Destruiremos el objeto asset con esta función estática
|
|
void Audio::destroy()
|
|
{
|
|
delete Audio::audio_;
|
|
}
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él
|
|
Audio *Audio::get()
|
|
{
|
|
return Audio::audio_;
|
|
}
|
|
|
|
// Constructor
|
|
Audio::Audio()
|
|
{
|
|
// Inicializa SDL
|
|
if (!SDL_Init(SDL_INIT_AUDIO))
|
|
{
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
|
|
}
|
|
else
|
|
{
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "\n** SDL_AUDIO: INITIALIZING\n");
|
|
|
|
JA_Init(48000, SDL_AUDIO_S16LE, 2);
|
|
enable(options.audio.enabled);
|
|
setMusicVolume(options.audio.music.volume);
|
|
setSoundVolume(options.audio.sound.volume);
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "** SDL_AUDIO: INITIALIZATION COMPLETE\n");
|
|
}
|
|
}
|
|
|
|
// Destructor
|
|
Audio::~Audio()
|
|
{
|
|
JA_Quit();
|
|
}
|
|
|
|
// Reproduce la música
|
|
void Audio::playMusic(JA_Music_t *music, const int loop)
|
|
{
|
|
if (enabled_ && music_enabled_)
|
|
{
|
|
JA_PlayMusic(music, loop);
|
|
}
|
|
}
|
|
|
|
// Pausa la música
|
|
void Audio::pauseMusic()
|
|
{
|
|
if (enabled_ && music_enabled_)
|
|
{
|
|
JA_PauseMusic();
|
|
}
|
|
}
|
|
|
|
// Detiene la música
|
|
void Audio::stopMusic()
|
|
{
|
|
if (enabled_ && music_enabled_)
|
|
{
|
|
JA_StopMusic();
|
|
}
|
|
}
|
|
|
|
// Reproduce un sonido
|
|
void Audio::playSound(JA_Sound_t *sound)
|
|
{
|
|
if (enabled_ && sound_enabled_)
|
|
{
|
|
JA_PlaySound(sound);
|
|
}
|
|
}
|
|
|
|
// Detiene todos los sonidos
|
|
void Audio::stopAllSounds()
|
|
{
|
|
if (enabled_ && sound_enabled_)
|
|
{
|
|
JA_StopChannel(-1);
|
|
}
|
|
}
|
|
|
|
// Realiza un fundido de salida de la música
|
|
void Audio::fadeOutMusic(int milliseconds)
|
|
{
|
|
if (enabled_ && music_enabled_)
|
|
{
|
|
JA_FadeOutMusic(milliseconds);
|
|
}
|
|
}
|
|
|
|
// Establece el volumen de los sonidos
|
|
void Audio::setSoundVolume(int volume)
|
|
{
|
|
if (enabled_ && sound_enabled_)
|
|
{
|
|
volume = std::clamp(volume, 0, 100);
|
|
const int COVERTED_VOLUME = static_cast<int>((volume / 100.0) * 128);
|
|
JA_SetSoundVolume(COVERTED_VOLUME);
|
|
}
|
|
}
|
|
|
|
// Establece el volumen de la música
|
|
void Audio::setMusicVolume(int volume)
|
|
{
|
|
if (enabled_ && music_enabled_)
|
|
{
|
|
volume = std::clamp(volume, 0, 100);
|
|
const int COVERTED_VOLUME = static_cast<int>((volume / 100.0) * 128);
|
|
JA_SetMusicVolume(COVERTED_VOLUME);
|
|
}
|
|
} |