diff --git a/animation.cpp b/animation.cpp index c6e6d2c..e98382c 100644 --- a/animation.cpp +++ b/animation.cpp @@ -89,21 +89,30 @@ namespace components { } } - bool Animation::next_frame() { - if(playing && current < frames) { - subframe += speed; - current = looped ? int(subframe) % frames : int(subframe); - } else if(toggled) { - playing = false; + void Animation::next_frame() { + subframe += speed; + + if(looped) { + // it never ends so always do this + current = int(subframe) % frames; + return; + } + + current = int(subframe); + playing = current < frames; + + if(playing) { + return; + } + + // not looped handle not_playing/playing/toggled + if(toggled) { current = frames - 1; subframe = float(frames - 1); } else { - playing = false; current = 0; subframe = 0.0f; } - - return playing; } void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { @@ -111,7 +120,7 @@ namespace components { dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >"); next_frame(); - if(playing && !simple) { + if(!simple) { rect_out.position.x = (current % frames) * frame_width; } @@ -121,7 +130,8 @@ namespace components { bool Animation::apply(sf::Transformable& target, sf::Vector2f pos) { sf::Vector2f scale{min_x, min_y}; - if(next_frame()) { + if(playing) { + next_frame(); tween(scale, pos); target.setPosition(pos); @@ -137,23 +147,26 @@ namespace components { sf::IntRect rect{{0,0}, {frame_width, frame_height}}; sf::Vector2f scale{min_x, min_y}; - step(scale, pos, rect); + if(playing) { + step(scale, pos, rect); - sprite.setPosition(pos); + sprite.setPosition(pos); - if(scaled) { - sprite.setScale(scale); + if(scaled) { + sprite.setScale(scale); + } + + sprite.setTextureRect(rect); } - sprite.setTextureRect(rect); - return playing; } bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) { sf::Vector2f scale{min_x, min_y}; - if(next_frame()) { + if(playing) { + next_frame(); tween(scale, pos); view_out.setCenter(pos); diff --git a/components.hpp b/components.hpp index bcde96b..5f85c61 100644 --- a/components.hpp +++ b/components.hpp @@ -170,7 +170,7 @@ namespace components { void lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out); void tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out); - bool next_frame(); + void next_frame(); void play(); float twitching();