Files
orni-attack/source/core/audio/audio_cache.cpp
T
JailDesigner 7ee359b910 Fase 1d: rename del codi restant (effects, stage_system, locals)
Sweep final del naming a CamelCase/camelBack/lower_case:

Fitxers renombrats:
- effects/gestor_puntuacio_flotant.{hpp,cpp} -> floating_score_manager.{hpp,cpp}
- effects/puntuacio_flotant.hpp -> floating_score.hpp

Tipus (CamelCase):
- GestorPuntuacioFlotant -> FloatingScoreManager
- PuntuacioFlotant -> FloatingScore
- ConfigStage -> StageConfig
- ConfigSistemaStages -> StageSystemConfig
- NauTitol -> TitleShip
- EstatNau -> ShipState

Metodes publics (camelBack):
- obte_renderer -> getRenderer
- get_num_actius -> getActiveCount
- calcular_direccio_explosio -> computeExplosionDirection
- trobar_slot_lliure -> findFreeSlot
- explotar -> explode
- reiniciar -> reset
- es_valida -> isValid
- parsejar_fitxer -> parseFile
- carregar -> load
- crear_explosio -> createExplosion
- registrar_puntuacio -> registerScore
- construir_marcador -> buildScoreboard
- render_centered -> renderCentered

Camps struct publics (snake_case):
- actiu/actius -> active
- rotacio -> rotation, rotacio_visual -> visual_rotation
- acceleracio -> acceleration
- velocitat -> velocity
- escala/escala_inicial/objectiu/actual -> scale/initial_scale/...
- posicio/posicio_inicial/objectiu/actual -> position/initial_position/...
- fase_oscilacio -> oscillation_phase
- temps_estat -> state_time
- jugador_id -> player_id
- estat -> state
- brillantor -> brightness
- tipus -> type

Camps privats (sufix _):
- naus_ -> ships_, orni_ -> enemies_, bales_ -> bullets_
- gestor_puntuacio_ -> floating_score_manager_
- punt_mort_ -> death_position_, punt_spawn_ -> spawn_position_
- itocado_per_jugador_ -> hit_timer_per_player_
- vides_per_jugador_ -> lives_per_player_
- puntuacio_per_jugador_ -> score_per_player_
- estat_game_over_ -> game_over_state_
- continues_usados_ -> continues_used_

Constants:
- MARGE_ESQ/DRET/DALT/BAIX -> MARGIN_LEFT/RIGHT/TOP/BOTTOM

Variables locals i parametres comuns (snake_case):
- nau -> ship, enemic -> enemy, bala -> bullet
- forma -> shape, punt(s) -> point(s)
- jugador -> player, partida -> match
- temps -> time, missatge -> message

Diff: 59 fitxers, +1000/-1000 (simetric). Compila i enllaça.

Pendents per a futures fases (no bloquejants):
- Comentaris de capçalera en catala -> castella
- Variables locals/parametres minoritaris en catala
- Include guards (queden alguns #ifndef en lloc de #pragma once)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 11:44:45 +02:00

143 lines
4.3 KiB
C++

// audio_cache.cpp - Implementació del caché de sons i música
// © 2025 Port a C++20 amb SDL3
#include "core/audio/audio_cache.hpp"
#include <iostream>
#include "core/resources/resource_helper.hpp"
// Inicialització de variables estàtiques
std::unordered_map<std::string, JA_Sound_t*> AudioCache::sounds_;
std::unordered_map<std::string, JA_Music_t*> AudioCache::musics_;
std::string AudioCache::sounds_base_path_ = "data/sounds/";
std::string AudioCache::music_base_path_ = "data/music/";
JA_Sound_t* AudioCache::getSound(const std::string& name) {
// Cache hit
auto it = sounds_.find(name);
if (it != sounds_.end()) {
std::cout << "[AudioCache] Sound cache hit: " << name << std::endl;
return it->second;
}
// Normalize path: "laser_shoot.wav" → "sounds/laser_shoot.wav"
std::string normalized = name;
if (normalized.find("sounds/") != 0) {
normalized = "sounds/" + normalized;
}
// Load from resource system
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
if (data.empty()) {
std::cerr << "[AudioCache] Error: no s'ha pogut load " << normalized << std::endl;
return nullptr;
}
// Load sound from memory
JA_Sound_t* sound = JA_LoadSound(data.data(), static_cast<uint32_t>(data.size()));
if (sound == nullptr) {
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
<< std::endl;
return nullptr;
}
std::cout << "[AudioCache] Sound loaded: " << normalized << std::endl;
sounds_[name] = sound;
return sound;
}
JA_Music_t* AudioCache::getMusic(const std::string& name) {
// Cache hit
auto it = musics_.find(name);
if (it != musics_.end()) {
std::cout << "[AudioCache] Music cache hit: " << name << std::endl;
return it->second;
}
// Normalize path: "title.ogg" → "music/title.ogg"
std::string normalized = name;
if (normalized.find("music/") != 0) {
normalized = "music/" + normalized;
}
// Load from resource system
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
if (data.empty()) {
std::cerr << "[AudioCache] Error: no s'ha pogut load " << normalized << std::endl;
return nullptr;
}
// Load music from memory
JA_Music_t* music = JA_LoadMusic(data.data(), static_cast<uint32_t>(data.size()));
if (music == nullptr) {
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
<< std::endl;
return nullptr;
}
std::cout << "[AudioCache] Music loaded: " << normalized << std::endl;
musics_[name] = music;
return music;
}
void AudioCache::clear() {
std::cout << "[AudioCache] Clearing cache (" << sounds_.size() << " sounds, "
<< musics_.size() << " music)" << std::endl;
// Liberar memoria de sonidos
for (auto& [name, sound] : sounds_) {
if (sound && sound->buffer) {
SDL_free(sound->buffer);
}
delete sound;
}
sounds_.clear();
// Liberar memoria de música
for (auto& [name, music] : musics_) {
if (music && music->buffer) {
SDL_free(music->buffer);
}
if (music && music->filename) {
free(music->filename);
}
delete music;
}
musics_.clear();
}
size_t AudioCache::getSoundCacheSize() { return sounds_.size(); }
size_t AudioCache::getMusicCacheSize() { return musics_.size(); }
std::string AudioCache::resolveSoundPath(const std::string& name) {
// Si es un path absoluto (comienza con '/'), usarlo directamente
if (!name.empty() && name[0] == '/') {
return name;
}
// Si ya contiene el prefix base_path, usarlo directamente
if (name.find(sounds_base_path_) == 0) {
return name;
}
// Caso contrario, añadir base_path
return sounds_base_path_ + name;
}
std::string AudioCache::resolveMusicPath(const std::string& name) {
// Si es un path absoluto (comienza con '/'), usarlo directamente
if (!name.empty() && name[0] == '/') {
return name;
}
// Si ya contiene el prefix base_path, usarlo directamente
if (name.find(music_base_path_) == 0) {
return name;
}
// Caso contrario, añadir base_path
return music_base_path_ + name;
}