feat(anim): suport de flip H/V per frame i per animació
This commit is contained in:
1 parent
39eda56c9d
commit
1d43a77800
5 files changed
+438
-87
No files matched your search
@@ -7,36 +7,19 @@
|
||||
|
||||
#include "core/rendering/surface.hpp"
|
||||
#include "core/resources/resource_cache.hpp"
|
||||
#include "external/fkyaml_node.hpp"
|
||||
|
||||
// El parser pure de YAML d'animacions (parseAnimationsFromYaml + helpers)
|
||||
// viu a `animated_sprite_parser.cpp` perquè és testable sense Surface ni
|
||||
// Resource::Cache. Aquesta unitat es queda amb el runtime de la classe i
|
||||
// amb el que sí toca el cache de surfaces (parseSpriteBlock).
|
||||
|
||||
namespace {
|
||||
|
||||
auto convertYamlFramesToRects(const fkyaml::node& frames_node, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> std::vector<SDL_FRect> {
|
||||
std::vector<SDL_FRect> frames;
|
||||
SDL_FRect rect = {.x = 0.0F, .y = 0.0F, .w = frame_width, .h = frame_height};
|
||||
for (const auto& frame_index_node : frames_node) {
|
||||
const int NUM_TILE = frame_index_node.get_value<int>();
|
||||
if (NUM_TILE <= max_tiles) {
|
||||
rect.x = static_cast<float>(NUM_TILE % frames_per_row) * frame_width;
|
||||
rect.y = static_cast<float>(NUM_TILE / frames_per_row) * frame_height;
|
||||
frames.emplace_back(rect);
|
||||
}
|
||||
}
|
||||
return frames;
|
||||
}
|
||||
|
||||
struct SheetParams {
|
||||
float frame_width{0.0F};
|
||||
float frame_height{0.0F};
|
||||
int frames_per_row{1};
|
||||
int max_tiles{1};
|
||||
};
|
||||
|
||||
// Llegeix el bloc `sprite: { path, size: [w, h] }` — mateix format que
|
||||
// les plantilles d'entitat. La surface es resol via Resource::Cache amb
|
||||
// el basename del fitxer.
|
||||
auto parseSpriteBlock(const fkyaml::node& yaml, std::shared_ptr<Surface>& surface) -> SheetParams {
|
||||
SheetParams params;
|
||||
auto parseSpriteBlock(const fkyaml::node& yaml, std::shared_ptr<Surface>& surface) -> AnimatedSprite::SheetParams {
|
||||
AnimatedSprite::SheetParams params;
|
||||
if (!yaml.contains("sprite")) { return params; }
|
||||
const auto& s = yaml["sprite"];
|
||||
if (s.contains("path")) {
|
||||
@@ -57,62 +40,18 @@ namespace {
|
||||
return params;
|
||||
}
|
||||
|
||||
// `frame_ms` pot ser un escalar (temps uniforme) o una llista (per-frame).
|
||||
// Convertim sempre a segons perquè `animate()` treballa amb delta_time
|
||||
// en segons. Si falta o val 0, `speeds` queda buit i l'animació es
|
||||
// considera estàtica (no avança).
|
||||
void parseFrameTimes(const fkyaml::node& anim_node, AnimatedSprite::AnimationData& animation) {
|
||||
if (!anim_node.contains("frame_ms")) { return; }
|
||||
const auto& fm = anim_node["frame_ms"];
|
||||
if (fm.is_sequence()) {
|
||||
for (const auto& v : fm) {
|
||||
animation.speeds.push_back(static_cast<float>(v.get_value<int>()) / 1000.0F);
|
||||
}
|
||||
} else {
|
||||
const float SEC = static_cast<float>(fm.get_value<int>()) / 1000.0F;
|
||||
if (SEC > 0.0F) { animation.speeds.assign(animation.frames.size(), SEC); }
|
||||
}
|
||||
}
|
||||
|
||||
// Ordre de prioritat per a la política de loop:
|
||||
// 1) `loop_from: N` → rebota al frame N en passar del final (implica loop).
|
||||
// 2) `loop: false` → animació d'un sol tret (es queda al darrer frame).
|
||||
// 3) Res → loop complet des del frame 0 (default).
|
||||
void parseLoopPolicy(const fkyaml::node& anim_node, AnimatedSprite::AnimationData& animation) {
|
||||
if (anim_node.contains("loop_from")) {
|
||||
animation.loop_from = anim_node["loop_from"].get_value<int>();
|
||||
} else if (anim_node.contains("loop") && !anim_node["loop"].get_value<bool>()) {
|
||||
animation.loop_from = -1;
|
||||
} else {
|
||||
animation.loop_from = 0;
|
||||
}
|
||||
}
|
||||
|
||||
auto parseAnimations(const fkyaml::node& yaml, const SheetParams& params) -> std::unordered_map<std::string, AnimatedSprite::AnimationData> {
|
||||
std::unordered_map<std::string, AnimatedSprite::AnimationData> animations;
|
||||
if (!yaml.contains("animations") || !yaml["animations"].is_mapping()) {
|
||||
return animations;
|
||||
}
|
||||
const auto& anims_node = yaml["animations"];
|
||||
for (auto it = anims_node.begin(); it != anims_node.end(); ++it) {
|
||||
const auto NAME = it.key().get_value<std::string>();
|
||||
const auto& anim_node = *it;
|
||||
|
||||
AnimatedSprite::AnimationData animation;
|
||||
if (anim_node.contains("frames") && anim_node["frames"].is_sequence()) {
|
||||
animation.frames = convertYamlFramesToRects(
|
||||
anim_node["frames"],
|
||||
params.frame_width,
|
||||
params.frame_height,
|
||||
params.frames_per_row,
|
||||
params.max_tiles);
|
||||
}
|
||||
parseFrameTimes(anim_node, animation);
|
||||
parseLoopPolicy(anim_node, animation);
|
||||
|
||||
animations.emplace(NAME, std::move(animation));
|
||||
}
|
||||
return animations;
|
||||
// Tradueix els flags lògics flip_h/flip_v al `SDL_FlipMode` que admet
|
||||
// `Surface::render`. Important: a SDL3, `SDL_FlipMode` és un enum
|
||||
// exclusiu (NONE/HORIZONTAL/VERTICAL), no un bitmask combinable. Per
|
||||
// tant, V+H simultani al mateix frame no es pot expressar amb una
|
||||
// única crida — prioritzem H (suficient per als casos pràctics
|
||||
// d'animació, com `walk_west` o `death_spin`). Si en algun moment fa
|
||||
// falta combinar, caldrà ampliar `Surface::render` per a doble-flipar
|
||||
// a baix nivell.
|
||||
auto toFlipMode(bool flip_h, bool flip_v) -> SDL_FlipMode {
|
||||
if (flip_h) { return SDL_FLIP_HORIZONTAL; }
|
||||
if (flip_v) { return SDL_FLIP_VERTICAL; }
|
||||
return SDL_FLIP_NONE;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -122,7 +61,7 @@ AnimatedSprite::AnimatedSprite(const AnimationResource& cached_data) {
|
||||
try {
|
||||
auto yaml = fkyaml::node::deserialize(yaml_content);
|
||||
auto params = parseSpriteBlock(yaml, surface_);
|
||||
animations_ = parseAnimations(yaml, params);
|
||||
animations_ = parseAnimationsFromYaml(yaml, params);
|
||||
|
||||
setWidth(params.frame_width);
|
||||
setHeight(params.frame_height);
|
||||
@@ -132,7 +71,7 @@ AnimatedSprite::AnimatedSprite(const AnimationResource& cached_data) {
|
||||
// abans del primer `update()`, així que el valor real el posa ell.
|
||||
if (!animations_.empty()) {
|
||||
const auto& first = animations_.begin()->second;
|
||||
if (!first.frames.empty()) { setClip(first.frames[0]); }
|
||||
if (!first.frames.empty()) { setClip(first.frames[0].rect); }
|
||||
}
|
||||
} catch (const fkyaml::exception& e) {
|
||||
std::cerr << "YAML parsing error in animation " << cached_data.name << ": " << e.what() << '\n';
|
||||
@@ -154,11 +93,21 @@ void AnimatedSprite::animate(float delta_time) {
|
||||
|
||||
const float TOTAL = std::accumulate(anim.speeds.begin(), anim.speeds.end(), 0.0F);
|
||||
|
||||
// Helper local per a propagar el frame actual (clip + opcionalment flip)
|
||||
// al sprite. El flip només s'escriu si l'animació declara `controls_flip`,
|
||||
// per a no trepitjar el flip que el codi del joc encara controla
|
||||
// externament en animacions sense declaració de flip al YAML.
|
||||
auto apply_current_frame = [&](int frame_index) {
|
||||
const auto& frame = anim.frames[frame_index];
|
||||
setClip(frame.rect);
|
||||
if (anim.controls_flip) { setFlip(toFlipMode(frame.flip_h, frame.flip_v)); }
|
||||
};
|
||||
|
||||
if (anim.accumulated_time >= TOTAL) {
|
||||
if (anim.loop_from < 0) {
|
||||
anim.current_frame = static_cast<int>(anim.frames.size()) - 1;
|
||||
anim.completed = true;
|
||||
setClip(anim.frames[anim.current_frame]);
|
||||
apply_current_frame(anim.current_frame);
|
||||
return;
|
||||
}
|
||||
float loop_start = 0.0F;
|
||||
@@ -172,13 +121,13 @@ void AnimatedSprite::animate(float delta_time) {
|
||||
cursor += anim.speeds[i];
|
||||
if (anim.accumulated_time < cursor) {
|
||||
anim.current_frame = i;
|
||||
setClip(anim.frames[i]);
|
||||
apply_current_frame(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
anim.current_frame = static_cast<int>(anim.frames.size()) - 1;
|
||||
setClip(anim.frames[anim.current_frame]);
|
||||
apply_current_frame(anim.current_frame);
|
||||
}
|
||||
|
||||
auto AnimatedSprite::animationIsCompleted() -> bool {
|
||||
@@ -200,7 +149,10 @@ void AnimatedSprite::setCurrentAnimation(const std::string& name) {
|
||||
anim.current_frame = 0;
|
||||
anim.accumulated_time = 0.0F;
|
||||
anim.completed = false;
|
||||
if (!anim.frames.empty()) { setClip(anim.frames[0]); }
|
||||
if (!anim.frames.empty()) {
|
||||
setClip(anim.frames[0].rect);
|
||||
if (anim.controls_flip) { setFlip(toFlipMode(anim.frames[0].flip_h, anim.frames[0].flip_v)); }
|
||||
}
|
||||
}
|
||||
|
||||
void AnimatedSprite::update(float delta_time) {
|
||||
|
||||
Reference in new issue
Block a user