clang-tidy: animated_sprite, asset

This commit is contained in:
2025-07-18 21:27:21 +02:00
parent dabba41179
commit f9877dc82b
5 changed files with 191 additions and 144 deletions

View File

@@ -1,9 +1,9 @@
#include "animated_sprite.h"
#include <SDL3/SDL.h> // Para SDL_LogWarn, SDL_LogCategory, SDL_LogError
#include <stddef.h> // Para size_t
#include <algorithm> // Para min
#include <cstddef> // Para size_t
#include <fstream> // Para basic_istream, basic_ifstream, basic_ios, ifst...
#include <sstream> // Para basic_stringstream
#include <stdexcept> // Para runtime_error
@@ -13,7 +13,7 @@
#include "utils.h" // Para printWithDots
// Carga las animaciones en un vector(Animations) desde un fichero
AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path) {
auto loadAnimationsFromFile(const std::string &file_path) -> AnimationsFileBuffer {
std::ifstream file(file_path);
if (!file) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
@@ -25,8 +25,9 @@ AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path) {
std::vector<std::string> buffer;
std::string line;
while (std::getline(file, line)) {
if (!line.empty())
if (!line.empty()) {
buffer.push_back(line);
}
}
return buffer;
@@ -37,8 +38,8 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::stri
: MovingSprite(texture) {
// Carga las animaciones
if (!file_path.empty()) {
AnimationsFileBuffer v = loadAnimationsFromFile(file_path);
loadFromAnimationsFileBuffer(v);
auto buffer = loadAnimationsFromFile(file_path);
loadFromAnimationsFileBuffer(buffer);
}
}
@@ -51,11 +52,11 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const Animation
}
// Obtiene el índice de la animación a partir del nombre
int AnimatedSprite::getIndex(const std::string &name) {
auto it = animation_indices_.find(name);
if (it != animation_indices_.end()) {
auto AnimatedSprite::getIndex(const std::string &name) -> int {
auto iterator = animation_indices_.find(name);
if (iterator != animation_indices_.end()) {
// Si se encuentra la animación en el mapa, devuelve su índice
return it->second;
return iterator->second;
}
// Si no se encuentra, muestra una advertencia y devuelve -1
@@ -94,7 +95,7 @@ void AnimatedSprite::animate() {
}
// Comprueba si ha terminado la animación
bool AnimatedSprite::animationIsCompleted() {
auto AnimatedSprite::animationIsCompleted() -> bool {
return animations_[current_animation_].completed;
}
@@ -149,89 +150,117 @@ void AnimatedSprite::resetAnimation() {
// Carga la animación desde un vector de cadenas
void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source) {
float frame_width = 1;
float frame_height = 1;
int frames_per_row = 1;
int max_tiles = 1;
AnimationConfig config;
size_t index = 0;
while (index < source.size()) {
std::string line = source.at(index);
// Parsea el fichero para buscar variables y valores
if (line != "[animation]") {
// Encuentra la posición del carácter '='
size_t pos = line.find("=");
// Procesa las dos subcadenas
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
int value = std::stoi(line.substr(pos + 1));
if (key == "frame_width")
frame_width = value;
else if (key == "frame_height")
frame_height = value;
else
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
frames_per_row = texture_->getWidth() / frame_width;
const int w = texture_->getWidth() / frame_width;
const int h = texture_->getHeight() / frame_height;
max_tiles = w * h;
}
}
// Si la línea contiene el texto [animation] se realiza el proceso de carga de una animación
const std::string& line = source.at(index);
if (line == "[animation]") {
Animation animation;
do {
index++;
line = source.at(index);
size_t pos = line.find("=");
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
if (key == "name")
animation.name = value;
else if (key == "speed")
animation.speed = std::stoi(value);
else if (key == "loop")
animation.loop = std::stoi(value);
else if (key == "frames") {
// Se introducen los valores separados por comas en un vector
std::stringstream ss(value);
std::string tmp;
SDL_FRect rect = {0, 0, frame_width, frame_height};
while (getline(ss, tmp, ',')) {
// Comprueba que el tile no sea mayor que el máximo índice permitido
const int num_tile = std::stoi(tmp);
if (num_tile <= max_tiles) {
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
animation.frames.emplace_back(rect);
}
}
} else
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
animations_.emplace_back(animation);
// Rellena el mapa con el nombre y el nuevo índice
animation_indices_[animation.name] = animations_.size() - 1;
index = processAnimationBlock(source, index, config);
} else {
processConfigLine(line, config);
}
// Una vez procesada la línea, aumenta el índice para pasar a la siguiente
index++;
}
// Pone un valor por defecto
setWidth(frame_width);
setHeight(frame_height);
setWidth(config.frame_width);
setHeight(config.frame_height);
}
// Procesa una línea de configuración
void AnimatedSprite::processConfigLine(const std::string& line, AnimationConfig& config) {
size_t pos = line.find("=");
if (pos == std::string::npos) {
return;
}
std::string key = line.substr(0, pos);
int value = std::stoi(line.substr(pos + 1));
if (key == "frame_width") {
config.frame_width = value;
updateFrameCalculations(config);
} else if (key == "frame_height") {
config.frame_height = value;
updateFrameCalculations(config);
} else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
}
}
// Actualiza los cálculos basados en las dimensiones del frame
void AnimatedSprite::updateFrameCalculations(AnimationConfig& config) {
config.frames_per_row = texture_->getWidth() / config.frame_width;
const int WIDTH = texture_->getWidth() / config.frame_width;
const int HEIGHT = texture_->getHeight() / config.frame_height;
config.max_tiles = WIDTH * HEIGHT;
}
// Procesa un bloque completo de animación
auto AnimatedSprite::processAnimationBlock(const AnimationsFileBuffer& source, size_t start_index, const AnimationConfig& config) -> size_t {
Animation animation;
size_t index = start_index + 1; // Salta la línea "[animation]"
while (index < source.size()) {
const std::string& line = source.at(index);
if (line == "[/animation]") {
break;
}
processAnimationParameter(line, animation, config);
index++;
}
// Añade la animación al vector de animaciones
animations_.emplace_back(animation);
// Rellena el mapa con el nombre y el nuevo índice
animation_indices_[animation.name] = animations_.size() - 1;
return index;
}
// Procesa un parámetro individual de animación
void AnimatedSprite::processAnimationParameter(const std::string& line, Animation& animation, const AnimationConfig& config) {
size_t pos = line.find("=");
if (pos == std::string::npos) {
return;
}
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
if (key == "name") {
animation.name = value;
} else if (key == "speed") {
animation.speed = std::stoi(value);
} else if (key == "loop") {
animation.loop = std::stoi(value);
} else if (key == "frames") {
parseFramesParameter(value, animation, config);
} else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
}
}
// Parsea el parámetro de frames (lista separada por comas)
void AnimatedSprite::parseFramesParameter(const std::string& frames_str, Animation& animation, const AnimationConfig& config) {
std::stringstream stream(frames_str);
std::string tmp;
SDL_FRect rect = {0, 0, config.frame_width, config.frame_height};
while (getline(stream, tmp, ',')) {
const int NUM_TILE = std::stoi(tmp);
if (NUM_TILE <= config.max_tiles) {
rect.x = (NUM_TILE % config.frames_per_row) * config.frame_width;
rect.y = (NUM_TILE / config.frames_per_row) * config.frame_height;
animation.frames.emplace_back(rect);
}
}
}
// Establece la velocidad de la animación