neteja cppcheck (105 → 0)
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError, SDL_SetRenderDrawColor, SDL_EventType, SDL_PollEvent, SDL_RenderFillRect, SDL_RenderRect, SDLK_ESCAPE, SDL_Event
|
||||
|
||||
#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
|
||||
@@ -138,40 +140,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); };
|
||||
|
||||
// Inicializa lista de sonidos
|
||||
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||
const auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||
sounds_.clear();
|
||||
for (const auto& file : sound_list) {
|
||||
sounds_.emplace_back(getFileName(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
|
||||
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
||||
const auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
||||
musics_.clear();
|
||||
for (const auto& file : music_list) {
|
||||
musics_.emplace_back(getFileName(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
|
||||
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||
const auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||
textures_.clear();
|
||||
for (const auto& file : texture_list) {
|
||||
textures_.emplace_back(getFileName(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
|
||||
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||
const auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||
text_files_.clear();
|
||||
for (const auto& file : text_file_list) {
|
||||
text_files_.emplace_back(getFileName(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
|
||||
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
||||
const auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
||||
animations_.clear();
|
||||
for (const auto& file : animation_list) {
|
||||
animations_.emplace_back(getFileName(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();
|
||||
@@ -192,9 +191,8 @@ void Resource::initResourceLists() {
|
||||
"smb2_grad"};
|
||||
|
||||
texts_.clear();
|
||||
for (const auto& text_name : TEXT_OBJECTS) {
|
||||
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
|
||||
}
|
||||
texts_.reserve(TEXT_OBJECTS.size());
|
||||
std::ranges::transform(TEXT_OBJECTS, std::back_inserter(texts_), [](const auto& text_name) { return ResourceText(text_name); });
|
||||
}
|
||||
|
||||
// Obtiene el sonido a partir de un nombre (con carga perezosa)
|
||||
@@ -336,23 +334,17 @@ auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
|
||||
}
|
||||
|
||||
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
|
||||
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||
for (const auto& file : texture_list) {
|
||||
if (getFileName(file) == name) {
|
||||
return std::make_shared<Texture>(Screen::get()->getRenderer(), file);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
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;
|
||||
}
|
||||
|
||||
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
|
||||
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||
for (const auto& file : text_file_list) {
|
||||
if (getFileName(file) == name) {
|
||||
return Text::loadFile(file);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
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;
|
||||
}
|
||||
|
||||
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
|
||||
@@ -377,27 +369,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"}};
|
||||
|
||||
for (const auto& mapping : TEXT_MAPPINGS) {
|
||||
if (mapping.key == name) {
|
||||
// Cargar las dependencias automáticamente
|
||||
auto texture = getTexture(mapping.texture_file); // Esto cargará la textura si no está cargada
|
||||
auto text_file = getTextFile(mapping.text_file); // Esto cargará el archivo de texto si no está cargado
|
||||
|
||||
if (texture && text_file) {
|
||||
return std::make_shared<Text>(texture, text_file);
|
||||
}
|
||||
}
|
||||
const auto it = std::ranges::find_if(TEXT_MAPPINGS,
|
||||
[&name](const auto& mapping) { return mapping.key == name; });
|
||||
if (it == TEXT_MAPPINGS.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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
|
||||
return (texture && text_file) ? std::make_shared<Text>(texture, text_file) : nullptr;
|
||||
}
|
||||
|
||||
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
|
||||
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
||||
for (const auto& file : animation_list) {
|
||||
if (getFileName(file) == name) {
|
||||
return loadAnimationsFromFile(file);
|
||||
}
|
||||
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);
|
||||
}
|
||||
// Si no se encuentra, retorna vector vacío
|
||||
return AnimationsFileBuffer{};
|
||||
@@ -640,14 +627,10 @@ void Resource::createPlayerTextures() {
|
||||
const auto& player = players[player_idx]; // Obtenemos el jugador actual
|
||||
|
||||
// Encontrar el archivo original de la textura
|
||||
std::string texture_file_path;
|
||||
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||
for (const auto& file : texture_list) {
|
||||
if (getFileName(file) == player.base_texture) {
|
||||
texture_file_path = file;
|
||||
break;
|
||||
}
|
||||
}
|
||||
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{};
|
||||
|
||||
// Crear las 4 texturas con sus respectivas paletas
|
||||
for (int palette_idx = 0; palette_idx < 4; ++palette_idx) {
|
||||
@@ -720,9 +703,9 @@ void Resource::createTextTextures() {
|
||||
{"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};
|
||||
|
||||
auto text1 = getText("04b_25_enhanced");
|
||||
for (const auto& s : strings1) {
|
||||
textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||
}
|
||||
std::ranges::transform(strings1, std::back_inserter(textures_), [&](const auto& s) {
|
||||
return ResourceTexture(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||
});
|
||||
|
||||
// Texturas de tamaño doble
|
||||
std::vector<NameAndText> strings2 = {
|
||||
@@ -734,9 +717,9 @@ void Resource::createTextTextures() {
|
||||
{"game_text_game_over", "Game Over"}};
|
||||
|
||||
auto text2 = getText("04b_25_2x_enhanced");
|
||||
for (const auto& s : strings2) {
|
||||
textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||
}
|
||||
std::ranges::transform(strings2, std::back_inserter(textures_), [&](const auto& s) {
|
||||
return ResourceTexture(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||
});
|
||||
}
|
||||
|
||||
// Crea los objetos de texto a partir de los archivos de textura y texto
|
||||
@@ -897,12 +880,10 @@ void Resource::renderProgress() {
|
||||
|
||||
// Carga los datos para el modo demostración (sin mostrar progreso)
|
||||
void Resource::loadDemoDataQuiet() {
|
||||
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
||||
const auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
||||
demos_.clear();
|
||||
|
||||
for (const auto& l : list) {
|
||||
demos_.emplace_back(loadDemoDataFromFile(l));
|
||||
}
|
||||
demos_.reserve(list.size());
|
||||
std::ranges::transform(list, std::back_inserter(demos_), [this](const auto& l) { return loadDemoDataFromFile(l); });
|
||||
}
|
||||
|
||||
// Inicializa los rectangulos que definen la barra de progreso
|
||||
|
||||
@@ -60,7 +60,7 @@ class Resource {
|
||||
std::string name; // Nombre del sonido
|
||||
JA_Sound_t* sound; // Objeto con el sonido
|
||||
|
||||
ResourceSound(std::string name, JA_Sound_t* sound = nullptr)
|
||||
explicit ResourceSound(std::string name, JA_Sound_t* sound = nullptr)
|
||||
: name(std::move(name)),
|
||||
sound(sound) {}
|
||||
};
|
||||
@@ -69,7 +69,7 @@ class Resource {
|
||||
std::string name; // Nombre de la música
|
||||
JA_Music_t* music; // Objeto con la música
|
||||
|
||||
ResourceMusic(std::string name, JA_Music_t* music = nullptr)
|
||||
explicit ResourceMusic(std::string name, JA_Music_t* music = nullptr)
|
||||
: name(std::move(name)),
|
||||
music(music) {}
|
||||
};
|
||||
@@ -78,7 +78,7 @@ class Resource {
|
||||
std::string name; // Nombre de la textura
|
||||
std::shared_ptr<Texture> texture; // Objeto con la textura
|
||||
|
||||
ResourceTexture(std::string name, std::shared_ptr<Texture> texture = nullptr)
|
||||
explicit ResourceTexture(std::string name, std::shared_ptr<Texture> texture = nullptr)
|
||||
: name(std::move(name)),
|
||||
texture(std::move(texture)) {}
|
||||
};
|
||||
@@ -87,7 +87,7 @@ class Resource {
|
||||
std::string name; // Nombre del fichero
|
||||
std::shared_ptr<Text::File> text_file; // Objeto con los descriptores de la fuente de texto
|
||||
|
||||
ResourceTextFile(std::string name, std::shared_ptr<Text::File> text_file = nullptr)
|
||||
explicit ResourceTextFile(std::string name, std::shared_ptr<Text::File> text_file = nullptr)
|
||||
: name(std::move(name)),
|
||||
text_file(std::move(text_file)) {}
|
||||
};
|
||||
@@ -96,7 +96,7 @@ class Resource {
|
||||
std::string name; // Nombre del objeto
|
||||
std::shared_ptr<Text> text; // Objeto de texto
|
||||
|
||||
ResourceText(std::string name, std::shared_ptr<Text> text = nullptr)
|
||||
explicit ResourceText(std::string name, std::shared_ptr<Text> text = nullptr)
|
||||
: name(std::move(name)),
|
||||
text(std::move(text)) {}
|
||||
};
|
||||
@@ -105,7 +105,7 @@ class Resource {
|
||||
std::string name; // Nombre de la animación
|
||||
AnimationsFileBuffer animation; // Objeto con las animaciones
|
||||
|
||||
ResourceAnimation(std::string name, AnimationsFileBuffer animation = {})
|
||||
explicit ResourceAnimation(std::string name, AnimationsFileBuffer animation = {})
|
||||
: name(std::move(name)),
|
||||
animation(std::move(animation)) {}
|
||||
};
|
||||
@@ -116,7 +116,7 @@ class Resource {
|
||||
size_t loaded{0}; // Número de recursos cargados
|
||||
|
||||
ResourceCount() = default;
|
||||
ResourceCount(size_t total)
|
||||
explicit ResourceCount(size_t total)
|
||||
: total(total) {}
|
||||
|
||||
void add(size_t amount) { loaded += amount; }
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint> // Para uint8_t
|
||||
#include <filesystem> // Para remove, path
|
||||
#include <fstream> // Para basic_ofstream, basic_ios, basic_ostream::write, ios, ofstream
|
||||
#include <string> // Para string, basic_string, hash, operator+, to_string, __str_hash_base
|
||||
#include <vector> // Para vector
|
||||
#include <cstdint> // Para uint8_t
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
// Helper functions para integrar ResourceLoader con el sistema existente
|
||||
namespace ResourceHelper {
|
||||
@@ -22,24 +20,4 @@ namespace ResourceHelper {
|
||||
|
||||
// Convierte ruta Asset a ruta relativa para ResourceLoader
|
||||
auto getPackPath(const std::string& asset_path) -> std::string;
|
||||
|
||||
// Wrappea la carga de archivos para mantener compatibilidad
|
||||
template <typename T>
|
||||
auto loadResourceFile(const std::string& asset_path, T* (*loader_func)(const char*)) -> T* {
|
||||
auto data = loadFile(asset_path);
|
||||
if (data.empty()) {
|
||||
return loader_func(asset_path.c_str());
|
||||
}
|
||||
|
||||
// Crear archivo temporal para funciones que esperan path
|
||||
std::string temp_path = "/tmp/ccae_" + std::to_string(std::hash<std::string>{}(asset_path));
|
||||
std::ofstream temp_file(temp_path, std::ios::binary);
|
||||
temp_file.write(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
temp_file.close();
|
||||
|
||||
T* result = loader_func(temp_path.c_str());
|
||||
std::filesystem::remove(temp_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
} // namespace ResourceHelper
|
||||
@@ -1,10 +1,11 @@
|
||||
#include "core/resources/resource_pack.hpp"
|
||||
|
||||
#include <algorithm> // Para replace
|
||||
#include <algorithm> // Para replace, ranges::all_of
|
||||
#include <array> // Para array
|
||||
#include <filesystem> // Para path, recursive_directory_iterator, directory_entry, exists, relative
|
||||
#include <fstream> // Para basic_ifstream, basic_ostream, basic_ofstream, operator<<, basic_ios, basic_istream::read, basic_ostream::write, endl, ios, basic_istream, ifstream, operator|, basic_istream::seekg, basic_istream::tellg, ofstream, streamsize
|
||||
#include <iostream> // Para cerr
|
||||
#include <numeric> // Para accumulate
|
||||
#include <utility> // Para pair
|
||||
|
||||
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "CCAE_RESOURCES__2024";
|
||||
@@ -17,11 +18,7 @@ ResourcePack::~ResourcePack() {
|
||||
}
|
||||
|
||||
auto ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
|
||||
uint32_t checksum = 0x12345678;
|
||||
for (unsigned char i : data) {
|
||||
checksum = ((checksum << 5) + checksum) + i;
|
||||
}
|
||||
return checksum;
|
||||
return std::accumulate(data.begin(), data.end(), static_cast<uint32_t>(0x12345678), [](uint32_t checksum, unsigned char i) { return ((checksum << 5) + checksum) + i; });
|
||||
}
|
||||
|
||||
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
@@ -162,20 +159,16 @@ auto ResourcePack::addDirectory(const std::string& directory) -> bool {
|
||||
return false; // NOLINT(readability-simplify-boolean-expr)
|
||||
}
|
||||
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
||||
if (entry.is_regular_file()) {
|
||||
return std::ranges::all_of(std::filesystem::recursive_directory_iterator(directory),
|
||||
[this, &directory](const auto& entry) {
|
||||
if (!entry.is_regular_file()) {
|
||||
return true;
|
||||
}
|
||||
std::string filepath = entry.path().string();
|
||||
std::string filename = std::filesystem::relative(entry.path(), directory).string();
|
||||
|
||||
std::ranges::replace(filename, '\\', '/');
|
||||
|
||||
if (!addFile(filename, filepath)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return addFile(filename, filepath);
|
||||
});
|
||||
}
|
||||
|
||||
auto ResourcePack::getResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||
|
||||
Reference in New Issue
Block a user