renderbug-cpp/lib/Figments/Surface.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2019-05-10 05:17:29 +00:00
#include "./Surface.h"
#include "./Display.h"
2021-03-29 08:10:55 +00:00
#include <ArduinoLog.h>
#include "Perfcounter.h"
2019-05-10 05:17:29 +00:00
Surface::Surface(Display* dpy, const VirtualCoordinates& start, const VirtualCoordinates& end)
: start(dpy->coordinateMapping()->virtualToPhysicalCoords(start)),
end(dpy->coordinateMapping()->virtualToPhysicalCoords(end)),
virtStart(start),
virtEnd(end),
2019-05-10 05:17:29 +00:00
m_display(dpy)
{
//assert(start.x <= end.x);
//assert(start.y <= end.y);
}
Surface::Surface(Display* dpy, const VirtualCoordinates& start, const VirtualCoordinates& end, uint8_t rotation)
: start(dpy->coordinateMapping()->virtualToPhysicalCoords(start)),
end(dpy->coordinateMapping()->virtualToPhysicalCoords(end)),
virtStart(start),
virtEnd(end),
m_display(dpy),
m_rotation(rotation)
{
//assert(start.x <= end.x);
//assert(start.y <= end.y);
2019-05-10 05:17:29 +00:00
}
Surface&
2021-03-29 08:10:55 +00:00
Surface::operator=(const CRGB& color)
2019-05-10 05:17:29 +00:00
{
2021-03-29 08:10:55 +00:00
paintWith([&](CRGB& pixel) {
2019-05-10 05:17:29 +00:00
pixel = color;
});
return *this;
}
Surface&
2021-03-29 08:10:55 +00:00
Surface::operator+=(const CRGB& color)
2019-05-10 05:17:29 +00:00
{
2021-03-29 08:10:55 +00:00
paintWith([&](CRGB& pixel) {
2019-05-10 05:17:29 +00:00
pixel += color;
});
return *this;
}
void
2021-03-29 08:10:55 +00:00
Surface::paintWith(std::function<void(CRGB&)> func)
2019-05-10 05:17:29 +00:00
{
paintShader([=](CRGB& pixel, const VirtualCoordinates&, const PhysicalCoordinates&, const VirtualCoordinates&){ func(pixel); });
}
void
Surface::paintShader(Surface::Shader shader)
{
PerfCounter _("paintShader");
const uint16_t width = end.x - start.x + 1;
const uint16_t height = end.y - start.y + 1;
const uint8_t xMod = 255 / width;
const uint8_t yMod = 255 / height;
for(auto x = 0; x < width; x++) {
for(auto y = 0; y < height; y++) {
PhysicalCoordinates coords{x + start.x, y + start.y};
VirtualCoordinates virtCoords{m_display->coordinateMapping()->physicalToVirtualCoords(coords)};
VirtualCoordinates surfaceCoords{xMod * x, yMod * y};
//Log.notice("width=%d height=%d vx=%d vy=%d sx=%d sy=%d x=%d y=%d px=%d py=%d", width, height, start.x, start.y, x, y, coords.x, coords.y);
// 256 = 1.0
// 128 = 0.0
// 0 = 1.0
shader(m_display->pixelAt(coords), virtCoords, coords, surfaceCoords);
2019-05-10 05:17:29 +00:00
}
}
}