TinyGPU
Loading...
Searching...
No Matches
CartesianView.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5
6#include "ISurface.h"
7#include "IFont.h"
8
9namespace tinygpu {
10
11/**
12 * @brief Bottom-left Cartesian coordinate view over an existing Surface.
13 *
14 * CartesianView wraps a Surface and remaps the logical y-axis so that
15 * `(0, 0)` refers to the lower-left corner while preserving the wrapped
16 * surface storage and export format.
17 */
18
19template <typename RGB_T = RGB565>
20class CartesianView : public ISurface<RGB_T> {
21 public:
22 /// Creates a Cartesian view over the provided surface.
23 explicit CartesianView(ISurface<RGB_T>& surface) : surface_(surface) {}
24
25 /// Resizes the wrapped surface.
26 void resize(size_t newWidth, size_t newHeight) override {
27 surface_.resize(newWidth, newHeight);
28 }
29
30 /// Sets a pixel using Cartesian bottom-left coordinates.
31 void setPixel(size_t x, size_t y, RGB_T color) override {
32 surface_.setPixel(x, mappedY(y), color);
33 }
34
35 /// Returns a pixel using Cartesian bottom-left coordinates.
36 RGB_T getPixel(size_t x, size_t y) const override {
37 return surface_.getPixel(x, mappedY(y));
38 }
39
40 /// Clears the wrapped surface.
41 void clear(RGB_T color = RGB_T(0)) override { surface_.clear(color); }
42
43 /// Draws a line using Cartesian bottom-left coordinates.
44 void drawLine(size_t x0, size_t y0, size_t x1, size_t y1,
45 RGB_T color) override {
46 surface_.drawLine(x0, mappedY(y0), x1, mappedY(y1), color);
47 }
48
49 /// Draws a rectangle outline using Cartesian bottom-left coordinates.
50 void drawRect(size_t x, size_t y, size_t w, size_t h, RGB_T color) override {
51 surface_.drawRect(x, mappedTopOfBox(y, h), w, h, color);
52 }
53
54 /// Fills a rectangle using Cartesian bottom-left coordinates.
55 void fillRect(size_t x, size_t y, size_t w, size_t h, RGB_T color) override {
56 surface_.fillRect(x, mappedTopOfBox(y, h), w, h, color);
57 }
58
59 /// Draws a circle outline using Cartesian bottom-left coordinates.
60 void drawCircle(size_t x, size_t y, size_t r, RGB_T color) override {
61 surface_.drawCircle(x, mappedY(y), r, color);
62 }
63
64 /// Fills a circle using Cartesian bottom-left coordinates.
65 void fillCircle(size_t x, size_t y, size_t r, RGB_T color) override {
66 surface_.fillCircle(x, mappedY(y), r, color);
67 }
68
69 /// Draws a sprite with its lower-left corner anchored at the given position.
70 void drawSprite(size_t x, size_t y, const ISurface<RGB_T>& sprite,
71 RGB_T invisibleColor = RGB_T(0)) override {
72 surface_.drawSprite(x, mappedTopOfBox(y, sprite.height()), sprite,
73 invisibleColor);
74 }
75
76 /// Clears the area occupied by a sprite anchored at its lower-left corner.
77 void clearSprite(size_t x, size_t y, ISurface<RGB_T>& sprite,
78 RGB_T clearColor = RGB_T(0)) override {
79 surface_.clearSprite(x, mappedTopOfBox(y, sprite.height()), sprite,
80 clearColor);
81 }
82
83 /// Copies pixels from the view into a sprite using Cartesian coordinates.
84 void copySprite(size_t x, size_t y, const ISurface<RGB_T>& sprite) override {
85 surface_.copySprite(x, mappedTopOfBox(y, sprite.height()), sprite);
86 }
87
88 /// Returns the wrapped surface width.
89 size_t width() const override { return surface_.width(); }
90
91 /// Returns the wrapped surface height.
92 size_t height() const override { return surface_.height(); }
93
94 /// Draws text with the given lower-left text box origin.
95 void drawText(int16_t x, int16_t y, const char* text, RGB565 foreground,
96 RGB565 background = RGB565(0), bool opaque = false,
97 uint8_t scale = 1, uint8_t spacing = 1,
98 uint8_t lineSpacing = 1) override {
99 const size_t textHeight =
100 surface_.font().measureTextHeight(text, scale, lineSpacing);
101 surface_.drawText(x,
102 static_cast<int16_t>(
103 mappedTopOfBox(static_cast<size_t>(y), textHeight)),
104 text, foreground, background, opaque, scale, spacing,
105 lineSpacing);
106 }
107
108 /// Returns the wrapped byte buffer.
109 const uint8_t* data() const override { return surface_.data(); }
110
111 /// Returns the wrapped buffer size in bytes.
112 size_t size() const override { return surface_.size(); }
113
114 /// Returns the wrapped surface as a reference.
115 ISurface<RGB_T>& surface() { return surface_; }
116
117 /// Returns the wrapped surface as a const reference.
118 const ISurface<RGB_T>& surface() const { return surface_; }
119
120 /// Returns the font of the wrapped surface.
121 IFont<RGB_T>& font() override { return surface_.font(); }
122
123 protected:
124 ISurface<RGB_T>& surface_;
125
126 /// Maps a logical Cartesian Y coordinate (bottom-left origin) to the wrapped
127 /// surface's Y (top-left origin).
128 size_t mappedY(size_t y) const { return (height() - 1) - y; }
129
130 /**
131 * @brief Maps the logical bottom-left Y and object height to the top Y
132 * coordinate for box-based operations.
133 *
134 * For rectangles, sprites, and text, this computes the top Y (in Cartesian
135 * coordinates), then maps it to the wrapped surface's Y. Returns 0 if the
136 * computed top Y is out of bounds.
137 *
138 * @param y Logical bottom Y (Cartesian, bottom-left origin)
139 * @param objectHeight Height of the object (rectangle, sprite, or text)
140 * @return size_t Mapped Y coordinate for the top of the box in the wrapped
141 * surface
142 */
144 if (height() == 0 || objectHeight == 0) {
145 return 0;
146 }
147
148 const uint64_t topY = static_cast<uint64_t>(y) + objectHeight - 1U;
149 if (topY >= height()) {
150 return 0;
151 }
152 return mappedY(static_cast<size_t>(topY));
153 }
154};
155
156} // namespace tinygpu
Bottom-left Cartesian coordinate view over an existing Surface.
Definition: CartesianView.h:20
void drawLine(size_t x0, size_t y0, size_t x1, size_t y1, RGB_T color) override
Draws a line using Cartesian bottom-left coordinates.
Definition: CartesianView.h:44
void fillCircle(size_t x, size_t y, size_t r, RGB_T color) override
Fills a circle using Cartesian bottom-left coordinates.
Definition: CartesianView.h:65
void copySprite(size_t x, size_t y, const ISurface< RGB_T > &sprite) override
Copies pixels from the view into a sprite using Cartesian coordinates.
Definition: CartesianView.h:84
const ISurface< RGB_T > & surface() const
Returns the wrapped surface as a const reference.
Definition: CartesianView.h:118
ISurface< RGB_T > & surface_
Definition: CartesianView.h:124
CartesianView(ISurface< RGB_T > &surface)
Creates a Cartesian view over the provided surface.
Definition: CartesianView.h:23
void resize(size_t newWidth, size_t newHeight) override
Resizes the wrapped surface.
Definition: CartesianView.h:26
size_t height() const override
Returns the wrapped surface height.
Definition: CartesianView.h:92
void drawText(int16_t x, int16_t y, const char *text, RGB565 foreground, RGB565 background=RGB565(0), bool opaque=false, uint8_t scale=1, uint8_t spacing=1, uint8_t lineSpacing=1) override
Draws text with the given lower-left text box origin.
Definition: CartesianView.h:95
ISurface< RGB_T > & surface()
Returns the wrapped surface as a reference.
Definition: CartesianView.h:115
const uint8_t * data() const override
Returns the wrapped byte buffer.
Definition: CartesianView.h:109
size_t mappedTopOfBox(size_t y, size_t objectHeight) const
Maps the logical bottom-left Y and object height to the top Y coordinate for box-based operations.
Definition: CartesianView.h:143
size_t mappedY(size_t y) const
Definition: CartesianView.h:128
IFont< RGB_T > & font() override
Returns the font of the wrapped surface.
Definition: CartesianView.h:121
void fillRect(size_t x, size_t y, size_t w, size_t h, RGB_T color) override
Fills a rectangle using Cartesian bottom-left coordinates.
Definition: CartesianView.h:55
void drawRect(size_t x, size_t y, size_t w, size_t h, RGB_T color) override
Draws a rectangle outline using Cartesian bottom-left coordinates.
Definition: CartesianView.h:50
void drawSprite(size_t x, size_t y, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T(0)) override
Draws a sprite with its lower-left corner anchored at the given position.
Definition: CartesianView.h:70
size_t width() const override
Returns the wrapped surface width.
Definition: CartesianView.h:89
void setPixel(size_t x, size_t y, RGB_T color) override
Sets a pixel using Cartesian bottom-left coordinates.
Definition: CartesianView.h:31
size_t size() const override
Returns the wrapped buffer size in bytes.
Definition: CartesianView.h:112
void clearSprite(size_t x, size_t y, ISurface< RGB_T > &sprite, RGB_T clearColor=RGB_T(0)) override
Clears the area occupied by a sprite anchored at its lower-left corner.
Definition: CartesianView.h:77
RGB_T getPixel(size_t x, size_t y) const override
Returns a pixel using Cartesian bottom-left coordinates.
Definition: CartesianView.h:36
void clear(RGB_T color=RGB_T(0)) override
Clears the wrapped surface.
Definition: CartesianView.h:41
void drawCircle(size_t x, size_t y, size_t r, RGB_T color) override
Draws a circle outline using Cartesian bottom-left coordinates.
Definition: CartesianView.h:60
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
RGB color stored in 16-bit RGB565 format.
Definition: RGB565.h:13
RGB565(uint16_t packed)
Creates a color from a packed RGB565 value.
Definition: RGB565.h:23
Definition: AVIWriter.h:9