neteja clang-tidy

This commit is contained in:
2026-05-16 22:47:41 +02:00
parent 17341f923d
commit a903343385
40 changed files with 1246 additions and 1384 deletions
+73 -106
View File
@@ -5,16 +5,12 @@
#include <algorithm> // Para ranges::transform, ranges::find_if
#include <array> // Para array
#include <cstdlib> // Para exit
#include <exception> // Para exception
#include <filesystem> // Para exists, path, remove
#include <fstream> // Para basic_ofstream, basic_ios, basic_ostream::write, ios, ofstream
#include <iostream> // Para std::cout
#include <iterator> // Para back_inserter
#include <ranges> // Para __find_if_fn, find_if, __find_fn, find
#include <stdexcept> // Para runtime_error
#include <utility> // Para move
#include "core/audio/jail_audio.hpp" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
#include "core/audio/jail_audio.hpp" // Para Ja::loadMusic, Ja::loadSound, Ja::deleteMusic, Ja::deleteSound
#include "core/locale/lang.hpp" // Para getText
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/text.hpp" // Para Text
@@ -27,9 +23,6 @@
#include "utils/utils.hpp" // Para getFileName
#include "version.h" // Para APP_NAME, GIT_HASH
struct JA_Music_t; // lines 11-11
struct JA_Sound_t; // lines 12-12
// Helper para cargar archivos de audio desde pack o filesystem en memoria
namespace {
struct AudioData {
@@ -140,37 +133,37 @@ void Resource::loadEssentialTextures() {
// Inicializa las listas de recursos sin cargar el contenido (modo lazy)
void Resource::initResourceLists() {
const auto file_to_name = [](const auto& file) { return getFileName(file); };
const auto FILE_TO_NAME = [](const auto& file) { return getFileName(file); };
// Inicializa lista de sonidos
const auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
const auto SOUND_LIST = Asset::get()->getListByType(Asset::Type::SOUND);
sounds_.clear();
sounds_.reserve(sound_list.size());
std::ranges::transform(sound_list, std::back_inserter(sounds_), [&](const auto& file) { return ResourceSound(file_to_name(file)); });
sounds_.reserve(SOUND_LIST.size());
std::ranges::transform(SOUND_LIST, std::back_inserter(sounds_), [&](const auto& file) { return ResourceSound(FILE_TO_NAME(file)); });
// Inicializa lista de músicas
const auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
const auto MUSIC_LIST = Asset::get()->getListByType(Asset::Type::MUSIC);
musics_.clear();
musics_.reserve(music_list.size());
std::ranges::transform(music_list, std::back_inserter(musics_), [&](const auto& file) { return ResourceMusic(file_to_name(file)); });
musics_.reserve(MUSIC_LIST.size());
std::ranges::transform(MUSIC_LIST, std::back_inserter(musics_), [&](const auto& file) { return ResourceMusic(FILE_TO_NAME(file)); });
// Inicializa lista de texturas
const auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
const auto TEXTURE_LIST = Asset::get()->getListByType(Asset::Type::BITMAP);
textures_.clear();
textures_.reserve(texture_list.size());
std::ranges::transform(texture_list, std::back_inserter(textures_), [&](const auto& file) { return ResourceTexture(file_to_name(file)); });
textures_.reserve(TEXTURE_LIST.size());
std::ranges::transform(TEXTURE_LIST, std::back_inserter(textures_), [&](const auto& file) { return ResourceTexture(FILE_TO_NAME(file)); });
// Inicializa lista de ficheros de texto
const auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
const auto TEXT_FILE_LIST = Asset::get()->getListByType(Asset::Type::FONT);
text_files_.clear();
text_files_.reserve(text_file_list.size());
std::ranges::transform(text_file_list, std::back_inserter(text_files_), [&](const auto& file) { return ResourceTextFile(file_to_name(file)); });
text_files_.reserve(TEXT_FILE_LIST.size());
std::ranges::transform(TEXT_FILE_LIST, std::back_inserter(text_files_), [&](const auto& file) { return ResourceTextFile(FILE_TO_NAME(file)); });
// Inicializa lista de animaciones
const auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
const auto ANIMATION_LIST = Asset::get()->getListByType(Asset::Type::ANIMATION);
animations_.clear();
animations_.reserve(animation_list.size());
std::ranges::transform(animation_list, std::back_inserter(animations_), [&](const auto& file) { return ResourceAnimation(file_to_name(file)); });
animations_.reserve(ANIMATION_LIST.size());
std::ranges::transform(ANIMATION_LIST, std::back_inserter(animations_), [&](const auto& file) { return ResourceAnimation(FILE_TO_NAME(file)); });
// Los demos se cargan directamente sin mostrar progreso (son pocos y pequeños)
loadDemoDataQuiet();
@@ -196,7 +189,7 @@ void Resource::initResourceLists() {
}
// Obtiene el sonido a partir de un nombre (con carga perezosa)
auto Resource::getSound(const std::string& name) -> JA_Sound_t* {
auto Resource::getSound(const std::string& name) -> Ja::Sound* {
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) -> auto { return s.name == name; });
if (it != sounds_.end()) {
@@ -212,7 +205,7 @@ auto Resource::getSound(const std::string& name) -> JA_Sound_t* {
}
// Obtiene la música a partir de un nombre (con carga perezosa)
auto Resource::getMusic(const std::string& name) -> JA_Music_t* {
auto Resource::getMusic(const std::string& name) -> Ja::Music* {
auto it = std::ranges::find_if(musics_, [&name](const auto& m) -> auto { return m.name == name; });
if (it != musics_.end()) {
@@ -303,48 +296,48 @@ auto Resource::getDemoData(int index) -> DemoData& {
// --- Métodos de carga perezosa ---
auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
auto Resource::loadSoundLazy(const std::string& name) -> Ja::Sound* {
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
for (const auto& file : sound_list) {
if (getFileName(file) == name) {
auto audio_data = loadAudioData(file);
if (!audio_data.data.empty()) {
return JA_LoadSound(audio_data.data.data(), audio_data.data.size());
return Ja::loadSound(audio_data.data.data(), audio_data.data.size());
}
// Fallback a cargar desde disco si no está en pack
return JA_LoadSound(file.c_str());
return Ja::loadSound(file.c_str());
}
}
return nullptr;
}
auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
auto Resource::loadMusicLazy(const std::string& name) -> Ja::Music* {
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
for (const auto& file : music_list) {
if (getFileName(file) == name) {
auto audio_data = loadAudioData(file);
if (!audio_data.data.empty()) {
return JA_LoadMusic(audio_data.data.data(), audio_data.data.size());
return Ja::loadMusic(audio_data.data.data(), audio_data.data.size());
}
// Fallback a cargar desde disco si no está en pack
return JA_LoadMusic(file.c_str());
return Ja::loadMusic(file.c_str());
}
}
return nullptr;
}
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
const auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
const auto it = std::ranges::find_if(texture_list,
const auto TEXTURE_LIST = Asset::get()->getListByType(Asset::Type::BITMAP);
const auto IT = std::ranges::find_if(TEXTURE_LIST,
[&name](const auto& file) { return getFileName(file) == name; });
return it != texture_list.end() ? std::make_shared<Texture>(Screen::get()->getRenderer(), *it) : nullptr;
return IT != TEXTURE_LIST.end() ? std::make_shared<Texture>(Screen::get()->getRenderer(), *IT) : nullptr;
}
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
const auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
const auto it = std::ranges::find_if(text_file_list,
const auto TEXT_FILE_LIST = Asset::get()->getListByType(Asset::Type::FONT);
const auto IT = std::ranges::find_if(TEXT_FILE_LIST,
[&name](const auto& file) { return getFileName(file) == name; });
return it != text_file_list.end() ? Text::loadFile(*it) : nullptr;
return IT != TEXT_FILE_LIST.end() ? Text::loadFile(*IT) : nullptr;
}
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
@@ -369,22 +362,22 @@ auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
{.key = "smb2", .texture_file = "smb2.png", .text_file = "smb2.txt"},
{.key = "smb2_grad", .texture_file = "smb2_grad.png", .text_file = "smb2.txt"}};
const auto it = std::ranges::find_if(TEXT_MAPPINGS,
const auto IT = std::ranges::find_if(TEXT_MAPPINGS,
[&name](const auto& mapping) { return mapping.key == name; });
if (it == TEXT_MAPPINGS.end()) {
if (IT == TEXT_MAPPINGS.end()) {
return nullptr;
}
auto texture = getTexture(it->texture_file); // Esto cargará la textura si no está cargada
auto text_file = getTextFile(it->text_file); // Esto cargará el archivo de texto si no está cargado
auto texture = getTexture(IT->texture_file); // Esto cargará la textura si no está cargada
auto text_file = getTextFile(IT->text_file); // Esto cargará el archivo de texto si no está cargado
return (texture && text_file) ? std::make_shared<Text>(texture, text_file) : nullptr;
}
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
const auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
const auto it = std::ranges::find_if(animation_list,
const auto ANIMATION_LIST = Asset::get()->getListByType(Asset::Type::ANIMATION);
const auto IT = std::ranges::find_if(ANIMATION_LIST,
[&name](const auto& file) { return getFileName(file) == name; });
if (it != animation_list.end()) {
return loadAnimationsFromFile(*it);
if (IT != ANIMATION_LIST.end()) {
return loadAnimationsFromFile(*IT);
}
// Si no se encuentra, retorna vector vacío
return AnimationsFileBuffer{};
@@ -427,80 +420,54 @@ auto Resource::isLoadDone() const -> bool {
return stage_ == LoadStage::DONE;
}
// Avança una etapa que descarrega una llista d'assets.
void Resource::advanceListLoadStage(const std::vector<std::string>& list, void (Resource::*load_one)(size_t), LoadStage next_stage) {
if (stage_index_ >= list.size()) {
stage_ = next_stage;
stage_index_ = 0;
return;
}
(this->*load_one)(stage_index_++);
}
// Bombea la máquina de etapas hasta agotar el presupuesto de tiempo o completar la carga.
// Devuelve true cuando ya no queda nada por cargar.
auto Resource::loadStep(int budget_ms) -> bool {
if (stage_ == LoadStage::DONE) { return true; }
const Uint64 start_ns = SDL_GetTicksNS();
const Uint64 budget_ns = static_cast<Uint64>(budget_ms) * 1'000'000ULL;
const Uint64 START_NS = SDL_GetTicksNS();
const Uint64 BUDGET_NS = static_cast<Uint64>(budget_ms) * 1'000'000ULL;
while (stage_ != LoadStage::DONE) {
switch (stage_) {
case LoadStage::SOUNDS: {
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
if (stage_index_ == 0) { sounds_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::MUSICS;
stage_index_ = 0;
break;
}
loadOneSound(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::SOUND), &Resource::loadOneSound, LoadStage::MUSICS);
break;
}
case LoadStage::MUSICS: {
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
if (stage_index_ == 0) { musics_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::TEXTURES;
stage_index_ = 0;
break;
}
loadOneMusic(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::MUSIC), &Resource::loadOneMusic, LoadStage::TEXTURES);
break;
}
case LoadStage::TEXTURES: {
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
if (stage_index_ == 0) { textures_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::TEXT_FILES;
stage_index_ = 0;
break;
}
loadOneTexture(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::BITMAP), &Resource::loadOneTexture, LoadStage::TEXT_FILES);
break;
}
case LoadStage::TEXT_FILES: {
auto list = Asset::get()->getListByType(Asset::Type::FONT);
if (stage_index_ == 0) { text_files_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::ANIMATIONS;
stage_index_ = 0;
break;
}
loadOneTextFile(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::FONT), &Resource::loadOneTextFile, LoadStage::ANIMATIONS);
break;
}
case LoadStage::ANIMATIONS: {
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
if (stage_index_ == 0) { animations_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::DEMO_DATA;
stage_index_ = 0;
break;
}
loadOneAnimation(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::ANIMATION), &Resource::loadOneAnimation, LoadStage::DEMO_DATA);
break;
}
case LoadStage::DEMO_DATA: {
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
if (stage_index_ == 0) { demos_.clear(); }
if (stage_index_ >= list.size()) {
stage_ = LoadStage::CREATE_TEXT;
stage_index_ = 0;
break;
}
loadOneDemoData(stage_index_++);
advanceListLoadStage(Asset::get()->getListByType(Asset::Type::DEMODATA), &Resource::loadOneDemoData, LoadStage::CREATE_TEXT);
break;
}
case LoadStage::CREATE_TEXT:
@@ -519,7 +486,7 @@ auto Resource::loadStep(int budget_ms) -> bool {
break;
}
if ((SDL_GetTicksNS() - start_ns) >= budget_ns) { break; }
if ((SDL_GetTicksNS() - START_NS) >= BUDGET_NS) { break; }
}
return stage_ == LoadStage::DONE;
@@ -542,11 +509,11 @@ void Resource::loadOneSound(size_t idx) {
auto name = getFileName(path);
updateLoadingProgress(name);
auto audio_data = loadAudioData(path);
JA_Sound_t* sound = nullptr;
Ja::Sound* sound = nullptr;
if (!audio_data.data.empty()) {
sound = JA_LoadSound(audio_data.data.data(), audio_data.data.size());
sound = Ja::loadSound(audio_data.data.data(), audio_data.data.size());
} else {
sound = JA_LoadSound(path.c_str());
sound = Ja::loadSound(path.c_str());
}
if (sound == nullptr) {
std::cout << "Sound load failed: " << name << '\n';
@@ -561,11 +528,11 @@ void Resource::loadOneMusic(size_t idx) {
auto name = getFileName(path);
updateLoadingProgress(name);
auto audio_data = loadAudioData(path);
JA_Music_t* music = nullptr;
Ja::Music* music = nullptr;
if (!audio_data.data.empty()) {
music = JA_LoadMusic(audio_data.data.data(), audio_data.data.size());
music = Ja::loadMusic(audio_data.data.data(), audio_data.data.size());
} else {
music = JA_LoadMusic(path.c_str());
music = Ja::loadMusic(path.c_str());
}
if (music == nullptr) {
std::cout << "Music load failed: " << name << '\n';
@@ -627,10 +594,10 @@ void Resource::createPlayerTextures() {
const auto& player = players[player_idx]; // Obtenemos el jugador actual
// Encontrar el archivo original de la textura
const auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
const auto it = std::ranges::find_if(texture_list,
const auto TEXTURE_LIST = Asset::get()->getListByType(Asset::Type::BITMAP);
const auto IT = std::ranges::find_if(TEXTURE_LIST,
[&player](const auto& file) { return getFileName(file) == player.base_texture; });
const std::string texture_file_path = (it != texture_list.end()) ? *it : std::string{};
const std::string TEXTURE_FILE_PATH = (IT != TEXTURE_LIST.end()) ? *IT : std::string{};
// Crear las 4 texturas con sus respectivas paletas
for (int palette_idx = 0; palette_idx < 4; ++palette_idx) {
@@ -646,7 +613,7 @@ void Resource::createPlayerTextures() {
texture->setPaletteColor(0, 56, param.player.outline_color[player_idx].TO_UINT32());
} else {
// Crear textura nueva desde archivo usando ResourceHelper
texture = std::make_shared<Texture>(Screen::get()->getRenderer(), texture_file_path);
texture = std::make_shared<Texture>(Screen::get()->getRenderer(), TEXTURE_FILE_PATH);
// Añadir todas las paletas
texture->addPaletteFromPalFile(Asset::get()->getPath(player.palette_files[0]));
@@ -769,7 +736,7 @@ void Resource::createText() {
void Resource::clearSounds() {
for (auto& sound : sounds_) {
if (sound.sound != nullptr) {
JA_DeleteSound(sound.sound);
Ja::deleteSound(sound.sound);
sound.sound = nullptr;
}
}
@@ -780,7 +747,7 @@ void Resource::clearSounds() {
void Resource::clearMusics() {
for (auto& music : musics_) {
if (music.music != nullptr) {
JA_DeleteMusic(music.music);
Ja::deleteMusic(music.music);
music.music = nullptr;
}
}
@@ -880,10 +847,10 @@ void Resource::renderProgress() {
// Carga los datos para el modo demostración (sin mostrar progreso)
void Resource::loadDemoDataQuiet() {
const auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
const auto LIST = Asset::get()->getListByType(Asset::Type::DEMODATA);
demos_.clear();
demos_.reserve(list.size());
std::ranges::transform(list, std::back_inserter(demos_), [this](const auto& l) { return loadDemoDataFromFile(l); });
demos_.reserve(LIST.size());
std::ranges::transform(LIST, std::back_inserter(demos_), [](const auto& l) { return loadDemoDataFromFile(l); });
}
// Inicializa los rectangulos que definen la barra de progreso