Files
esqueleto/source/core/rendering/sprites/animated_sprite.hpp
2026-03-24 21:59:22 +01:00

63 lines
2.0 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <cstdint> // Para uint8_t
#include <memory> // Para shared_ptr
#include <string> // Para string
#include <utility>
#include <vector> // Para vector
#include "core/rendering/sprites/moving_sprite.hpp" // Para MovingSprite
class Surface;
// Recurso de animación: bytes crudos de un fichero YAML (para carga lazy)
struct AnimationResource {
std::string name; // Nombre del fichero
std::vector<uint8_t> yaml_data; // Bytes del archivo YAML sin parsear
};
class AnimatedSprite : public MovingSprite {
public:
using Animations = std::vector<std::string>;
struct AnimationData {
std::string name;
std::vector<SDL_FRect> frames;
float speed{0.083F}; // Segundos por frame
int loop{0}; // Frame al que vuelve al terminar (-1 = sin loop)
bool completed{false};
int current_frame{0};
float accumulated_time{0.0F};
};
// Carga las animaciones desde un fichero YAML en el sistema de ficheros
static auto loadAnimationsFromYAML(const std::string& file_path, std::shared_ptr<Surface>& surface, float& frame_width, float& frame_height) -> std::vector<AnimationData>;
// Constructor con datos pre-cargados (bytes YAML en memoria)
explicit AnimatedSprite(const AnimationResource& cached_data);
~AnimatedSprite() override = default;
void update(float delta_time) override;
auto animationIsCompleted() -> bool;
auto getIndex(const std::string& name) -> int;
auto getCurrentAnimationSize() -> int { return static_cast<int>(animations_[current_animation_].frames.size()); }
void setCurrentAnimation(const std::string& name = "default");
void setCurrentAnimation(int index = 0);
void resetAnimation();
void setCurrentAnimationFrame(int num);
protected:
AnimatedSprite(std::shared_ptr<Surface> surface, SDL_FRect pos);
void animate(float delta_time);
private:
std::vector<AnimationData> animations_;
int current_animation_{0};
};