neteja cppcheck (44 → 0) i aïllar impls de tercers
This commit is contained in:
@@ -5,20 +5,9 @@
|
||||
#include <algorithm> // Para clamp
|
||||
#include <iostream> // Para std::cout
|
||||
|
||||
// Implementación de stb_vorbis (debe estar ANTES de incluir jail_audio.hpp).
|
||||
// clang-format off
|
||||
#undef STB_VORBIS_HEADER_ONLY
|
||||
#include "external/stb_vorbis.c"
|
||||
// stb_vorbis.c filtra les macros L, C i R (i PLAYBACK_*) al TU. Les netegem
|
||||
// perquè xocarien amb noms de paràmetres de plantilla en altres headers.
|
||||
#undef L
|
||||
#undef C
|
||||
#undef R
|
||||
#undef PLAYBACK_MONO
|
||||
#undef PLAYBACK_LEFT
|
||||
#undef PLAYBACK_RIGHT
|
||||
// clang-format on
|
||||
|
||||
// La implementació de stb_vorbis viu en source/external/stb_vorbis_impl.cpp.
|
||||
// Ací només en veiem les declaracions via jail_audio.hpp (que inclou amb
|
||||
// STB_VORBIS_HEADER_ONLY).
|
||||
#include "core/audio/audio_adapter.hpp" // Para AudioResource::getMusic/getSound
|
||||
#include "core/audio/jail_audio.hpp" // Para JA_*
|
||||
#include "game/options.hpp" // Para Options::audio
|
||||
|
||||
@@ -265,13 +265,13 @@ inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) {
|
||||
auto* music = new JA_Music_t();
|
||||
music->ogg_data.assign(buffer, buffer + length);
|
||||
|
||||
int error = 0;
|
||||
int vorbis_error = 0;
|
||||
music->vorbis = stb_vorbis_open_memory(music->ogg_data.data(),
|
||||
static_cast<int>(length),
|
||||
&error,
|
||||
&vorbis_error,
|
||||
nullptr);
|
||||
if (!music->vorbis) {
|
||||
std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << error << ")" << '\n';
|
||||
std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << vorbis_error << ")" << '\n';
|
||||
delete music;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
+26
-60
@@ -173,15 +173,11 @@ auto Input::checkAction(Action action, bool repeat, bool check_keyboard, const s
|
||||
auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& gamepad) -> bool {
|
||||
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
|
||||
|
||||
auto pressed = [](const auto& pair) { return pair.second.just_pressed; };
|
||||
|
||||
// --- Comprobación del Teclado ---
|
||||
if (check_keyboard) {
|
||||
for (const auto& pair : keyboard_.bindings) {
|
||||
// Simplemente leemos el estado pre-calculado por Input::update().
|
||||
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'.
|
||||
if (pair.second.just_pressed) {
|
||||
return true; // Se encontró una acción recién pulsada.
|
||||
}
|
||||
}
|
||||
if (check_keyboard && std::ranges::any_of(keyboard_.bindings, pressed)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Si gamepad es nullptr pero hay mandos conectados, usar el primero
|
||||
@@ -191,15 +187,8 @@ auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& g
|
||||
}
|
||||
|
||||
// --- Comprobación del Mando ---
|
||||
// Comprobamos si hay mandos y si el índice solicitado es válido.
|
||||
if (active_gamepad != nullptr) {
|
||||
// Iteramos sobre todas las acciones, no sobre el número de mandos.
|
||||
for (const auto& pair : active_gamepad->bindings) {
|
||||
// Leemos el estado pre-calculado para el mando y la acción específicos.
|
||||
if (pair.second.just_pressed) {
|
||||
return true; // Se encontró una acción recién pulsada en el mando.
|
||||
}
|
||||
}
|
||||
if (active_gamepad != nullptr && std::ranges::any_of(active_gamepad->bindings, pressed)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Si llegamos hasta aquí, no se detectó ninguna nueva pulsación.
|
||||
@@ -208,22 +197,13 @@ auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& g
|
||||
|
||||
// Comprueba si hay algún botón pulsado
|
||||
auto Input::checkAnyButton(bool repeat) -> bool {
|
||||
// Solo comprueba los botones definidos previamente
|
||||
for (auto bi : BUTTON_INPUTS) {
|
||||
// Comprueba el teclado
|
||||
if (checkAction(bi, repeat, CHECK_KEYBOARD)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Comprueba los mandos
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
if (checkAction(bi, repeat, DO_NOT_CHECK_KEYBOARD, gamepad)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
auto button_pressed = [&](auto bi) {
|
||||
if (checkAction(bi, repeat, CHECK_KEYBOARD)) { return true; }
|
||||
return std::ranges::any_of(gamepads_, [&](const auto& gp) {
|
||||
return checkAction(bi, repeat, DO_NOT_CHECK_KEYBOARD, gp);
|
||||
});
|
||||
};
|
||||
return std::ranges::any_of(BUTTON_INPUTS, button_pressed);
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
@@ -237,9 +217,8 @@ auto Input::getControllerName(const std::shared_ptr<Gamepad>& gamepad) -> std::s
|
||||
// Obtiene la lista de nombres de mandos
|
||||
auto Input::getControllerNames() const -> std::vector<std::string> {
|
||||
std::vector<std::string> names;
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
names.push_back(gamepad->name);
|
||||
}
|
||||
names.reserve(gamepads_.size());
|
||||
std::ranges::transform(gamepads_, std::back_inserter(names), [](const auto& gp) { return gp->name; });
|
||||
return names;
|
||||
}
|
||||
|
||||
@@ -248,21 +227,13 @@ auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
|
||||
|
||||
// Obtiene el gamepad a partir de un event.id
|
||||
auto Input::getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Input::Gamepad> {
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
if (gamepad->instance_id == id) {
|
||||
return gamepad;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
auto it = std::ranges::find_if(gamepads_, [id](const auto& gp) { return gp->instance_id == id; });
|
||||
return (it != gamepads_.end()) ? *it : nullptr;
|
||||
}
|
||||
|
||||
auto Input::getGamepadByName(const std::string& name) const -> std::shared_ptr<Input::Gamepad> {
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
if (gamepad && gamepad->name == name) {
|
||||
return gamepad;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
auto it = std::ranges::find_if(gamepads_, [&](const auto& gp) { return gp && gp->name == name; });
|
||||
return (it != gamepads_.end()) ? *it : nullptr;
|
||||
}
|
||||
|
||||
// Obtiene el SDL_GamepadButton asignado a un action
|
||||
@@ -495,19 +466,14 @@ auto Input::findAvailableGamepadByName(const std::string& gamepad_name) -> std::
|
||||
}
|
||||
|
||||
// Buscar por nombre
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
if (gamepad && gamepad->name == gamepad_name) {
|
||||
return gamepad;
|
||||
}
|
||||
auto by_name = std::ranges::find_if(gamepads_, [&](const auto& gp) {
|
||||
return gp && gp->name == gamepad_name;
|
||||
});
|
||||
if (by_name != gamepads_.end()) {
|
||||
return *by_name;
|
||||
}
|
||||
|
||||
// Si no se encuentra por nombre, devolver el primer gamepad válido
|
||||
for (const auto& gamepad : gamepads_) {
|
||||
if (gamepad) {
|
||||
return gamepad;
|
||||
}
|
||||
}
|
||||
|
||||
// Si llegamos aquí, no hay gamepads válidos
|
||||
return nullptr;
|
||||
auto any_valid = std::ranges::find_if(gamepads_, [](const auto& gp) { return static_cast<bool>(gp); });
|
||||
return (any_valid != gamepads_.end()) ? *any_valid : nullptr;
|
||||
}
|
||||
@@ -178,9 +178,7 @@ namespace {
|
||||
}
|
||||
|
||||
// Si quedan colores sin asignar, añadirlos al final
|
||||
for (const auto& c : available) {
|
||||
result.push_back(c);
|
||||
}
|
||||
std::ranges::copy(available, std::back_inserter(result));
|
||||
|
||||
Palette out{};
|
||||
out.fill(0);
|
||||
@@ -208,11 +206,11 @@ PaletteManager::PaletteManager(
|
||||
// Leer la paleta de referencia directamente desde el archivo
|
||||
// (Resource::Cache aún no está disponible en este punto del ciclo de vida)
|
||||
const std::string REF_NAME = std::string(Defaults::Video::PALETTE_NAME) + ".pal";
|
||||
for (const auto& p : palettes_) {
|
||||
if (getFileName(p) == REF_NAME) {
|
||||
reference_palette_ = readPalFile(p);
|
||||
break;
|
||||
}
|
||||
auto ref_it = std::ranges::find_if(palettes_, [&](const auto& p) {
|
||||
return getFileName(p) == REF_NAME;
|
||||
});
|
||||
if (ref_it != palettes_.end()) {
|
||||
reference_palette_ = readPalFile(*ref_it);
|
||||
}
|
||||
|
||||
// Leer y aplicar paleta inicial
|
||||
|
||||
@@ -8,9 +8,8 @@
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "external/stb_image_write.h" // Para stbi_write_png
|
||||
#include "external/stb_image_write.h" // Para stbi_write_png (impl en source/external/stb_image_write_impl.cpp)
|
||||
|
||||
namespace Screenshot {
|
||||
|
||||
@@ -21,7 +20,7 @@ namespace Screenshot {
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t time = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm* tm = std::localtime(&time);
|
||||
const std::tm* tm = std::localtime(&time);
|
||||
|
||||
char timestamp[20];
|
||||
std::strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", tm);
|
||||
@@ -45,15 +44,15 @@ namespace Screenshot {
|
||||
} // namespace
|
||||
|
||||
auto save(const Surface& surface) -> std::string {
|
||||
int width = static_cast<int>(surface.getWidth());
|
||||
int height = static_cast<int>(surface.getHeight());
|
||||
std::vector<Uint32> buffer(static_cast<size_t>(width * height));
|
||||
const int WIDTH = surface.getWidth();
|
||||
const int HEIGHT = surface.getHeight();
|
||||
std::vector<Uint32> buffer(static_cast<size_t>(WIDTH * HEIGHT));
|
||||
surface.toARGBBuffer(buffer.data());
|
||||
|
||||
std::string filename;
|
||||
std::string filepath = generateFilePath(filename);
|
||||
|
||||
return writePng(buffer.data(), width, height, filepath) ? filename : "";
|
||||
return writePng(buffer.data(), WIDTH, HEIGHT, filepath) ? filename : "";
|
||||
}
|
||||
|
||||
auto save(const Uint32* buffer, int width, int height) -> std::string {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <cstddef> // Para size_t
|
||||
#include <fstream> // Para basic_ostream, basic_istream, operator<<, basic...
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <numeric> // Para std::accumulate
|
||||
#include <sstream> // Para basic_stringstream
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <utility>
|
||||
@@ -155,11 +156,10 @@ void AnimatedSprite::animate(float delta_time) {
|
||||
anim.accumulated_time += delta_time;
|
||||
|
||||
// Calcular duración total de la animación
|
||||
float total = 0.0F;
|
||||
for (auto s : anim.speeds) { total += s; }
|
||||
const float TOTAL = std::accumulate(anim.speeds.begin(), anim.speeds.end(), 0.0F);
|
||||
|
||||
// Si hemos superado la duración total, manejar loop o congelar
|
||||
if (anim.accumulated_time >= total) {
|
||||
if (anim.accumulated_time >= TOTAL) {
|
||||
if (anim.loop_from < 0) {
|
||||
// Sin loop: congelar en el último frame
|
||||
anim.current_frame = static_cast<int>(anim.frames.size()) - 1;
|
||||
@@ -168,10 +168,9 @@ void AnimatedSprite::animate(float delta_time) {
|
||||
return;
|
||||
}
|
||||
// Con loop: envolver el tiempo en el rango del loop
|
||||
float loop_start = 0.0F;
|
||||
for (int i = 0; i < anim.loop_from; ++i) { loop_start += anim.speeds[i]; }
|
||||
float loop_len = total - loop_start;
|
||||
anim.accumulated_time = loop_start + std::fmod(anim.accumulated_time - loop_start, loop_len);
|
||||
const float LOOP_START = std::accumulate(anim.speeds.begin(), anim.speeds.begin() + anim.loop_from, 0.0F);
|
||||
const float LOOP_LEN = TOTAL - LOOP_START;
|
||||
anim.accumulated_time = LOOP_START + std::fmod(anim.accumulated_time - LOOP_START, LOOP_LEN);
|
||||
}
|
||||
|
||||
// Buscar el frame correspondiente al tiempo acumulado
|
||||
|
||||
@@ -427,7 +427,7 @@ static auto computeFadeDensity(int screen_y, int fade_h, int canvas_height) -> f
|
||||
}
|
||||
|
||||
// Render amb dissolució als cantons superior/inferior (hash 2D, sense parpelleig)
|
||||
void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, SDL_FRect* src_rect) const {
|
||||
void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, const SDL_FRect* src_rect) const {
|
||||
// Aplicar render offset (usado per transicions entre pantalles)
|
||||
x += Screen::get()->getRenderOffsetX();
|
||||
y += Screen::get()->getRenderOffsetY();
|
||||
@@ -468,7 +468,7 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height
|
||||
}
|
||||
|
||||
// Idem però reemplaçant un color índex
|
||||
void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, Uint8 source_color, Uint8 target_color, SDL_FRect* src_rect) const {
|
||||
void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, Uint8 source_color, Uint8 target_color, const SDL_FRect* src_rect) const {
|
||||
// Aplicar render offset (usado per transicions entre pantalles)
|
||||
x += Screen::get()->getRenderOffsetX();
|
||||
y += Screen::get()->getRenderOffsetY();
|
||||
|
||||
@@ -87,10 +87,10 @@ class Surface {
|
||||
void renderWithColorReplace(int x, int y, Uint8 source_color = 0, Uint8 target_color = 0, SDL_FRect* src_rect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE) const;
|
||||
|
||||
// Render amb dissolució als cantons superior/inferior (hash 2D, sense parpelleig)
|
||||
void renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, SDL_FRect* src_rect = nullptr) const;
|
||||
void renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, const SDL_FRect* src_rect = nullptr) const;
|
||||
|
||||
// Idem però reemplaçant un color índex (per a sprites sobre fons del mateix color)
|
||||
void renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, Uint8 source_color, Uint8 target_color, SDL_FRect* src_rect = nullptr) const;
|
||||
void renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, Uint8 source_color, Uint8 target_color, const SDL_FRect* src_rect = nullptr) const;
|
||||
|
||||
// Establece un color en la paleta
|
||||
void setColor(int index, Uint32 color);
|
||||
|
||||
@@ -577,27 +577,13 @@ namespace Resource {
|
||||
animations_.clear();
|
||||
rooms_.clear();
|
||||
|
||||
for (const auto& l : List::get()->getListByType(List::Type::SOUND)) {
|
||||
sounds_.emplace_back(SoundResource{.name = getFileName(l), .sound = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::MUSIC)) {
|
||||
musics_.emplace_back(MusicResource{.name = getFileName(l), .music = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::BITMAP)) {
|
||||
surfaces_.emplace_back(SurfaceResource{.name = getFileName(l), .surface = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::PALETTE)) {
|
||||
palettes_.emplace_back(ResourcePalette{.name = getFileName(l)});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::FONT)) {
|
||||
text_files_.emplace_back(TextFileResource{.name = getFileName(l), .text_file = nullptr});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::ANIMATION)) {
|
||||
animations_.emplace_back(AnimationResource{.name = getFileName(l), .yaml_data = {}});
|
||||
}
|
||||
for (const auto& l : List::get()->getListByType(List::Type::ROOM)) {
|
||||
rooms_.emplace_back(RoomResource{.name = getFileName(l), .room = nullptr});
|
||||
}
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::SOUND), std::back_inserter(sounds_), [](const auto& l) { return SoundResource{.name = getFileName(l), .sound = nullptr}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::MUSIC), std::back_inserter(musics_), [](const auto& l) { return MusicResource{.name = getFileName(l), .music = nullptr}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::BITMAP), std::back_inserter(surfaces_), [](const auto& l) { return SurfaceResource{.name = getFileName(l), .surface = nullptr}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::PALETTE), std::back_inserter(palettes_), [](const auto& l) { return ResourcePalette{.name = getFileName(l)}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::FONT), std::back_inserter(text_files_), [](const auto& l) { return TextFileResource{.name = getFileName(l), .text_file = nullptr}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::ANIMATION), std::back_inserter(animations_), [](const auto& l) { return AnimationResource{.name = getFileName(l), .yaml_data = {}}; });
|
||||
std::ranges::transform(List::get()->getListByType(List::Type::ROOM), std::back_inserter(rooms_), [](const auto& l) { return RoomResource{.name = getFileName(l), .room = nullptr}; });
|
||||
}
|
||||
|
||||
void Cache::loadSoundByName(const std::string& name) {
|
||||
|
||||
@@ -9,16 +9,13 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
namespace Resource {
|
||||
|
||||
// Calculate CRC32 checksum for data verification
|
||||
auto Pack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
|
||||
uint32_t checksum = 0x12345678;
|
||||
for (unsigned char byte : data) {
|
||||
checksum = ((checksum << 5) + checksum) + byte;
|
||||
}
|
||||
return checksum;
|
||||
return std::accumulate(data.begin(), data.end(), static_cast<uint32_t>(0x12345678), [](uint32_t acc, unsigned char byte) { return ((acc << 5) + acc) + byte; });
|
||||
}
|
||||
|
||||
// XOR encryption (symmetric - same function for encrypt/decrypt)
|
||||
|
||||
Reference in New Issue
Block a user