migrant .ani a .yaml
This commit is contained in:
@@ -124,17 +124,95 @@ auto SurfaceAnimatedSprite::loadAnimationsFromYAML(const std::string& file_path,
|
|||||||
return animations;
|
return animations;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor con datos pre-cargados del cache
|
// Constructor con bytes YAML del cache (parsing lazy)
|
||||||
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_data)
|
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_data) {
|
||||||
: SurfaceMovingSprite(cached_data.surface) {
|
// Parsear YAML desde los bytes cargados en cache
|
||||||
// Copiar datos pre-cargados del cache
|
std::string yaml_content(cached_data.yaml_data.begin(), cached_data.yaml_data.end());
|
||||||
animations_ = cached_data.animations;
|
|
||||||
setWidth(cached_data.frame_width);
|
|
||||||
setHeight(cached_data.frame_height);
|
|
||||||
|
|
||||||
// Inicializar con la primera animación si existe
|
try {
|
||||||
if (!animations_.empty() && !animations_[0].frames.empty()) {
|
auto yaml = fkyaml::node::deserialize(yaml_content);
|
||||||
setClip(animations_[0].frames[0]);
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -301,13 +301,11 @@ void Cache::loadAnimations() {
|
|||||||
for (const auto& l : list) {
|
for (const auto& l : list) {
|
||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
|
|
||||||
// Parsear YAML y almacenar datos pre-cargados
|
// Cargar bytes del archivo YAML sin parsear (carga lazy)
|
||||||
std::shared_ptr<Surface> surface;
|
auto yaml_bytes = Helper::loadFile(l);
|
||||||
float frame_width = 0.0F;
|
|
||||||
float frame_height = 0.0F;
|
|
||||||
auto animation_data = SurfaceAnimatedSprite::loadAnimationsFromYAML(l, surface, frame_width, frame_height);
|
|
||||||
|
|
||||||
animations_.emplace_back(name, animation_data, surface, frame_width, frame_height);
|
animations_.emplace_back(name, yaml_bytes);
|
||||||
|
printWithDots("Animation : ", name, "[ LOADED ]");
|
||||||
updateLoadingProgress();
|
updateLoadingProgress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,19 +80,13 @@ struct ResourceText {
|
|||||||
|
|
||||||
// Estructura para almacenar ficheros animaciones y su nombre
|
// Estructura para almacenar ficheros animaciones y su nombre
|
||||||
struct ResourceAnimation {
|
struct ResourceAnimation {
|
||||||
std::string name; // Nombre del fichero
|
std::string name; // Nombre del fichero
|
||||||
std::vector<SurfaceAnimatedSprite::AnimationData> animations; // Datos de animaciones parseadas desde YAML
|
std::vector<uint8_t> yaml_data; // Bytes del archivo YAML sin parsear
|
||||||
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
|
|
||||||
|
|
||||||
// Constructor
|
// 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)),
|
: name(std::move(name)),
|
||||||
animations(std::move(animations)),
|
yaml_data(std::move(yaml_data)) {}
|
||||||
surface(std::move(surface)),
|
|
||||||
frame_width(frame_width),
|
|
||||||
frame_height(frame_height) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para almacenar ficheros con el mapa de tiles de una habitación y su nombre
|
// 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
|
// Obtiene el objeto de texto a partir de un nombre
|
||||||
auto getText(const std::string& name) -> std::shared_ptr<Text>;
|
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&;
|
auto getAnimationData(const std::string& name) -> const ResourceAnimation&;
|
||||||
|
|
||||||
// Obtiene el mapa de tiles a partir de un nombre
|
// Obtiene el mapa de tiles a partir de un nombre
|
||||||
|
|||||||
Reference in New Issue
Block a user