diff --git a/source/core/rendering/surface_animated_sprite.cpp b/source/core/rendering/surface_animated_sprite.cpp index f5926d8..07845f2 100644 --- a/source/core/rendering/surface_animated_sprite.cpp +++ b/source/core/rendering/surface_animated_sprite.cpp @@ -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(); + // 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(yaml["frameWidth"].get_value()); + } + + if (yaml.contains("frameHeight")) { + frame_height = static_cast(yaml["frameHeight"].get_value()); + } + + // Calculate sprite sheet parameters + int frames_per_row = 1; + int max_tiles = 1; + if (surface_) { + frames_per_row = surface_->getWidth() / static_cast(frame_width); + const int W = surface_->getWidth() / static_cast(frame_width); + const int H = surface_->getHeight() / static_cast(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(); + } + + // Parse speed (seconds per frame) + if (anim_node.contains("speed")) { + animation.speed = anim_node["speed"].get_value(); + } + + // Parse loop frame index + if (anim_node.contains("loop")) { + animation.loop = anim_node["loop"].get_value(); + } + + // 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; } } diff --git a/source/core/resources/resource_cache.cpp b/source/core/resources/resource_cache.cpp index f9a2bcf..7906e04 100644 --- a/source/core/resources/resource_cache.cpp +++ b/source/core/resources/resource_cache.cpp @@ -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; - 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(); } } diff --git a/source/core/resources/resource_cache.hpp b/source/core/resources/resource_cache.hpp index 2a2e977..5da4a90 100644 --- a/source/core/resources/resource_cache.hpp +++ b/source/core/resources/resource_cache.hpp @@ -80,19 +80,13 @@ struct ResourceText { // Estructura para almacenar ficheros animaciones y su nombre struct ResourceAnimation { - std::string name; // Nombre del fichero - std::vector animations; // Datos de animaciones parseadas desde YAML - std::shared_ptr 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 yaml_data; // Bytes del archivo YAML sin parsear // Constructor - ResourceAnimation(std::string name, std::vector animations, std::shared_ptr surface, float frame_width, float frame_height) + ResourceAnimation(std::string name, std::vector 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; - // 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