From 41af01ee0b7c21c82778f1262557680f262d088a Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 19 Jun 2019 22:16:06 +0200 Subject: [PATCH] Better handling of Animations, their members and the destructor. --- include/Animation.h | 18 +++++++++--------- include/effect_animation.h | 8 ++++---- src/Animation.cpp | 38 ++++++++++++++++++++++++++------------ src/effect_animation.cpp | 10 +++++++--- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/include/Animation.h b/include/Animation.h index 6ecfa79..37c775e 100644 --- a/include/Animation.h +++ b/include/Animation.h @@ -41,7 +41,7 @@ protected: unsigned long currentFrameSince; uint8_t currentFrame = 0; uint8_t* animation_data; - CRGB** _colors; + CRGB** _colors = NULL; CRGB* fgColor = NULL; CRGB* bgColor = new CRGB(0x000000); int8_t xOffset = 0; @@ -51,22 +51,22 @@ protected: Window* _window; uint8_t _width; uint8_t _height; - uint8_t _frame_count; - uint8_t _color_count; - uint16_t* _frame_times; - uint16_t* _frame_data_lengths; - uint8_t** _frame_data; + uint8_t _frame_count = 0; + uint8_t _color_count = 0; + uint16_t* _frame_times = NULL; + uint16_t* _frame_data_lengths = NULL; + uint8_t** _frame_data = NULL; bool _data_valid = false; virtual CRGB* getColor(uint8_t color_index); - + void drawPixel(int index, CRGB* color); uint16_t getFrameDelay(int frame); bool _load_from_file(const char* c); public: Animation(const char* filename, Window* win); - void setFgColor(CRGB*); - void setBgColor(CRGB* bg_color); + void setFgColor(uint32_t c); + void setBgColor(uint32_t c); bool invert(); void setOffsets(int8_t x, int8_t y); void setStartFrame(uint8_t sf); diff --git a/include/effect_animation.h b/include/effect_animation.h index a9854aa..5f3b0b3 100644 --- a/include/effect_animation.h +++ b/include/effect_animation.h @@ -8,13 +8,13 @@ class AnimationEffect : public Effect { private: Animation *animation; - CRGB *bg_color; uint16_t xOffset; uint16_t yOffset; public: - AnimationEffect(const char* name) : AnimationEffect(name, new CRGB(0x000000), 0, 0) {} - AnimationEffect(const char* name, CRGB* background_color) : AnimationEffect(name, background_color, 0, 0) {} - AnimationEffect(const char* name, CRGB* bg_color, int x, int y); + AnimationEffect(const char* name) : AnimationEffect(name, 0x000000, 0, 0) {} + AnimationEffect(const char* name, uint32_t bg_color) : AnimationEffect(name, bg_color, 0, 0) {} + AnimationEffect(const char* name, uint32_t bg_color, int x, int y); ~AnimationEffect(); + AnimationEffect* setFgColor(uint32_t c); void loop(); }; diff --git a/src/Animation.cpp b/src/Animation.cpp index f8eeaa5..cf4ee3e 100644 --- a/src/Animation.cpp +++ b/src/Animation.cpp @@ -143,7 +143,7 @@ bool Animation::_load_from_file(const char* filename) { } file.close(); - + LOGln("Animation * Loading completed successfully."); return true; @@ -160,12 +160,14 @@ Animation::Animation(const char* filename, Window* win) { } } -void Animation::setFgColor(CRGB* fg_color) { - this->fgColor = fg_color; +void Animation::setFgColor(uint32_t c) { + if (this->fgColor) delete this->fgColor; + this->fgColor = new CRGB(c); } -void Animation::setBgColor(CRGB* bg_color) { - this->bgColor = bg_color; +void Animation::setBgColor(uint32_t c) { + if (this->bgColor) delete this->bgColor; + this->bgColor = new CRGB(c); } bool Animation::invert() { @@ -205,15 +207,27 @@ void Animation::setSingleFrame(uint8_t frame) { Animation::~Animation() { for (int i=0; i<_color_count; i++) delete _colors[i]; - delete [] _colors; - if (fgColor) delete fgColor; - delete bgColor; - delete [] _frame_data_lengths; - delete [] _frame_times; + LOGln("Deleting _colors..."); + if (_colors) delete [] _colors; + LOGln("Deleting fgColor..."); + + if (fgColor != NULL) delete fgColor; + LOGln("Deleting bgColor..."); + + if (bgColor != NULL) delete bgColor; + LOGln("Deleting _frame_data_lengths..."); + + if (_frame_data_lengths) delete [] _frame_data_lengths; + LOGln("Deleting _frame_times..."); + + if (_frame_times) delete [] _frame_times; for (int i=0; i<_frame_count; i++) { delete [] _frame_data[i]; } - delete [] _frame_data; + LOGln("Deleting _frame_data..."); + + if (_frame_data) delete [] _frame_data; + LOGln("Deleteion done."); } void Animation::draw() { @@ -230,7 +244,7 @@ void Animation::drawFrame(uint8_t frame_index) { CRGB red(0xFF0000); CRGB black(0x000000); for (int x=0; x<_window->width; x++) for (int y=0; y<_window->height; y++) { - _window->setPixel(x, y, (y*_window->width+x) % 2 ? &red : &black); + _window->setPixel(x, y, (y*_window->width+x + y) % 2 ? &red : &black); } return; } diff --git a/src/effect_animation.cpp b/src/effect_animation.cpp index 725c0e5..09e201c 100644 --- a/src/effect_animation.cpp +++ b/src/effect_animation.cpp @@ -1,16 +1,20 @@ #include "effect_animation.h" #include "functions.h" -AnimationEffect::AnimationEffect(const char* name, CRGB* bg, int x, int y) { - this->bg_color = bg; +AnimationEffect::AnimationEffect(const char* name, uint32_t bg, int x, int y) { this->xOffset = x; this->yOffset = y; this->animation = new Animation(name, window); - this->animation->setBgColor(this->bg_color); + this->animation->setBgColor(bg); this->animation->setOffsets(this->xOffset, this->yOffset); } +AnimationEffect* AnimationEffect::setFgColor(uint32_t c) { + animation->setFgColor(c); + return this; +} + AnimationEffect::~AnimationEffect() { delete this->animation; }