TinyGPU
Loading...
Searching...
No Matches
SurfaceWithExternalBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5#include <assert.h>
6#include <algorithm>
7#include <cstddef>
8
9#include "TinyGPU/Font/Font5x7.h"
10#include "TinyGPU/Font/IFont.h"
11#include "TinyGPU/Surface/ISurface.h"
12#include "TinyGPU/Font/LinePrinter.h"
13#include "TinyGPU/Color/RGB565.h"
14#include "TinyGPU/Color/RGB666.h"
15#include "TinyGPU/Color/RGB888.h"
16#include "TinyGPU/Surface/SurfaceBase.h"
17#include "TinyGPUConfig.h"
18#include "TinyGPU/ThreeD/Vector.h"
19
20namespace tinygpu {
21
22/**
23 * @brief In-memory bitmap surface with basic 2D drawing and text rendering.
24 *
25 * @tparam RGB_T The pixel color type. Can be RGB565, RGB666, RGB888, etc.
26 *
27 * The class stores pixel data in a contiguous buffer and provides helpers for
28 * drawing lines, rectangles, circles, sprites, and UTF-8 text.
29 *
30 * Example usage:
31 * Surface<RGB565> surf565(240, 320);
32 * Surface<RGB666> surf666(240, 320);
33 * Surface<RGB888> surf888(240, 320);
34 */
35
36template <typename RGB_T = RGB565>
38 static_assert(!std::is_same<RGB_T, bool>::value,
39 "Use SurfaceMonochrome for monochrome surfaces");
40
41 public:
42 using SurfaceBase<RGB_T>::width_;
43 using SurfaceBase<RGB_T>::height_;
44 using SurfaceBase<RGB_T>::font_;
45 using SurfaceBase<RGB_T>::linePrinter_;
46 /// @brief Default constructor. Creates an empty surface.
48
49 /// @brief Constructs a surface with the given width, height, and font.
50 /// @param w Surface width in pixels
51 /// @param h Surface height in pixels
52 /// @param font Reference to the font used for text rendering
53 SurfaceWithExternalBuffer(size_t w, size_t h, IFont<RGB_T>& font = FontRGB565)
54 : SurfaceBase<RGB_T>(w, h, font) {}
55
56 /// @brief Sets the pixel at (x, y) to the specified color.
57 /// @param x X coordinate
58 /// @param y Y coordinate
59 /// @param color Pixel color value
60 void setPixel(size_t x, size_t y, RGB_T color) override {
61 if (buffer == nullptr) return;
62 if (x >= width_ || y >= height_) return; // Bounds check
63 buffer[(y * width_) + x] = color;
64 }
65
66 /// @brief Gets the color of the pixel at (x, y).
67 /// @param x X coordinate
68 /// @param y Y coordinate
69 /// @return Pixel color value
70 RGB_T getPixel(size_t x, size_t y) const override {
71 if (buffer == nullptr) return RGB_T();
72 if (x >= width_ || y >= height_) return RGB_T(); // Bounds check
73 return buffer[(y * width_) + x];
74 }
75
76 /// @brief Resizes the internal pixel buffer to the specified width and
77 /// height.
78 /// @param w New width
79 /// @param h New height
80 bool resizeBuffer(size_t w, size_t h) override {
81 buffer_size = w * h * sizeof(RGB_T);
82 assert(buffer_size <= buffer_capacity);
83 return true;
84 }
85
86 /// @brief Returns a pointer to the raw data as bytes.
87 /// @return Pointer to the buffer as uint8_t
88 const uint8_t* data() const override {
89 return reinterpret_cast<const uint8_t*>(buffer);
90 }
91
92 /// @brief Mutable counterpart of data() - a pointer to the raw pixel
93 /// buffer as bytes, for callers that want to fill it directly (e.g. a
94 /// codec's own RGB565/RGB666/RGB888 conversion output) instead of
95 /// going through setPixel() one pixel at a time. Same bytes data()
96 /// exposes read-only; writing size() bytes through this pointer is
97 /// exactly equivalent to that many setPixel() calls.
98 uint8_t* data() { return reinterpret_cast<uint8_t*>(buffer); }
99
100 /// @brief Mutable pointer to the pixel buffer, typed as RGB_T rather
101 /// than raw bytes - the same buffer data() above exposes, just typed
102 /// for direct pixel-at-a-time indexing without a cast. RGB_T's binary
103 /// layout matches a plain packed integer of the same width for every
104 /// format this library ships (e.g. RGB565 <-> uint16_t), so a caller
105 /// producing raw packed values can reinterpret_cast this pointer to
106 /// that type if it doesn't want to construct RGB_T instances directly.
107 RGB_T* pixels() { return buffer; }
108
109 /// @brief Number of RGB_T entries pixels()/data() has room for
110 /// (width() * height()) - distinct from size(), which is that count
111 /// converted to bytes.
112 size_t pixelCount() const { return buffer_size / sizeof(RGB_T); }
113
114 /// @brief Returns the size of the buffer in bytes.
115 /// @return Buffer size in bytes
116 size_t size() const override { return buffer_size; }
117
118 /// Defines the external buffer
119 void setExternalBuffer(void* externalBuffer, size_t capacityBytes) {
120 buffer = static_cast<RGB_T*>(externalBuffer) ;
121 buffer_capacity = capacityBytes;
122 }
123
124 /// Returns the capacity of the external buffer in bytes.
125 size_t capacity() const { return buffer_capacity; }
126
127 protected:
128 RGB_T* buffer = nullptr;
129 size_t buffer_size = 0; // in bytes
130 size_t buffer_capacity = 0; // in bytes
131};
132
133
134} // namespace tinygpu
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Base class for 2D surfaces with drawing and text rendering support.
Definition: SurfaceBase.h:25
In-memory bitmap surface with basic 2D drawing and text rendering.
Definition: SurfaceWithExternalBuffer.h:37
void setExternalBuffer(void *externalBuffer, size_t capacityBytes)
Defines the external buffer.
Definition: SurfaceWithExternalBuffer.h:119
RGB_T * pixels()
Mutable pointer to the pixel buffer, typed as RGB_T rather than raw bytes - the same buffer data() ab...
Definition: SurfaceWithExternalBuffer.h:107
size_t pixelCount() const
Number of RGB_T entries pixels()/data() has room for (width() * height()) - distinct from size(),...
Definition: SurfaceWithExternalBuffer.h:112
size_t capacity() const
Returns the capacity of the external buffer in bytes.
Definition: SurfaceWithExternalBuffer.h:125
size_t buffer_size
Definition: SurfaceWithExternalBuffer.h:129
SurfaceWithExternalBuffer()=default
Default constructor. Creates an empty surface.
const uint8_t * data() const override
Returns a pointer to the raw data as bytes.
Definition: SurfaceWithExternalBuffer.h:88
uint8_t * data()
Mutable counterpart of data() - a pointer to the raw pixel buffer as bytes, for callers that want to ...
Definition: SurfaceWithExternalBuffer.h:98
RGB_T * buffer
Definition: SurfaceWithExternalBuffer.h:128
SurfaceWithExternalBuffer(size_t w, size_t h, IFont< RGB_T > &font=FontRGB565)
Constructs a surface with the given width, height, and font.
Definition: SurfaceWithExternalBuffer.h:53
void setPixel(size_t x, size_t y, RGB_T color) override
Sets the pixel at (x, y) to the specified color.
Definition: SurfaceWithExternalBuffer.h:60
size_t size() const override
Returns the size of the buffer in bytes.
Definition: SurfaceWithExternalBuffer.h:116
bool resizeBuffer(size_t w, size_t h) override
Resizes the internal pixel buffer to the specified width and height.
Definition: SurfaceWithExternalBuffer.h:80
size_t buffer_capacity
Definition: SurfaceWithExternalBuffer.h:130
RGB_T getPixel(size_t x, size_t y) const override
Gets the color of the pixel at (x, y).
Definition: SurfaceWithExternalBuffer.h:70
Definition: DSIBusESP32.h:19