Better handling of Animations, their members and the destructor.

This commit is contained in:
Fabian Schlenz 2019-06-19 22:16:06 +02:00
parent cb4afa5043
commit 41af01ee0b
4 changed files with 46 additions and 28 deletions

View File

@ -41,7 +41,7 @@ protected:
unsigned long currentFrameSince; unsigned long currentFrameSince;
uint8_t currentFrame = 0; uint8_t currentFrame = 0;
uint8_t* animation_data; uint8_t* animation_data;
CRGB** _colors; CRGB** _colors = NULL;
CRGB* fgColor = NULL; CRGB* fgColor = NULL;
CRGB* bgColor = new CRGB(0x000000); CRGB* bgColor = new CRGB(0x000000);
int8_t xOffset = 0; int8_t xOffset = 0;
@ -51,22 +51,22 @@ protected:
Window* _window; Window* _window;
uint8_t _width; uint8_t _width;
uint8_t _height; uint8_t _height;
uint8_t _frame_count; uint8_t _frame_count = 0;
uint8_t _color_count; uint8_t _color_count = 0;
uint16_t* _frame_times; uint16_t* _frame_times = NULL;
uint16_t* _frame_data_lengths; uint16_t* _frame_data_lengths = NULL;
uint8_t** _frame_data; uint8_t** _frame_data = NULL;
bool _data_valid = false; bool _data_valid = false;
virtual CRGB* getColor(uint8_t color_index); virtual CRGB* getColor(uint8_t color_index);
void drawPixel(int index, CRGB* color); void drawPixel(int index, CRGB* color);
uint16_t getFrameDelay(int frame); uint16_t getFrameDelay(int frame);
bool _load_from_file(const char* c); bool _load_from_file(const char* c);
public: public:
Animation(const char* filename, Window* win); Animation(const char* filename, Window* win);
void setFgColor(CRGB*); void setFgColor(uint32_t c);
void setBgColor(CRGB* bg_color); void setBgColor(uint32_t c);
bool invert(); bool invert();
void setOffsets(int8_t x, int8_t y); void setOffsets(int8_t x, int8_t y);
void setStartFrame(uint8_t sf); void setStartFrame(uint8_t sf);

View File

@ -8,13 +8,13 @@
class AnimationEffect : public Effect { class AnimationEffect : public Effect {
private: private:
Animation *animation; Animation *animation;
CRGB *bg_color;
uint16_t xOffset; uint16_t xOffset;
uint16_t yOffset; uint16_t yOffset;
public: public:
AnimationEffect(const char* name) : AnimationEffect(name, new CRGB(0x000000), 0, 0) {} AnimationEffect(const char* name) : AnimationEffect(name, 0x000000, 0, 0) {}
AnimationEffect(const char* name, CRGB* background_color) : AnimationEffect(name, background_color, 0, 0) {} AnimationEffect(const char* name, uint32_t bg_color) : AnimationEffect(name, bg_color, 0, 0) {}
AnimationEffect(const char* name, CRGB* bg_color, int x, int y); AnimationEffect(const char* name, uint32_t bg_color, int x, int y);
~AnimationEffect(); ~AnimationEffect();
AnimationEffect* setFgColor(uint32_t c);
void loop(); void loop();
}; };

View File

@ -143,7 +143,7 @@ bool Animation::_load_from_file(const char* filename) {
} }
file.close(); file.close();
LOGln("Animation * Loading completed successfully."); LOGln("Animation * Loading completed successfully.");
return true; return true;
@ -160,12 +160,14 @@ Animation::Animation(const char* filename, Window* win) {
} }
} }
void Animation::setFgColor(CRGB* fg_color) { void Animation::setFgColor(uint32_t c) {
this->fgColor = fg_color; if (this->fgColor) delete this->fgColor;
this->fgColor = new CRGB(c);
} }
void Animation::setBgColor(CRGB* bg_color) { void Animation::setBgColor(uint32_t c) {
this->bgColor = bg_color; if (this->bgColor) delete this->bgColor;
this->bgColor = new CRGB(c);
} }
bool Animation::invert() { bool Animation::invert() {
@ -205,15 +207,27 @@ void Animation::setSingleFrame(uint8_t frame) {
Animation::~Animation() { Animation::~Animation() {
for (int i=0; i<_color_count; i++) delete _colors[i]; for (int i=0; i<_color_count; i++) delete _colors[i];
delete [] _colors; LOGln("Deleting _colors...");
if (fgColor) delete fgColor; if (_colors) delete [] _colors;
delete bgColor; LOGln("Deleting fgColor...");
delete [] _frame_data_lengths;
delete [] _frame_times; 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++) { for (int i=0; i<_frame_count; i++) {
delete [] _frame_data[i]; delete [] _frame_data[i];
} }
delete [] _frame_data; LOGln("Deleting _frame_data...");
if (_frame_data) delete [] _frame_data;
LOGln("Deleteion done.");
} }
void Animation::draw() { void Animation::draw() {
@ -230,7 +244,7 @@ void Animation::drawFrame(uint8_t frame_index) {
CRGB red(0xFF0000); CRGB red(0xFF0000);
CRGB black(0x000000); CRGB black(0x000000);
for (int x=0; x<_window->width; x++) for (int y=0; y<_window->height; y++) { 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; return;
} }

View File

@ -1,16 +1,20 @@
#include "effect_animation.h" #include "effect_animation.h"
#include "functions.h" #include "functions.h"
AnimationEffect::AnimationEffect(const char* name, CRGB* bg, int x, int y) { AnimationEffect::AnimationEffect(const char* name, uint32_t bg, int x, int y) {
this->bg_color = bg;
this->xOffset = x; this->xOffset = x;
this->yOffset = y; this->yOffset = y;
this->animation = new Animation(name, window); this->animation = new Animation(name, window);
this->animation->setBgColor(this->bg_color); this->animation->setBgColor(bg);
this->animation->setOffsets(this->xOffset, this->yOffset); this->animation->setOffsets(this->xOffset, this->yOffset);
} }
AnimationEffect* AnimationEffect::setFgColor(uint32_t c) {
animation->setFgColor(c);
return this;
}
AnimationEffect::~AnimationEffect() { AnimationEffect::~AnimationEffect() {
delete this->animation; delete this->animation;
} }