renderbug-cpp/lib/Figments/Surface.h

76 lines
2.2 KiB
C
Raw Normal View History

#pragma once
2021-03-29 08:10:55 +00:00
#include <FastLED.h>
2019-05-10 05:17:29 +00:00
#include "./Geometry.h"
2021-03-29 08:10:55 +00:00
#include <functional>
2019-05-10 05:17:29 +00:00
class Display;
2023-03-03 18:43:51 +00:00
/**
* A high performance and hardware-independent rendering surface which
* represents a full 2d display ranging from (0,0) to (255, 255), that may be
* rotated.
*
* Simple brushes and complex shaders may be applied to a Surface using
* @paintWith and @paintShader. The Surface will only execute each shader or
* brush once for each physical pixel in the display, allowing you to design
* display-independent graphics.
*/
2019-05-10 05:17:29 +00:00
class Surface {
public:
Surface(Display* dpy, const VirtualCoordinates& start, const VirtualCoordinates& end);
Surface(Display* dpy, const VirtualCoordinates& start, const VirtualCoordinates& end, uint8_t rotation);
2019-05-10 05:17:29 +00:00
2023-03-03 18:43:51 +00:00
/**
* Sets the entire surface to one solid color
*/
2021-03-29 08:10:55 +00:00
Surface& operator=(const CRGB& color);
2023-03-03 18:43:51 +00:00
/**
* Adds the given color to every pixel on the surface
*/
2021-03-29 08:10:55 +00:00
Surface& operator+=(const CRGB& color);
2023-03-03 18:43:51 +00:00
/**
* OR operation that applies the given color to every pixel on the surface
*/
template<typename T>
Surface& operator|=(const T& val) {
paintWith([&](CRGB& pixel) {
pixel |= val;
});
return *this;
}
2023-03-03 18:43:51 +00:00
/**
* A lambda function that maps coordinates to colors.
*/
using Shader = std::function<void(CRGB&, const VirtualCoordinates& virtPos, const PhysicalCoordinates& realPos, const VirtualCoordinates& surfacePos)>;
2023-03-03 18:43:51 +00:00
/**
* A simple lambda that is called for every pixel in a surface. Commonly
* used for solid fills; if you want to map pixel coordinates to colors or
* textures, you probably want to use a Shader instead.
*/
using BrushFunc = std::function<void(CRGB&)>;
2019-05-10 05:17:29 +00:00
2023-03-03 18:43:51 +00:00
/**
* Applies the given BrushFunc to every physical pixel in the surface
*/
void paintWith(BrushFunc func);
2023-03-03 18:43:51 +00:00
/**
* Applies the given Shader to every physical pixel in the surface
*/
void paintShader(Shader shader);
2019-05-10 05:17:29 +00:00
const PhysicalCoordinates start;
const PhysicalCoordinates end;
const VirtualCoordinates virtStart;
const VirtualCoordinates virtEnd;
2019-05-10 05:17:29 +00:00
private:
Display* m_display;
uint8_t m_rotation = 0;
2019-05-10 05:17:29 +00:00
};