From 536a5ec5f553fed635acbeb1ea6c5ae803afa8ac Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Thu, 27 Aug 2020 20:41:21 -0400 Subject: [PATCH] separate move weighted from move in sprite class --- demo/Demo.cpp | 3 ++- src/Sprite.cpp | 59 +++++++++++++++++++++++++++++--------------------- src/Sprite.hpp | 5 +++-- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index ae5c702..98352b1 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -13,7 +13,8 @@ resolution, debug display, loading wheel animation, shadowed sprite, separate update and draw, sprite movement cage, multiple windows, multiple renderers, node children list, node animations list, copy constructor for node, private - and public class members, pixel class iterator + and public class members, pixel class iterator, sprite movement history, input + history :) SWEATY HANDS :) OILY SNACKS :) AND BAD HYGIENE :) diff --git a/src/Sprite.cpp b/src/Sprite.cpp index d4e9a87..b434f35 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -207,11 +207,14 @@ bool Sprite::is_loaded() const return !frames.empty(); } -void Sprite::unload() +void Sprite::unload(bool leave_memory_allocated) { while (!frames.empty()) { - SDL_DestroyTexture(frames.back()); + if (!leave_memory_allocated) + { + SDL_DestroyTexture(frames.back()); + } frames.pop_back(); } for (Box& box : boxes) @@ -363,67 +366,67 @@ glm::vec2 Sprite::get_center(int index) void Sprite::set_top(float top) { - move({0, top - get_top()}, false); + move({0, top - get_top()}); } void Sprite::set_right(float right) { - move({right - get_right(), 0}, false); + move({right - get_right(), 0}); } void Sprite::set_bottom(float bottom) { - move({0, bottom - get_bottom()}, false); + move({0, bottom - get_bottom()}); } void Sprite::set_left(float left) { - move({left - get_left(), 0}, false); + move({left - get_left(), 0}); } void Sprite::set_center_x(float x) { - move({x - get_center_x(), 0}, false); + move({x - get_center_x(), 0}); } void Sprite::set_center_y(float y) { - move({0, y - get_center_y()}, false); + move({0, y - get_center_y()}); } void Sprite::set_nw(const glm::vec2& nw) { - move(nw - get_nw(), false); + move(nw - get_nw()); } void Sprite::set_ne(const glm::vec2& ne) { - move(ne - get_ne(), false); + move(ne - get_ne()); } void Sprite::set_se(const glm::vec2& se) { - move(se - get_se(), false); + move(se - get_se()); } void Sprite::set_south(const glm::vec2& south) { - move(south - get_south(), false); + move(south - get_south()); } void Sprite::set_sw(const glm::vec2& sw) { - move(sw - get_sw(), false); + move(sw - get_sw()); } void Sprite::set_west(const glm::vec2& west) { - move(west - get_west(), false); + move(west - get_west()); } void Sprite::set_center(const glm::vec2& center) { - move(center - get_center(), false); + move(center - get_center()); } void Sprite::add_wrap(bool x, bool y) @@ -463,12 +466,8 @@ void Sprite::add_hue_shift_frames(int count) } } -glm::vec2 Sprite::move(glm::vec2 delta, bool weighted) +glm::vec2 Sprite::move(const glm::vec2& delta) { - if (weighted) - { - delta = get_root()->weight(delta); - } for (Box& box : boxes) { box.move(delta); @@ -477,27 +476,34 @@ glm::vec2 Sprite::move(glm::vec2 delta, bool weighted) { if (get_right() > wrap_frame.get_right()) { - move({-wrap_frame.get_w(), 0}, false); + move({-wrap_frame.get_w(), 0}); } else if (get_right() < wrap_frame.get_left()) { - move({wrap_frame.get_w(), 0}, false); + move({wrap_frame.get_w(), 0}); } } if (wrap.y) { if (get_bottom() > wrap_frame.get_bottom()) { - move({0, -wrap_frame.get_h()}, false); + move({0, -wrap_frame.get_h()}); } else if (get_bottom() < wrap_frame.get_top()) { - move({0, wrap_frame.get_h()}, false); + move({0, wrap_frame.get_h()}); } } return delta; } +glm::vec2 Sprite::move_weighted(const glm::vec2& delta) +{ + glm::vec2 delta_weighted = get_root()->weight(delta); + move(delta_weighted); + return delta_weighted; +} + bool Sprite::collide(const glm::vec2& point, bool all) const { if (!all) @@ -667,7 +673,10 @@ void Sprite::update() { if (active) { - move(step); + if (step != glm::vec2(0, 0)) + { + move_weighted(step); + } frame_animation.update(); blink_animation.update(); if (is_loaded() && !is_hidden() && get_current_frameset().get_frame_count()) diff --git a/src/Sprite.hpp b/src/Sprite.hpp index 7c07dff..77a52ff 100644 --- a/src/Sprite.hpp +++ b/src/Sprite.hpp @@ -64,7 +64,7 @@ struct Sprite : Node void set_scale_quality(const std::string&); float get_scale() const; bool is_loaded() const; - void unload(); + virtual void unload(bool = false); void advance_frame(); void hide(); void unhide(); @@ -109,7 +109,8 @@ struct Sprite : Node void add_wrap(bool, bool); void add_wrap(bool, bool, Box); void add_hue_shift_frames(int); - virtual glm::vec2 move(glm::vec2, bool = true); + virtual glm::vec2 move(const glm::vec2&); + virtual glm::vec2 move_weighted(const glm::vec2&); bool collide(const glm::vec2&, bool = false) const; bool collide(const Segment&, glm::vec2* = NULL, bool = false) const; bool collide(const Segment&, glm::vec2&, bool = false) const;