renderbug-cpp/src/sprites/Blob.h
2023-12-20 10:48:10 +01:00

73 lines
1.9 KiB
C++

#pragma once
#include <Figments.h>
#include <ArduinoLog.h>
class Blob {
uint8_t m_pos;
int8_t m_velocity;
uint8_t m_hue;
int16_t m_brightness;
uint8_t m_saturation;
int8_t m_fadeDir;
public:
Blob()
: m_pos(0),
m_velocity(1),
m_hue(0),
m_brightness(1),
m_saturation(200),
m_fadeDir(1) {}
void setSaturation(uint8_t v) {
m_saturation = v;
}
void setPos(uint16_t p) {
m_pos = p;
m_brightness = p % 120 + 1;
}
void setHue(uint8_t p) {
m_hue = p;
}
void setBrightness(uint8_t p) {
m_brightness = p;
}
void setVelocity(int8_t v) {
m_velocity = v;
}
void update() {
m_pos += m_velocity;
m_hue += 1;
m_brightness += m_fadeDir;
if (m_brightness >= 255 || m_brightness <= 0) {
m_fadeDir *= -1;
}
}
void render(Display* display) const {
const uint8_t width = 25;
auto map = display->coordinateMapping();
// Grab the physical pixel we'll start with
//PhysicalCoordinates startPos = map->virtualToPhysicalCoords({m_pos, m_pos});
//PhysicalCoordinates endPos = map->virtualToPhysicalCoords({m_pos + width, m_pos});
Surface sfc{display, {m_pos, m_pos}, {qadd8(m_pos, width), qadd8(m_pos, width)}};
sfc.paintShader([=](CRGB& pixel, const VirtualCoordinates& coords, const PhysicalCoordinates, const VirtualCoordinates& surfaceCoords) {
uint8_t pixelMod = std::min(surfaceCoords.y, surfaceCoords.x);
pixelMod = surfaceCoords.x;
// Blobs desaturate and dim towards their edges
uint8_t saturation = lerp8by8(0, m_brightness, pixelMod);
uint8_t val = lerp8by8(0, m_brightness, pixelMod);
CHSV blobColor(m_hue, m_saturation, val);
//PhysicalCoordinates pos{startPos.x + (i*m_fadeDir), startPos.y};
pixel += blend(CRGB(blobColor), pixel, 80);
});
}
};