refactor: JA_* a namespace Ja:: (estil aee_arcade)

This commit is contained in:
2026-05-16 14:15:25 +02:00
parent 08f587ffe4
commit 1ce0d9c56c
15 changed files with 645 additions and 719 deletions
+27 -27
View File
@@ -40,15 +40,15 @@ Audio::Audio() { initSDLAudio(); }
// Destructor
Audio::~Audio() {
JA_Quit();
Ja::quit();
}
// Método principal
void Audio::update() {
JA_Update();
Ja::update();
// Sincronizar estado: detectar cuando la música se para (ej. fade-out completado)
if (instance && instance->music_.state == MusicState::PLAYING && JA_GetMusicState() != JA_MUSIC_PLAYING) {
if (instance && instance->music_.state == MusicState::PLAYING && Ja::getMusicState() != Ja::MusicState::PLAYING) {
instance->music_.state = MusicState::STOPPED;
}
}
@@ -72,12 +72,12 @@ void Audio::playMusic(const std::string& name, const int loop, const int crossfa
}
if (crossfade_ms > 0 && music_.state == MusicState::PLAYING) {
JA_CrossfadeMusic(resource, crossfade_ms, loop);
Ja::crossfadeMusic(resource, crossfade_ms, loop);
} else {
if (music_.state == MusicState::PLAYING) {
JA_StopMusic();
Ja::stopMusic();
}
JA_PlayMusic(resource, loop);
Ja::playMusic(resource, loop);
}
music_.name = name;
@@ -86,18 +86,18 @@ void Audio::playMusic(const std::string& name, const int loop, const int crossfa
}
// Reproduce la música por puntero (con crossfade opcional)
void Audio::playMusic(JA_Music_t* music, const int loop, const int crossfade_ms) {
void Audio::playMusic(Ja::Music* music, const int loop, const int crossfade_ms) {
if (!music_enabled_ || music == nullptr) {
return;
}
if (crossfade_ms > 0 && music_.state == MusicState::PLAYING) {
JA_CrossfadeMusic(music, crossfade_ms, loop);
Ja::crossfadeMusic(music, crossfade_ms, loop);
} else {
if (music_.state == MusicState::PLAYING) {
JA_StopMusic();
Ja::stopMusic();
}
JA_PlayMusic(music, loop);
Ja::playMusic(music, loop);
}
music_.name.clear(); // nom desconegut quan es passa per punter
@@ -108,7 +108,7 @@ void Audio::playMusic(JA_Music_t* music, const int loop, const int crossfade_ms)
// Pausa la música
void Audio::pauseMusic() {
if (music_enabled_ && music_.state == MusicState::PLAYING) {
JA_PauseMusic();
Ja::pauseMusic();
music_.state = MusicState::PAUSED;
}
}
@@ -116,7 +116,7 @@ void Audio::pauseMusic() {
// Continua la música pausada
void Audio::resumeMusic() {
if (music_enabled_ && music_.state == MusicState::PAUSED) {
JA_ResumeMusic();
Ja::resumeMusic();
music_.state = MusicState::PLAYING;
}
}
@@ -124,7 +124,7 @@ void Audio::resumeMusic() {
// Detiene la música
void Audio::stopMusic() {
if (music_enabled_) {
JA_StopMusic();
Ja::stopMusic();
music_.state = MusicState::STOPPED;
}
}
@@ -132,42 +132,42 @@ void Audio::stopMusic() {
// Reproduce un sonido por nombre
void Audio::playSound(const std::string& name, Group group) const {
if (sound_enabled_) {
JA_PlaySound(AudioResource::getSound(name), 0, static_cast<int>(group));
Ja::playSound(AudioResource::getSound(name), 0, static_cast<int>(group));
}
}
// Reproduce un sonido por puntero directo
void Audio::playSound(JA_Sound_t* sound, Group group) const {
void Audio::playSound(Ja::Sound* sound, Group group) const {
if (sound_enabled_ && sound != nullptr) {
JA_PlaySound(sound, 0, static_cast<int>(group));
Ja::playSound(sound, 0, static_cast<int>(group));
}
}
// Detiene todos los sonidos
void Audio::stopAllSounds() const {
if (sound_enabled_) {
JA_StopChannel(-1);
Ja::stopChannel(-1);
}
}
// Realiza un fundido de salida de la música
void Audio::fadeOutMusic(int milliseconds) const {
if (music_enabled_ && getRealMusicState() == MusicState::PLAYING) {
JA_FadeOutMusic(milliseconds);
Ja::fadeOutMusic(milliseconds);
}
}
// Consulta directamente el estado real de la música en jailaudio
auto Audio::getRealMusicState() -> MusicState {
JA_Music_state ja_state = JA_GetMusicState();
Ja::MusicState ja_state = Ja::getMusicState();
switch (ja_state) {
case JA_MUSIC_PLAYING:
case Ja::MusicState::PLAYING:
return MusicState::PLAYING;
case JA_MUSIC_PAUSED:
case Ja::MusicState::PAUSED:
return MusicState::PAUSED;
case JA_MUSIC_STOPPED:
case JA_MUSIC_INVALID:
case JA_MUSIC_DISABLED:
case Ja::MusicState::STOPPED:
case Ja::MusicState::INVALID:
case Ja::MusicState::DISABLED:
default:
return MusicState::STOPPED;
}
@@ -178,7 +178,7 @@ void Audio::setSoundVolume(float sound_volume, Group group) const {
sound_volume = std::clamp(sound_volume, MIN_VOLUME, MAX_VOLUME);
const bool active = enabled_ && sound_enabled_;
const float CONVERTED_VOLUME = active ? sound_volume * Options::audio.volume : 0.0F;
JA_SetSoundVolume(CONVERTED_VOLUME, static_cast<int>(group));
Ja::setSoundVolume(CONVERTED_VOLUME, static_cast<int>(group));
}
// Establece el volumen de la música (float 0.0..1.0)
@@ -186,7 +186,7 @@ void Audio::setMusicVolume(float music_volume) const {
music_volume = std::clamp(music_volume, MIN_VOLUME, MAX_VOLUME);
const bool active = enabled_ && music_enabled_;
const float CONVERTED_VOLUME = active ? music_volume * Options::audio.volume : 0.0F;
JA_SetMusicVolume(CONVERTED_VOLUME);
Ja::setMusicVolume(CONVERTED_VOLUME);
}
// Aplica la configuración
@@ -207,7 +207,7 @@ void Audio::initSDLAudio() {
if (!SDL_Init(SDL_INIT_AUDIO)) {
std::cout << "SDL_AUDIO could not initialize! SDL Error: " << SDL_GetError() << '\n';
} else {
JA_Init(FREQUENCY, SDL_AUDIO_S16LE, 2);
Ja::init(FREQUENCY, SDL_AUDIO_S16LE, 2);
enable(Options::audio.enabled);
}
}
+7 -2
View File
@@ -6,6 +6,11 @@
#include <string> // Para string
#include <utility> // Para move
namespace Ja {
struct Music;
struct Sound;
} // namespace Ja
// --- Clase Audio: gestor de audio (singleton) ---
// Implementació canònica, byte-idèntica entre projectes.
// Els volums es manegen internament com a float 0.01.0; la capa de
@@ -45,7 +50,7 @@ class Audio {
// --- Control de música ---
void playMusic(const std::string& name, int loop = -1, int crossfade_ms = 0); // Reproducir música por nombre (con crossfade opcional)
void playMusic(struct JA_Music_t* music, int loop = -1, int crossfade_ms = 0); // Reproducir música por puntero (con crossfade opcional)
void playMusic(Ja::Music* music, int loop = -1, int crossfade_ms = 0); // Reproducir música por puntero (con crossfade opcional)
void pauseMusic(); // Pausar reproducción de música
void resumeMusic(); // Continua la música pausada
void stopMusic(); // Detener completamente la música
@@ -53,7 +58,7 @@ class Audio {
// --- Control de sonidos ---
void playSound(const std::string& name, Group group = Group::GAME) const; // Reproducir sonido puntual por nombre
void playSound(struct JA_Sound_t* sound, Group group = Group::GAME) const; // Reproducir sonido puntual por puntero
void playSound(Ja::Sound* sound, Group group = Group::GAME) const; // Reproducir sonido puntual por puntero
void stopAllSounds() const; // Detener todos los sonidos
// --- Control de volumen (API interna: float 0.0..1.0) ---
+2 -2
View File
@@ -4,11 +4,11 @@
namespace AudioResource {
auto getMusic(const std::string& name) -> JA_Music_t* {
auto getMusic(const std::string& name) -> Ja::Music* {
return Resource::Cache::get()->getMusic(name);
}
auto getSound(const std::string& name) -> JA_Sound_t* {
auto getSound(const std::string& name) -> Ja::Sound* {
return Resource::Cache::get()->getSound(name);
}
+8 -6
View File
@@ -1,17 +1,19 @@
#pragma once
// --- Audio Resource Adapter ---
// Aquest fitxer exposa una interfície comuna a Audio per obtenir JA_Music_t* /
// JA_Sound_t* per nom. Cada projecte la implementa en audio_adapter.cpp
// Aquest fitxer exposa una interfície comuna a Audio per obtenir Ja::Music* /
// Ja::Sound* per nom. Cada projecte la implementa en audio_adapter.cpp
// delegant al seu singleton de recursos (Resource::get(), Resource::Cache::get(),
// etc.). Això permet que audio.hpp/audio.cpp siguin idèntics entre projectes.
#include <string> // Para string
struct JA_Music_t;
struct JA_Sound_t;
namespace Ja {
struct Music;
struct Sound;
} // namespace Ja
namespace AudioResource {
auto getMusic(const std::string& name) -> JA_Music_t*;
auto getSound(const std::string& name) -> JA_Sound_t*;
auto getMusic(const std::string& name) -> Ja::Music*;
auto getSound(const std::string& name) -> Ja::Sound*;
} // namespace AudioResource
File diff suppressed because it is too large Load Diff
+8 -8
View File
@@ -33,7 +33,7 @@ namespace Resource {
}
} // namespace
auto Cache::getMusic(const std::string& name) -> JA_Music_t* {
auto Cache::getMusic(const std::string& name) -> Ja::Music* {
auto it = std::ranges::find_if(musics_, [&](const auto& m) { return m.name == name; });
if (it != musics_.end()) {
return it->music.get();
@@ -42,7 +42,7 @@ namespace Resource {
throw std::runtime_error("Music not found: " + name);
}
auto Cache::getSound(const std::string& name) -> JA_Sound_t* {
auto Cache::getSound(const std::string& name) -> Ja::Sound* {
auto it = std::ranges::find_if(sounds_, [&](const auto& s) { return s.name == name; });
if (it != sounds_.end()) {
return it->sound.get();
@@ -192,12 +192,12 @@ namespace Resource {
std::cerr << "Resource::Cache: no s'ha pogut llegir " << path << '\n';
return;
}
JA_Music_t* music = JA_LoadMusic(bytes.data(), static_cast<Uint32>(bytes.size()), path.c_str());
Ja::Music* music = Ja::loadMusic(bytes.data(), static_cast<Uint32>(bytes.size()), path.c_str());
if (music == nullptr) {
std::cerr << "Resource::Cache: JA_LoadMusic ha fallat per " << path << '\n';
std::cerr << "Resource::Cache: Ja::loadMusic ha fallat per " << path << '\n';
return;
}
musics_.push_back(MusicResource{.name = name, .music = std::unique_ptr<JA_Music_t, MusicDeleter>(music)});
musics_.push_back(MusicResource{.name = name, .music = std::unique_ptr<Ja::Music, MusicDeleter>(music)});
++loaded_count_;
std::cout << " [music ] " << name << '\n';
}
@@ -213,12 +213,12 @@ namespace Resource {
std::cerr << "Resource::Cache: no s'ha pogut llegir " << path << '\n';
return;
}
JA_Sound_t* sound = JA_LoadSound(bytes.data(), static_cast<uint32_t>(bytes.size()));
Ja::Sound* sound = Ja::loadSound(bytes.data(), static_cast<uint32_t>(bytes.size()));
if (sound == nullptr) {
std::cerr << "Resource::Cache: JA_LoadSound ha fallat per " << path << '\n';
std::cerr << "Resource::Cache: Ja::loadSound ha fallat per " << path << '\n';
return;
}
sounds_.push_back(SoundResource{.name = name, .sound = std::unique_ptr<JA_Sound_t, SoundDeleter>(sound)});
sounds_.push_back(SoundResource{.name = name, .sound = std::unique_ptr<Ja::Sound, SoundDeleter>(sound)});
++loaded_count_;
std::cout << " [sound ] " << name << '\n';
}
+2 -2
View File
@@ -27,8 +27,8 @@ namespace Resource {
auto operator=(const Cache&) -> Cache& = delete;
// Getters: throw runtime_error si el nom no existeix al cache.
auto getMusic(const std::string& name) -> JA_Music_t*;
auto getSound(const std::string& name) -> JA_Sound_t*;
auto getMusic(const std::string& name) -> Ja::Music*;
auto getSound(const std::string& name) -> Ja::Sound*;
auto getSurfacePixels(const std::string& name) -> const std::vector<Uint8>&;
auto getPaletteBytes(const std::string& name) -> const std::vector<Uint8>&;
auto getTextFile(const std::string& name) -> const std::vector<uint8_t>&;
+12 -11
View File
@@ -8,38 +8,39 @@
#include <vector>
// Forward declarations to keep this header light.
struct JA_Music_t;
struct JA_Sound_t;
void JA_DeleteMusic(JA_Music_t* music);
void JA_DeleteSound(JA_Sound_t* sound);
namespace Ja {
struct Music;
struct Sound;
void deleteMusic(Music* music);
void deleteSound(Sound* sound);
} // namespace Ja
namespace Resource {
struct MusicDeleter {
void operator()(JA_Music_t* music) const noexcept {
void operator()(Ja::Music* music) const noexcept {
if (music != nullptr) {
JA_DeleteMusic(music);
Ja::deleteMusic(music);
}
}
};
struct SoundDeleter {
void operator()(JA_Sound_t* sound) const noexcept {
void operator()(Ja::Sound* sound) const noexcept {
if (sound != nullptr) {
JA_DeleteSound(sound);
Ja::deleteSound(sound);
}
}
};
struct MusicResource {
std::string name;
std::unique_ptr<JA_Music_t, MusicDeleter> music;
std::unique_ptr<Ja::Music, MusicDeleter> music;
};
struct SoundResource {
std::string name;
std::unique_ptr<JA_Sound_t, SoundDeleter> sound;
std::unique_ptr<Ja::Sound, SoundDeleter> sound;
};
// Una entrada BITMAP descodifica un GIF i emmagatzema els seus