migrant .ani a .yaml

This commit is contained in:
2025-11-17 13:31:20 +01:00
parent 6a6cc22b21
commit 0c9becae4c
3 changed files with 97 additions and 27 deletions

View File

@@ -124,17 +124,95 @@ auto SurfaceAnimatedSprite::loadAnimationsFromYAML(const std::string& file_path,
return animations;
}
// Constructor con datos pre-cargados del cache
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_data)
: SurfaceMovingSprite(cached_data.surface) {
// Copiar datos pre-cargados del cache
animations_ = cached_data.animations;
setWidth(cached_data.frame_width);
setHeight(cached_data.frame_height);
// Constructor con bytes YAML del cache (parsing lazy)
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_data) {
// Parsear YAML desde los bytes cargados en cache
std::string yaml_content(cached_data.yaml_data.begin(), cached_data.yaml_data.end());
// Inicializar con la primera animación si existe
if (!animations_.empty() && !animations_[0].frames.empty()) {
setClip(animations_[0].frames[0]);
try {
auto yaml = fkyaml::node::deserialize(yaml_content);
// Variables para almacenar configuración global
float frame_width = 0.0F;
float frame_height = 0.0F;
// --- Parse global configuration ---
if (yaml.contains("tileSetFile")) {
std::string tile_set_file = yaml["tileSetFile"].get_value<std::string>();
// Ahora SÍ podemos acceder al cache (ya está completamente cargado)
surface_ = Resource::Cache::get()->getSurface(tile_set_file);
}
if (yaml.contains("frameWidth")) {
frame_width = static_cast<float>(yaml["frameWidth"].get_value<int>());
}
if (yaml.contains("frameHeight")) {
frame_height = static_cast<float>(yaml["frameHeight"].get_value<int>());
}
// Calculate sprite sheet parameters
int frames_per_row = 1;
int max_tiles = 1;
if (surface_) {
frames_per_row = surface_->getWidth() / static_cast<int>(frame_width);
const int W = surface_->getWidth() / static_cast<int>(frame_width);
const int H = surface_->getHeight() / static_cast<int>(frame_height);
max_tiles = W * H;
}
// --- Parse animations array ---
if (yaml.contains("animations") && yaml["animations"].is_sequence()) {
const auto& animations_node = yaml["animations"];
for (const auto& anim_node : animations_node) {
AnimationData animation;
// Parse animation name
if (anim_node.contains("name")) {
animation.name = anim_node["name"].get_value<std::string>();
}
// Parse speed (seconds per frame)
if (anim_node.contains("speed")) {
animation.speed = anim_node["speed"].get_value<float>();
}
// Parse loop frame index
if (anim_node.contains("loop")) {
animation.loop = anim_node["loop"].get_value<int>();
}
// Parse frames array
if (anim_node.contains("frames") && anim_node["frames"].is_sequence()) {
animation.frames = convertYAMLFramesToRects(
anim_node["frames"],
frame_width,
frame_height,
frames_per_row,
max_tiles
);
}
animations_.push_back(animation);
}
}
// Set dimensions
setWidth(frame_width);
setHeight(frame_height);
// Inicializar con la primera animación si existe
if (!animations_.empty() && !animations_[0].frames.empty()) {
setClip(animations_[0].frames[0]);
}
} catch (const fkyaml::exception& e) {
std::cerr << "YAML parsing error in animation " << cached_data.name << ": " << e.what() << '\n';
throw;
} catch (const std::exception& e) {
std::cerr << "Error loading animation " << cached_data.name << ": " << e.what() << '\n';
throw;
}
}

View File

@@ -301,13 +301,11 @@ void Cache::loadAnimations() {
for (const auto& l : list) {
auto name = getFileName(l);
// Parsear YAML y almacenar datos pre-cargados
std::shared_ptr<Surface> surface;
float frame_width = 0.0F;
float frame_height = 0.0F;
auto animation_data = SurfaceAnimatedSprite::loadAnimationsFromYAML(l, surface, frame_width, frame_height);
// Cargar bytes del archivo YAML sin parsear (carga lazy)
auto yaml_bytes = Helper::loadFile(l);
animations_.emplace_back(name, animation_data, surface, frame_width, frame_height);
animations_.emplace_back(name, yaml_bytes);
printWithDots("Animation : ", name, "[ LOADED ]");
updateLoadingProgress();
}
}

View File

@@ -80,19 +80,13 @@ struct ResourceText {
// Estructura para almacenar ficheros animaciones y su nombre
struct ResourceAnimation {
std::string name; // Nombre del fichero
std::vector<SurfaceAnimatedSprite::AnimationData> animations; // Datos de animaciones parseadas desde YAML
std::shared_ptr<Surface> surface; // Surface asociada al sprite sheet
float frame_width{0.0F}; // Ancho de cada frame
float frame_height{0.0F}; // Alto de cada frame
std::string name; // Nombre del fichero
std::vector<uint8_t> yaml_data; // Bytes del archivo YAML sin parsear
// Constructor
ResourceAnimation(std::string name, std::vector<SurfaceAnimatedSprite::AnimationData> animations, std::shared_ptr<Surface> surface, float frame_width, float frame_height)
ResourceAnimation(std::string name, std::vector<uint8_t> yaml_data)
: name(std::move(name)),
animations(std::move(animations)),
surface(std::move(surface)),
frame_width(frame_width),
frame_height(frame_height) {}
yaml_data(std::move(yaml_data)) {}
};
// Estructura para almacenar ficheros con el mapa de tiles de una habitación y su nombre
@@ -250,7 +244,7 @@ class Cache {
// Obtiene el objeto de texto a partir de un nombre
auto getText(const std::string& name) -> std::shared_ptr<Text>;
// Obtiene los datos de animación parseados a partir de un nombre
// Obtiene los bytes YAML de animación a partir de un nombre (parsing lazy)
auto getAnimationData(const std::string& name) -> const ResourceAnimation&;
// Obtiene el mapa de tiles a partir de un nombre