Files
esqueleto/source/core/audio/audio.hpp
2026-03-24 21:59:22 +01:00

105 lines
3.2 KiB
C++

#pragma once
#include <string>
#include <unordered_map>
#include <utility>
struct JA_Sound_t;
struct JA_Music_t;
// --- Clase Audio: gestor de audio (singleton) ---
class Audio {
public:
// --- Enums ---
enum class Group : int {
ALL = -1,
GAME = 0,
INTERFACE = 1
};
enum class MusicState {
PLAYING,
PAUSED,
STOPPED,
};
// --- Constantes ---
static constexpr float MAX_VOLUME = 1.0F;
static constexpr float MIN_VOLUME = 0.0F;
static constexpr int FREQUENCY = 48000;
// --- Singleton ---
static void init();
static void destroy();
static auto get() -> Audio*;
Audio(const Audio&) = delete;
auto operator=(const Audio&) -> Audio& = delete;
static void update();
// --- Control de música ---
// 'path' es la ruta al fichero de música (.ogg). Se cachea automáticamente.
void playMusic(const std::string& path, int loop = -1);
void pauseMusic();
void resumeMusic();
void stopMusic();
void fadeOutMusic(int milliseconds) const;
// --- Control de sonidos ---
// 'path' es la ruta al fichero WAV. Se cachea automáticamente.
void playSound(const std::string& path, Group group = Group::GAME) const;
void playSound(JA_Sound_t* sound, Group group = Group::GAME) const;
void stopAllSounds() const;
// --- Control de volumen ---
void setSoundVolume(float volume, Group group = Group::ALL) const;
void setMusicVolume(float volume) const;
// --- Configuración general ---
void enable(bool value);
void toggleEnabled() { enabled_ = !enabled_; }
void applySettings();
// --- Configuración de sonidos ---
void enableSound() { sound_enabled_ = true; }
void disableSound() { sound_enabled_ = false; }
void enableSound(bool value) { sound_enabled_ = value; }
void toggleSound() { sound_enabled_ = !sound_enabled_; }
// --- Configuración de música ---
void enableMusic() { music_enabled_ = true; }
void disableMusic() { music_enabled_ = false; }
void enableMusic(bool value) { music_enabled_ = value; }
void toggleMusic() { music_enabled_ = !music_enabled_; }
// --- Consultas de estado ---
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }
[[nodiscard]] auto isSoundEnabled() const -> bool { return sound_enabled_; }
[[nodiscard]] auto isMusicEnabled() const -> bool { return music_enabled_; }
[[nodiscard]] auto getMusicState() const -> MusicState { return music_.state; }
[[nodiscard]] static auto getRealMusicState() -> MusicState;
[[nodiscard]] auto getCurrentMusicName() const -> const std::string& { return music_.name; }
private:
struct Music {
MusicState state{MusicState::STOPPED};
std::string name;
bool loop{false};
};
Audio();
~Audio();
void initSDLAudio();
static Audio* instance;
Music music_;
bool enabled_{true};
bool sound_enabled_{true};
bool music_enabled_{true};
// Caché de recursos cargados (ruta → recurso)
mutable std::unordered_map<std::string, JA_Music_t*> music_cache_;
mutable std::unordered_map<std::string, JA_Sound_t*> sound_cache_;
};