From 35a5acbd532bdaf03a869e6b57664a787eb51134 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Sat, 5 Sep 2020 19:40:59 -0400 Subject: [PATCH] optionally exclude delay time from checking if animation is playing --- src/Animation.cpp | 4 +-- src/Animation.hpp | 2 +- src/Box.cpp | 2 ++ src/Box.hpp | 5 +--- src/Sprite.cpp | 76 +++++++++++++++++++++++------------------------ src/Sprite.hpp | 3 +- src/extension.cpp | 2 +- src/extension.hpp | 2 +- 8 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/Animation.cpp b/src/Animation.cpp index 3495667..45f57f9 100644 --- a/src/Animation.cpp +++ b/src/Animation.cpp @@ -44,9 +44,9 @@ void Animation::reset() timer.reset(); } -bool Animation::is_playing() +bool Animation::is_playing(bool include_delay) { - return playing && !paused; + return playing && !paused && (include_delay || delay <= 0); } void Animation::update() diff --git a/src/Animation.hpp b/src/Animation.hpp index 1c852e8..7f43742 100644 --- a/src/Animation.hpp +++ b/src/Animation.hpp @@ -35,7 +35,7 @@ struct Animation void pause(); void unpause(); void reset(); - bool is_playing(); + bool is_playing(bool = true); void update(); }; diff --git a/src/Box.cpp b/src/Box.cpp index f48a8c4..e4a37c2 100644 --- a/src/Box.cpp +++ b/src/Box.cpp @@ -1,3 +1,5 @@ +#include "extension.hpp" +#include "Segment.hpp" #include "Box.hpp" Box::Box(const glm::vec2& nw, const glm::vec2& size) diff --git a/src/Box.hpp b/src/Box.hpp index 2aa59a1..d385ad2 100644 --- a/src/Box.hpp +++ b/src/Box.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include "SDL.h" #define GLM_ENABLE_EXPERIMENTAL #include "glm/common.hpp" @@ -78,7 +78,4 @@ struct Box : SDL_FRect std::ostream& operator<<(std::ostream&, const Box&); -#include "extension.hpp" -#include "Segment.hpp" - #endif diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 2d80dfd..963f1bf 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -26,7 +26,6 @@ void Sprite::reset() Node::reset(); activate(); wipe_animation.reset(); - reset_wipe_index(); unhide(); } @@ -108,7 +107,6 @@ void Sprite::add_frames(SDL_Texture* frame) frameset.set_size(); } update_size(preserve_center); - reset_wipe_index(); } void Sprite::add_frames(const std::vector& frames) @@ -166,7 +164,14 @@ void Sprite::set_frame_length(float length) SDL_Texture* Sprite::get_current_frame() const { - return frames[get_current_frameset().get_current_frame_index()]; + if (frames.size() == 0) + { + return nullptr; + } + else + { + return frames[get_current_frameset().get_current_frame_index()]; + } } const Box& Sprite::get_box(int index) const @@ -198,7 +203,6 @@ void Sprite::update_size(bool preserve_center) boxes[ii].scale(scale, preserve_center); } } - wipe_blinds = sfw::get_blinds_boxes(get_size()); } void Sprite::set_scale(float s) @@ -738,12 +742,21 @@ bool Sprite::collide(const Sprite& sprite, Box& overlap, bool precise, bool all, return collide(sprite, precise, &overlap, all, all_other); } -void Sprite::wipe() +void Sprite::wipe(float delay) { - wipe_animation.play(); + wipe_blinds = sfw::get_blinds_boxes(get_size()); + if (wipe_increment < 0) + { + wipe_index = static_cast(wipe_blinds.size() - 1); + } + else + { + wipe_index = 0; + } + wipe_animation.play(delay); for (Child& child : children) { - child.sprite.wipe(); + child.sprite.wipe(delay); } } @@ -773,19 +786,6 @@ const std::vector& Sprite::get_current_wipe_blinds() void Sprite::reverse_wipe_direction() { wipe_increment *= -1; - reset_wipe_index(); -} - -void Sprite::reset_wipe_index() -{ - if (wipe_increment < 0) - { - wipe_index = static_cast(wipe_blinds.size() - 1); - } - else - { - wipe_index = 0; - } } Sprite& Sprite::insert_child(std::string name, std::list::iterator position) @@ -858,9 +858,9 @@ void Sprite::update() blink_animation.update(); wipe_animation.update(); toggle_hidden_animation.update(); + SDL_Texture* texture = get_current_frame(); if (is_loaded() && !is_hidden() && get_current_frameset().get_frame_count()) { - SDL_Texture* texture = get_current_frame(); SDL_Renderer* renderer = get_root()->renderer; SDL_SetTextureAlphaMod(texture, alpha_mod); SDL_SetTextureColorMod(texture, color_mod.r, color_mod.g, color_mod.b); @@ -872,7 +872,7 @@ void Sprite::update() } for (std::size_t box_ii = 0; box_ii < boxes.size(); box_ii++) { - if (!wipe_animation.is_playing()) + if (!wipe_animation.is_playing(false)) { SDL_RenderCopyF(renderer, texture, nullptr, &boxes[box_ii]); } @@ -901,23 +901,23 @@ void Sprite::update() { SDL_RenderSetClipRect(renderer, nullptr); } - for (Child& child : children) + } + for (Child& child : children) + { + if (draw_children_on_frame) { - if (draw_children_on_frame) - { - child.sprite.set_canvas(texture); - } - else - { - child.sprite.set_canvas(get_canvas()); - child_relative_position = child.sprite.get_nw(); - child.sprite.move(get_nw()); - } - child.sprite.update(); - if (!draw_children_on_frame) - { - child.sprite.set_nw(child_relative_position); - } + child.sprite.set_canvas(texture); + } + else + { + child.sprite.set_canvas(get_canvas()); + child_relative_position = child.sprite.get_nw(); + child.sprite.move(get_nw()); + } + child.sprite.update(); + if (!draw_children_on_frame) + { + child.sprite.set_nw(child_relative_position); } } } diff --git a/src/Sprite.hpp b/src/Sprite.hpp index 20396c5..1d083a7 100644 --- a/src/Sprite.hpp +++ b/src/Sprite.hpp @@ -126,11 +126,10 @@ struct Sprite : Node bool collide(const Box&, Box&, bool = false, bool = false) const; bool collide(const Sprite&, bool = false, Box* = NULL, bool = false, bool = false) const; bool collide(const Sprite&, Box&, bool = false, bool = false, bool = false) const; - void wipe(); + void wipe(float = 0.0f); void advance_wipe_frame(); const std::vector& get_current_wipe_blinds(); void reverse_wipe_direction(); - void reset_wipe_index(); Sprite& insert_child(std::string, std::list::iterator); Sprite& insert_child(std::string, std::string); Sprite& insert_child(std::string, int); diff --git a/src/extension.cpp b/src/extension.cpp index 23030ce..8f107f3 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -15,7 +15,7 @@ Box sfw::get_texture_box(SDL_Texture* texture) return Box(glm::vec2(0, 0), glm::vec2(width, height)); } -glm::vec2 sfw::fit_and_preserve_aspect(glm::vec2 inner, glm::vec2 outer) +glm::vec2 sfw::fit_and_preserve_aspect(const glm::vec2& inner, const glm::vec2& outer) { glm::vec2 delta = inner - outer; float aspect = inner.x / inner.y; diff --git a/src/extension.hpp b/src/extension.hpp index 3f75228..8bdfd11 100644 --- a/src/extension.hpp +++ b/src/extension.hpp @@ -35,7 +35,7 @@ namespace sfw void set_magnitude(glm::vec2&, float); Box get_texture_box(SDL_Texture*); - glm::vec2 fit_and_preserve_aspect(glm::vec2, glm::vec2); + glm::vec2 fit_and_preserve_aspect(const glm::vec2&, const glm::vec2&); std::vector> get_blinds_boxes(glm::vec2, float = 0.05f, int = 4); void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector>&); void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector>&, const Box&);