From be360b8a47fc26f7576a3f4a64e52ccf5bf9b9b1 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Tue, 8 Sep 2020 17:34:47 -0400 Subject: [PATCH] sprite update accepts a list of subsections of the sprite to draw --- demo/Demo.cpp | 3 ++- src/Sprite.cpp | 51 +++++++++++++++++++++++++++++++++----------------- src/Sprite.hpp | 8 +++++--- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index a236b8a..52a72c3 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -16,7 +16,8 @@ base classes, private and public class members, pixel class iterator, sprite movement history, input history, use seconds instead of milliseconds, store a node's animations in list, frame object for sprite class, inline short - functions, add box2d to library, load in separate thread and display progress + functions, add box2d to library, load in separate thread and display progress, + add anchor to box class :) SWEATY HANDS :) OILY SNACKS :) AND BAD HYGIENE :) diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 44993a0..480623c 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -778,7 +778,7 @@ void Sprite::advance_wipe_frame() } } -const std::vector& Sprite::get_current_wipe_blinds() +const std::vector& Sprite::get_current_wipe_blinds() const { return wipe_blinds[wipe_index]; } @@ -846,7 +846,7 @@ void Sprite::set_draw_children_on_frame(bool on_frame) draw_children_on_frame = on_frame; } -void Sprite::update() +void Sprite::update(const std::vector& subsections) { if (active) { @@ -872,28 +872,22 @@ void Sprite::update() } for (std::size_t box_ii = 0; box_ii < boxes.size(); box_ii++) { - if (!wipe_animation.is_playing(false)) + if (subsections.size() == 0 && !wipe_animation.is_playing(false)) { SDL_RenderCopyF(renderer, texture, nullptr, &boxes[box_ii]); } - else + else { - for (const Box& blind : get_current_wipe_blinds()) + if (wipe_animation.is_playing(false)) { - bottom_save = std::round(blind.get_bottom()); - subsection = blind; - subsection.y += bottom_save - (subsection.y + subsection.h); - subsection_destination = subsection; - subsection_destination.x += get_left(); - subsection_destination.y += get_top(); - if (get_scale() != 1) + for (const Box& blind : get_current_wipe_blinds()) { - unscaled_blind = blind; - unscaled_blind.set_nw(unscaled_blind.get_nw() / get_scale()); - unscaled_blind.set_size(unscaled_blind.get_size() / get_scale()); - subsection = unscaled_blind; + render_subsection(renderer, texture, blind, get_box(box_ii)); } - SDL_RenderCopy(renderer, texture, &subsection, &subsection_destination); + } + for (const Box& subsection : subsections) + { + render_subsection(renderer, texture, subsection, get_box(box_ii)); } } } @@ -923,6 +917,29 @@ void Sprite::update() } } +void Sprite::update() +{ + update({}); +} + +void Sprite::render_subsection(SDL_Renderer* renderer, SDL_Texture* texture, const Box& subsection, const Box& box) +{ + bottom_save = std::round(subsection.get_bottom()); + subsection_int_rect = SDL_Rect(subsection); + subsection_int_rect.y += bottom_save - (subsection_int_rect.y + subsection_int_rect.h); + subsection_destination = subsection_int_rect; + subsection_destination.x += box.get_left(); + subsection_destination.y += box.get_top(); + if (get_scale() != 1) + { + unscaled_subsection = subsection; + unscaled_subsection.set_nw(unscaled_subsection.get_nw() / get_scale()); + unscaled_subsection.set_size(unscaled_subsection.get_size() / get_scale()); + subsection_int_rect = unscaled_subsection; + } + SDL_RenderCopy(renderer, texture, &subsection_int_rect, &subsection_destination); +} + void Sprite::set_to_leave_memory_allocated() { leave_memory_allocated = true; diff --git a/src/Sprite.hpp b/src/Sprite.hpp index 6426782..06eff3d 100644 --- a/src/Sprite.hpp +++ b/src/Sprite.hpp @@ -40,10 +40,10 @@ struct Sprite : Node std::string current_frameset_name; glm::bvec2 wrap = {false, false}; int texture_access = SDL_TEXTUREACCESS_TARGET, wipe_index = 0, wipe_increment = -1; - Box wrap_frame, unscaled_blind; + Box wrap_frame, unscaled_subsection; bool leave_memory_allocated = false, hidden = false, draw_children_on_frame = true; std::vector> wipe_blinds; - SDL_Rect subsection, subsection_destination; + SDL_Rect subsection_int_rect, subsection_destination; std::list children = {}; Sprite(); @@ -128,7 +128,7 @@ struct Sprite : Node bool collide(const Sprite&, Box&, bool = false, bool = false, bool = false) const; void wipe(float = 0.0f); void advance_wipe_frame(); - const std::vector& get_current_wipe_blinds(); + const std::vector& get_current_wipe_blinds() const; void reverse_wipe_direction(); Sprite& insert_child(std::string, std::list::iterator); Sprite& insert_child(std::string, std::string); @@ -138,7 +138,9 @@ struct Sprite : Node bool has_child(std::string) const; void remove_child(std::string); void set_draw_children_on_frame(bool); + virtual void update(const std::vector&); virtual void update(); + void render_subsection(SDL_Renderer*, SDL_Texture*, const Box&, const Box&); void set_to_leave_memory_allocated(); void set_to_deallocate_memory(); virtual std::string get_class_name() { return "Sprite"; }