diff --git a/source/core/rendering/surface_animated_sprite.cpp b/source/core/rendering/surface_animated_sprite.cpp index 1af6b91..dddb168 100644 --- a/source/core/rendering/surface_animated_sprite.cpp +++ b/source/core/rendering/surface_animated_sprite.cpp @@ -13,7 +13,7 @@ #include "utils/utils.hpp" // Para printWithDots // Carga las animaciones en un vector(Animations) desde un fichero -auto loadAnimationsFromFile(const std::string& file_path) -> Animations { +auto SurfaceAnimatedSprite::loadAnimationsFromFile(const std::string& file_path) -> Animations { // Load file using ResourceHelper (supports both filesystem and pack) auto file_data = Resource::Helper::loadFile(file_path); if (file_data.empty()) { @@ -46,7 +46,7 @@ auto loadAnimationsFromFile(const std::string& file_path) -> Animations { SurfaceAnimatedSprite::SurfaceAnimatedSprite(const std::string& file_path) { // Carga las animaciones if (!file_path.empty()) { - Animations v = loadAnimationsFromFile(file_path); + Animations v = SurfaceAnimatedSprite::loadAnimationsFromFile(file_path); setAnimations(v); } } @@ -63,7 +63,7 @@ SurfaceAnimatedSprite::SurfaceAnimatedSprite(std::shared_ptr surface, c : SurfaceMovingSprite(std::move(surface)) { // Carga las animaciones if (!file_path.empty()) { - Animations v = loadAnimationsFromFile(file_path); + Animations v = SurfaceAnimatedSprite::loadAnimationsFromFile(file_path); setAnimations(v); } } @@ -211,7 +211,7 @@ auto parseGlobalParameter(const std::string& line, std::shared_ptr& sur } // Helper: Parsea los frames de una animación desde una cadena separada por comas -void parseAnimationFrames(const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) { +void parseAnimationFrames(const std::string& value, SurfaceAnimatedSprite::AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) { std::stringstream ss(value); std::string tmp; SDL_FRect rect = {0.0F, 0.0F, frame_width, frame_height}; @@ -227,7 +227,7 @@ void parseAnimationFrames(const std::string& value, AnimationData& animation, fl } // Helper: Parsea un parámetro de animación individual -auto parseAnimationParameter(const std::string& key, const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool { +auto parseAnimationParameter(const std::string& key, const std::string& value, SurfaceAnimatedSprite::AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool { if (key == "name") { animation.name = value; return true; @@ -251,8 +251,8 @@ auto parseAnimationParameter(const std::string& key, const std::string& value, A } // Helper: Parsea una animación completa -auto parseAnimation(const Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> AnimationData { - AnimationData animation; +auto parseAnimation(const SurfaceAnimatedSprite::Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> SurfaceAnimatedSprite::AnimationData { + SurfaceAnimatedSprite::AnimationData animation; std::string line; do { @@ -295,7 +295,7 @@ void SurfaceAnimatedSprite::setAnimations(const Animations& animations) { // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { - AnimationData animation = parseAnimation(animations, index, frame_width, frame_height, frames_per_row, max_tiles); + SurfaceAnimatedSprite::AnimationData animation = parseAnimation(animations, index, frame_width, frame_height, frames_per_row, max_tiles); animations_.emplace_back(animation); } diff --git a/source/core/rendering/surface_animated_sprite.hpp b/source/core/rendering/surface_animated_sprite.hpp index fc5da73..17a9617 100644 --- a/source/core/rendering/surface_animated_sprite.hpp +++ b/source/core/rendering/surface_animated_sprite.hpp @@ -8,37 +8,17 @@ #include // Para vector #include "core/rendering/surface_moving_sprite.hpp" // Para SMovingSprite -class Surface; // lines 9-9 -struct AnimationData { - std::string name; // Nombre de la animacion - std::vector frames; // Cada uno de los frames que componen la animación - float speed{0.083F}; // Velocidad de la animación (segundos por frame) - int loop{0}; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva - bool completed{false}; // Indica si ha finalizado la animación - int current_frame{0}; // Frame actual - float accumulated_time{0.0F}; // Tiempo acumulado para las animaciones (time-based) -}; - -using Animations = std::vector; - -// Carga las animaciones en un vector(Animations) desde un fichero -auto loadAnimationsFromFile(const std::string& file_path) -> Animations; +class Surface; class SurfaceAnimatedSprite : public SurfaceMovingSprite { - protected: - // Variables - std::vector animations_; // Vector con las diferentes animaciones - int current_animation_ = 0; // Animacion activa - - // Calcula el frame correspondiente a la animación actual (time-based) - void animate(float delta_time); - - // Carga la animación desde un vector de cadenas - void setAnimations(const Animations& animations); - public: - // Constructor + using Animations = std::vector; // Tipo para lista de animaciones + + // Métodos estáticos + static auto loadAnimationsFromFile(const std::string& file_path) -> Animations; // Carga las animaciones desde fichero + + // Constructores explicit SurfaceAnimatedSprite(const std::string& file_path); explicit SurfaceAnimatedSprite(const Animations& animations); SurfaceAnimatedSprite(std::shared_ptr surface, const std::string& file_path); @@ -46,28 +26,44 @@ class SurfaceAnimatedSprite : public SurfaceMovingSprite { explicit SurfaceAnimatedSprite(std::shared_ptr surface) : SurfaceMovingSprite(std::move(surface)) {} - // Destructor - ~SurfaceAnimatedSprite() override = default; + ~SurfaceAnimatedSprite() override = default; // Destructor - // Actualiza las variables del objeto (time-based) - void update(float delta_time) override; + void update(float delta_time) override; // Actualiza las variables del objeto (time-based) - // Comprueba si ha terminado la animación - auto animationIsCompleted() -> bool; + // Consultas de estado + auto animationIsCompleted() -> bool; // Comprueba si ha terminado la animación + auto getIndex(const std::string& name) -> int; // Obtiene el índice de la animación por nombre + auto getCurrentAnimationSize() -> int { return static_cast(animations_[current_animation_].frames.size()); } // Número de frames de la animación actual - // Obtiene el indice de la animación a partir del nombre - auto getIndex(const std::string& name) -> int; + // Modificadores de animación + void setCurrentAnimation(const std::string& name = "default"); // Establece la animación actual por nombre + void setCurrentAnimation(int index = 0); // Establece la animación actual por índice + void resetAnimation(); // Reinicia la animación + void setCurrentAnimationFrame(int num); // Establece el frame actual de la animación - // Establece la animacion actual - void setCurrentAnimation(const std::string& name = "default"); - void setCurrentAnimation(int index = 0); + protected: + // Métodos protegidos + void animate(float delta_time); // Calcula el frame correspondiente a la animación actual (time-based) + void setAnimations(const Animations& animations); // Carga la animación desde un vector de cadenas - // Reinicia la animación - void resetAnimation(); + private: + // Estructura interna de datos de animación + struct AnimationData { + std::string name{}; // Nombre de la animacion + std::vector frames; // Cada uno de los frames que componen la animación + float speed{0.083F}; // Velocidad de la animación (segundos por frame) + int loop{0}; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva + bool completed{false}; // Indica si ha finalizado la animación + int current_frame{0}; // Frame actual + float accumulated_time{0.0F}; // Tiempo acumulado para las animaciones (time-based) + }; - // Establece el frame actual de la animación - void setCurrentAnimationFrame(int num); + // Funciones amigas (helper functions del .cpp) + friend void parseAnimationFrames(const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles); + friend auto parseAnimationParameter(const std::string& key, const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool; + friend auto parseAnimation(const Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> AnimationData; - // Obtiene el numero de frames de la animación actual - auto getCurrentAnimationSize() -> int { return static_cast(animations_[current_animation_].frames.size()); } + // Variables miembro + std::vector animations_; // Vector con las diferentes animaciones + int current_animation_{0}; // Animación activa }; \ No newline at end of file diff --git a/source/core/resources/resource_cache.cpp b/source/core/resources/resource_cache.cpp index 30a7d5b..74a7a53 100644 --- a/source/core/resources/resource_cache.cpp +++ b/source/core/resources/resource_cache.cpp @@ -149,7 +149,7 @@ auto Cache::getText(const std::string& name) -> std::shared_ptr { } // Obtiene la animación a partir de un nombre -auto Cache::getAnimations(const std::string& name) -> Animations& { +auto Cache::getAnimations(const std::string& name) -> SurfaceAnimatedSprite::Animations& { auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; }); if (it != animations_.end()) { @@ -300,7 +300,7 @@ void Cache::loadAnimations() { for (const auto& l : list) { auto name = getFileName(l); - animations_.emplace_back(name, loadAnimationsFromFile(l)); + animations_.emplace_back(name, SurfaceAnimatedSprite::loadAnimationsFromFile(l)); updateLoadingProgress(); } } diff --git a/source/core/resources/resource_cache.hpp b/source/core/resources/resource_cache.hpp index a0a6983..83ef1da 100644 --- a/source/core/resources/resource_cache.hpp +++ b/source/core/resources/resource_cache.hpp @@ -80,11 +80,11 @@ struct ResourceText { // Estructura para almacenar ficheros animaciones y su nombre struct ResourceAnimation { - std::string name; // Nombre del fichero - Animations animation; // Objeto con las animaciones + std::string name; // Nombre del fichero + SurfaceAnimatedSprite::Animations animation; // Objeto con las animaciones // Constructor - ResourceAnimation(std::string name, Animations animation) + ResourceAnimation(std::string name, SurfaceAnimatedSprite::Animations animation) : name(std::move(name)), animation(std::move(animation)) {} }; @@ -245,7 +245,7 @@ class Cache { auto getText(const std::string& name) -> std::shared_ptr; // Obtiene la animación a partir de un nombre - auto getAnimations(const std::string& name) -> Animations&; + auto getAnimations(const std::string& name) -> SurfaceAnimatedSprite::Animations&; // Obtiene el mapa de tiles a partir de un nombre auto getTileMap(const std::string& name) -> std::vector&;