From 594be65f45ca15c2371aaf96fbdf03670ad6a50f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 24 Feb 2026 22:51:55 -0500 Subject: [PATCH] Refine how the raycaster.cpp uses animations so there's less back-and-forth on the intrect. --- animation.cpp | 16 +++++++++++++++- animation.hpp | 2 ++ raycaster.cpp | 5 +---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/animation.cpp b/animation.cpp index fd1333e..6eeeda0 100644 --- a/animation.cpp +++ b/animation.cpp @@ -50,10 +50,23 @@ namespace animation { dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects"); // NOTE: pos is not updated yet auto& rect = $frame_rects.at(sequence.current); - fmt::println("setting texture rect: {},{}", rect.position.x, rect.position.y); sprite.setTextureRect(rect); } + + /* + * Alternative mostly used in raycaster.cpp that + * DOES NOT setTextureRect() but just points + * the rect_io at the correct frame, but leaves + * it's size and base position alone. + */ + void Animation::apply(sf::Sprite& sprite, sf::IntRect& rect_io) { + dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects"); + auto& rect = $frame_rects.at(sequence.current); + rect_io.position.x += rect.position.x; + rect_io.position.y += rect.position.y; + } + void Animation::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) { dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio"); dbc::check(transform.flipped == false, "transform must be false, has no effect on View"); @@ -246,6 +259,7 @@ namespace animation { Animation anim; animation::from_json(data[anim_name], anim); + anim.name = anim_name; dbc::check(anim.forms.contains("idle"), fmt::format("animation {} must have 'idle' form", anim_name)); diff --git a/animation.hpp b/animation.hpp index dfba6ae..2f313bf 100644 --- a/animation.hpp +++ b/animation.hpp @@ -117,6 +117,7 @@ namespace animation { std::string form_name="idle"; std::string sequence_name=""; std::string transform_name=""; + std::string name=""; std::vector calc_frames(); void play(); @@ -125,6 +126,7 @@ namespace animation { bool has_form(const std::string& as_form); void set_form(const std::string& form); void apply(sf::Sprite& sprite); + void apply(sf::Sprite& sprite, sf::IntRect& rect_io); void apply_effect(std::shared_ptr effect); void update(); void motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale); diff --git a/raycaster.cpp b/raycaster.cpp index 1163834..27dd321 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -106,11 +106,8 @@ inline void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf:: if(anim != nullptr && anim->playing) { anim->update(); - anim->apply(sprite); + anim->apply(sprite, in_texture); anim->motion(sprite, position, scale); - - auto& old_rect = sprite.getTextureRect(); - in_texture.position.x += old_rect.position.x; } sprite.setScale(scale);