TinyGPU
Loading...
Searching...
No Matches
Surface.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <stddef.h>
5#include <stdint.h>
6
7#include <algorithm>
8#include <cstddef>
9
10#include "BGR565.h"
11#include "BitmapFont.h"
12#include "IFont.h"
13#include "ISurface.h"
14#include "LinePrinter.h"
15#include "RGB565.h"
16#include "RGB666.h"
17#include "RGB888.h"
18#include "SurfaceBase.h"
19#include "TinyGPUConfig.h"
20#include "TinyGPU/Vector.h"
21
22namespace tinygpu {
23
24/**
25 * @brief In-memory bitmap surface with basic 2D drawing and text rendering.
26 *
27 * @tparam RGB_T The pixel color type. Can be RGB565, RGB666, RGB888, etc.
28 *
29 * The class stores pixel data in a contiguous buffer and provides helpers for
30 * drawing lines, rectangles, circles, sprites, and UTF-8 text.
31 *
32 * Example usage:
33 * Surface<RGB565> surf565(240, 320);
34 * Surface<RGB666> surf666(240, 320);
35 * Surface<RGB888> surf888(240, 320);
36 */
37
38template <typename RGB_T = RGB565>
39class Surface : public SurfaceBase<RGB_T> {
40 static_assert(!std::is_same<RGB_T, bool>::value,
41 "Use SurfaceMonochrome for monochrome surfaces");
42
43 public:
44 using SurfaceBase<RGB_T>::width_;
45 using SurfaceBase<RGB_T>::height_;
46 using SurfaceBase<RGB_T>::font_;
47 using SurfaceBase<RGB_T>::linePrinter_;
48 /// @brief Default constructor. Creates an empty surface.
49 Surface() = default;
50
51 /// @brief Constructs a surface with the given width, height, and font.
52 /// @param w Surface width in pixels
53 /// @param h Surface height in pixels
54 /// @param font Reference to the font used for text rendering
55 Surface(size_t w, size_t h, IFont<RGB_T>& font)
56 : SurfaceBase<RGB_T>(w, h, font) {}
57
58 /// @brief Sets the pixel at (x, y) to the specified color.
59 /// @param x X coordinate
60 /// @param y Y coordinate
61 /// @param color Pixel color value
62 void setPixel(size_t x, size_t y, RGB_T color) override {
63 if (buffer.empty()) return;
64 buffer[(y * width_) + x] = color;
65 }
66
67 /// @brief Gets the color of the pixel at (x, y).
68 /// @param x X coordinate
69 /// @param y Y coordinate
70 /// @return Pixel color value
71 RGB_T getPixel(size_t x, size_t y) const override {
72 if (buffer.empty()) return RGB_T();
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.resize(w * h);
82 return buffer.size() == (w * h) || (w * h == 0 && buffer.empty());
83 }
84
85 /// @brief Returns a pointer to the raw data as bytes.
86 /// @return Pointer to the buffer as uint8_t
87 const uint8_t* data() const override {
88 return reinterpret_cast<const uint8_t*>(buffer.data());
89 }
90
91 /// @brief Returns the size of the buffer in bytes.
92 /// @return Buffer size in bytes
93 size_t size() const override { return buffer.size() * RGB_T::size() / 8; }
94
95 protected:
97};
98
99/// @brief Alias for a sprite surface with the same pixel format as the main surface.
100template <typename RGB_T = RGB565>
101using Sprite = Surface<RGB_T>;
102
103
104} // namespace tinygpu
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
RGB color stored in 16-bit RGB565 format.
Definition: RGB565.h:13
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: Surface.h:39
Surface()=default
Default constructor. Creates an empty surface.
Surface(size_t w, size_t h, IFont< RGB_T > &font)
Constructs a surface with the given width, height, and font.
Definition: Surface.h:55
const uint8_t * data() const override
Returns a pointer to the raw data as bytes.
Definition: Surface.h:87
void setPixel(size_t x, size_t y, RGB_T color) override
Sets the pixel at (x, y) to the specified color.
Definition: Surface.h:62
size_t size() const override
Returns the size of the buffer in bytes.
Definition: Surface.h:93
bool resizeBuffer(size_t w, size_t h) override
Resizes the internal pixel buffer to the specified width and height.
Definition: Surface.h:80
RGB_T getPixel(size_t x, size_t y) const override
Gets the color of the pixel at (x, y).
Definition: Surface.h:71
Vector< RGB_T > buffer
Definition: Surface.h:96
Definition: AVIWriter.h:9