pitrix/src/effect_fire.cpp

56 lines
1.8 KiB
C++

#include "effect_fire.h"
#include "my_color_palettes.h"
#include "config.h"
#include "my_fastled.h"
#include "functions.h"
void FireEffect::start() {
this->data = new uint8_t[this->window->width * this->window->height];
for (int i=0; i<(this->window->width * this->window->height); i++) this->data[i]=0;
for (int i=0; i<this->window->width; i++) this->data[i]=this->spark_temp();
}
void FireEffect::stop() {
delete [] this->data;
}
void FireEffect::loop() {
cooldown();
spark();
propagate();
draw();
}
void FireEffect::cooldown() {
for(int i=0; i<(this->window->width * this->window->height); i++) this->data[i] = scale8(this->data[i], EFFECT_FIRE_COOLDOWN); // 240 or something
}
void FireEffect::spark() {
for(int x=0; x<this->window->width; x++) if (random8(EFFECT_FIRE_SPARK_CHANCE)==0) this->data[x] = this->spark_temp();
}
inline uint8_t FireEffect::spark_temp() {
return random8(180, 255);
}
void FireEffect::propagate() {
for (int y=1; y<this->window->height; y++) for (int x=0; x<this->window->width; x++) {
int index = y*this->window->width + x;
uint8_t below_index = index - this->window->width;
if (x==0) {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index+1], 64);
} else if (x==this->window->width-1) {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index-1], 64);
} else {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index-1], 32) + scale8(this->data[below_index+1], 32);
}
}
}
void FireEffect::draw() {
for (int y=1; y<this->window->width; y++) for (int x=0; x<this->window->width; x++) {
CRGB color = ColorFromPalette((CRGBPalette16) palette_fire, this->data[y*this->window->width + x]);
this->window->setPixel(x, this->window->height - 1 - y, (CRGB *)&color);
}
}