8#include "TinyGPU/Color/RGB565.h"
14template <
typename RGB_T>
class IFont;
17
18
19
20
21
22
23
24
25
26template <
typename RGB_T =
RGB565>
35 virtual void end() = 0;
38 virtual bool resize(size_t newWidth, size_t newHeight) = 0;
40 virtual void setPixel(size_t x, size_t y, RGB_T color) = 0;
42 virtual RGB_T
getPixel(size_t x, size_t y)
const = 0;
44 virtual void scroll(
int dx,
int dy) = 0;
46 virtual void clear(RGB_T color = RGB_T(0)) = 0;
48 virtual void drawLine(size_t x0, size_t y0, size_t x1, size_t y1,
51 virtual void drawRect(size_t x, size_t y, size_t w, size_t h,
54 virtual void fillRect(size_t x, size_t y, size_t w, size_t h,
57 virtual void drawCircle(size_t x, size_t y, size_t r, RGB_T color) = 0;
59 virtual void fillCircle(size_t x, size_t y, size_t r, RGB_T color) = 0;
70 size_t radius, RGB_T color) {
71 if (w == 0 || h == 0)
return;
72 const size_t maxRadius = (w < h ? w : h) / 2;
73 if (radius > maxRadius) radius = maxRadius;
75 drawRect(x, y, w, h, color);
79 drawLine(x + radius, y, x + w - 1 - radius, y, color);
80 drawLine(x + radius, y + h - 1, x + w - 1 - radius, y + h - 1, color);
81 drawLine(x, y + radius, x, y + h - 1 - radius, color);
82 drawLine(x + w - 1, y + radius, x + w - 1, y + h - 1 - radius, color);
84 plotRoundRectCorner(x + radius, y + radius, radius,
85 RoundRectCorner::kTopLeft, color);
86 plotRoundRectCorner(x + w - 1 - radius, y + radius, radius,
87 RoundRectCorner::kTopRight, color);
88 plotRoundRectCorner(x + w - 1 - radius, y + h - 1 - radius, radius,
89 RoundRectCorner::kBottomRight, color);
90 plotRoundRectCorner(x + radius, y + h - 1 - radius, radius,
91 RoundRectCorner::kBottomLeft, color);
105 size_t radius, RGB_T color) {
106 if (w == 0 || h == 0)
return;
107 const size_t maxRadius = (w < h ? w : h) / 2;
108 if (radius > maxRadius) radius = maxRadius;
110 fillRect(x, y, w, h, color);
114 for (size_t row = 0; row < h; ++row) {
118 }
else if (row >= h - radius) {
119 dy = radius - (h - 1 - row);
124 const double dx = sqrt(
static_cast<
double>(radius) * radius -
125 static_cast<
double>(dy) * dy);
126 inset = radius -
static_cast<size_t>(dx);
129 const size_t rowWidth = (w > 2 * inset) ? (w - 2 * inset) : 0;
131 fillRect(x + inset, y + row, rowWidth, 1, color);
141 virtual void drawArc(size_t cx, size_t cy, size_t r,
float startDeg,
142 float endDeg, RGB_T color, uint8_t thickness = 1) {
144 if (thickness == 0) thickness = 1;
145 const size_t minRadius =
146 (
static_cast<size_t>(thickness) - 1 >= r)
148 : (r - (
static_cast<size_t>(thickness) - 1));
150 constexpr float kPi = 3.14159265358979323846f;
151 float startRad = startDeg * kPi / 180.0f;
152 float endRad = endDeg * kPi / 180.0f;
153 if (endRad < startRad) endRad += 2.0f * kPi;
155 for (size_t radius = minRadius; radius <= r; ++radius) {
156 const float step = 1.0f /
static_cast<
float>(radius);
157 for (
float angle = startRad; angle <= endRad; angle += step) {
159 static_cast<
long>(cx) +
160 static_cast<
long>(lroundf(cosf(angle) *
static_cast<
float>(radius)));
162 static_cast<
long>(cy) +
163 static_cast<
long>(lroundf(sinf(angle) *
static_cast<
float>(radius)));
164 if (px >= 0 && py >= 0 &&
165 contains(
static_cast<size_t>(px),
static_cast<size_t>(py))) {
166 setPixel(
static_cast<size_t>(px),
static_cast<size_t>(py), color);
173 virtual void drawSprite(size_t x, size_t y,
const ISurface<RGB_T>& sprite,
174 RGB_T invisibleColor = RGB_T(0)) = 0;
176 virtual void clearSprite(size_t x, size_t y, ISurface<RGB_T>& sprite,
177 RGB_T clearColor = RGB_T(0)) = 0;
179 virtual void copySprite(size_t x, size_t y,
const ISurface<RGB_T>& sprite) = 0;
185 virtual void drawText(int16_t x, int16_t y,
const char* text,
186 RGB_T foreground, RGB_T background = RGB_T(0),
187 bool opaque =
false, uint8_t scale = 1,
188 uint8_t spacing = 1, uint8_t lineSpacing = 1) = 0;
193 virtual const uint8_t*
data()
const = 0;
237 enum class RoundRectCorner : uint8_t {
248 void plotRoundRectCorner(size_t cx, size_t cy, size_t r,
249 RoundRectCorner corner, RGB_T color) {
250 long offsetX =
static_cast<
long>(r);
252 long decision = 1 - offsetX;
254 while (offsetX >= offsetY) {
255 long px1, py1, px2, py2;
257 case RoundRectCorner::kTopLeft:
258 px1 =
static_cast<
long>(cx) - offsetX;
259 py1 =
static_cast<
long>(cy) - offsetY;
260 px2 =
static_cast<
long>(cx) - offsetY;
261 py2 =
static_cast<
long>(cy) - offsetX;
263 case RoundRectCorner::kTopRight:
264 px1 =
static_cast<
long>(cx) + offsetY;
265 py1 =
static_cast<
long>(cy) - offsetX;
266 px2 =
static_cast<
long>(cx) + offsetX;
267 py2 =
static_cast<
long>(cy) - offsetY;
269 case RoundRectCorner::kBottomRight:
270 px1 =
static_cast<
long>(cx) + offsetX;
271 py1 =
static_cast<
long>(cy) + offsetY;
272 px2 =
static_cast<
long>(cx) + offsetY;
273 py2 =
static_cast<
long>(cy) + offsetX;
275 case RoundRectCorner::kBottomLeft:
277 px1 =
static_cast<
long>(cx) - offsetY;
278 py1 =
static_cast<
long>(cy) + offsetX;
279 px2 =
static_cast<
long>(cx) - offsetX;
280 py2 =
static_cast<
long>(cy) + offsetY;
284 if (px1 >= 0 && py1 >= 0 &&
285 contains(
static_cast<size_t>(px1),
static_cast<size_t>(py1))) {
286 setPixel(
static_cast<size_t>(px1),
static_cast<size_t>(py1), color);
288 if (px2 >= 0 && py2 >= 0 &&
289 contains(
static_cast<size_t>(px2),
static_cast<size_t>(py2))) {
290 setPixel(
static_cast<size_t>(px2),
static_cast<size_t>(py2), color);
295 decision += 2 * offsetY + 1;
298 decision += 2 * (offsetY - offsetX) + 1;
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
virtual ~ISurface()=default
Destroys the framebuffer interface.
virtual void fillRect(size_t x, size_t y, size_t w, size_t h, RGB_T color)=0
Fills a rectangle.
virtual size_t height() const =0
Returns the framebuffer height in pixels.
virtual void fillRoundRect(size_t x, size_t y, size_t w, size_t h, size_t radius, RGB_T color)
Definition: ISurface.h:104
virtual void drawRoundRect(size_t x, size_t y, size_t w, size_t h, size_t radius, RGB_T color)
Definition: ISurface.h:69
virtual void drawArc(size_t cx, size_t cy, size_t r, float startDeg, float endDeg, RGB_T color, uint8_t thickness=1)
Definition: ISurface.h:141
virtual void drawLine(size_t x0, size_t y0, size_t x1, size_t y1, RGB_T color)=0
Draws a line between two points.
virtual void copySprite(size_t x, size_t y, const ISurface< RGB_T > &sprite)=0
Copies pixels from the framebuffer into a sprite.
virtual void drawRect(size_t x, size_t y, size_t w, size_t h, RGB_T color)=0
Draws a rectangle outline.
virtual void fillCircle(size_t x, size_t y, size_t r, RGB_T color)=0
Fills a circle.
virtual bool contains(size_t x, size_t y)=0
Checks if the given coordinates are within the surface bounds.
virtual bool resize(size_t newWidth, size_t newHeight)=0
Resizes the framebuffer surface.
virtual bool isClipVisible(size_t x, size_t y) const
Definition: ISurface.h:228
virtual void clearSprite(size_t x, size_t y, ISurface< RGB_T > &sprite, RGB_T clearColor=RGB_T(0))=0
Clears the area covered by a sprite.
virtual void popClipRect()
Definition: ISurface.h:221
virtual void setPixel(size_t x, size_t y, RGB_T color)=0
Sets a pixel at the given position.
virtual void scroll(int dx, int dy)=0
Scrolls the framebuffer content by the specified offsets.
virtual void end()=0
Closes the framebuffer and releases resources.
virtual size_t size() const =0
Provides the total data size in bytes.
virtual void drawText(int16_t x, int16_t y, const char *text, RGB_T foreground, RGB_T background=RGB_T(0), bool opaque=false, uint8_t scale=1, uint8_t spacing=1, uint8_t lineSpacing=1)=0
Draws UTF-8 text using the configured font.
virtual void pushClipRect(size_t x, size_t y, size_t w, size_t h)
Definition: ISurface.h:212
virtual void clear(RGB_T color=RGB_T(0))=0
Clears the framebuffer with a single color.
virtual size_t width() const =0
Returns the framebuffer width in pixels.
virtual RGB_T getPixel(size_t x, size_t y) const =0
Returns the pixel at the given position.
virtual IFont< RGB_T > & font()=0
Returns the currently set font for text rendering.
virtual bool begin()=0
Initializes the framebuffer surface.
virtual void drawCircle(size_t x, size_t y, size_t r, RGB_T color)=0
Draws a circle outline.
virtual void drawSprite(size_t x, size_t y, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T(0))=0
Draws a sprite using an optional transparent color.
virtual const uint8_t * data() const =0
Provides access to the framebuffer as a byte buffer.
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19