TinyGPU
Loading...
Searching...
No Matches
SurfaceMonochrome.h
Go to the documentation of this file.
1#pragma once
2#include <stddef.h>
3#include <stdint.h>
4
5#include "IFont.h"
6#include "SurfaceBase.h"
8#include "TinyGPU/Vector.h"
9
10namespace tinygpu {
11
12/**
13 * @brief In-memory 1-bit (monochrome) bitmap surface with 2D drawing and text rendering.
14 *
15 * SurfaceMonochrome stores pixels in a bit-packed buffer for efficient memory usage.
16 * It provides the same drawing and text API as color surfaces, but only supports two pixel values (on/off).
17 *
18 * This class is ideal for displays or images where only black/white (or on/off) is needed.
19 */
20class SurfaceMonochrome : public SurfaceBase<bool> {
21 public:
22 using SurfaceBase<bool>::width_;
23 using SurfaceBase<bool>::height_;
24 using SurfaceBase<bool>::font_;
25 using SurfaceBase<bool>::linePrinter_;
26 /// @brief Default constructor. Creates an empty monochrome surface.
27 SurfaceMonochrome() = default;
28
29 /// @brief Constructs a monochrome surface with the given width, height, and font.
30 /// @param w Surface width in pixels
31 /// @param h Surface height in pixels
32 /// @param font Reference to the font used for text rendering
33 SurfaceMonochrome(size_t w, size_t h, IFont<bool>& font)
34 : SurfaceBase<bool>(w, h, font) {}
35
36 /// @brief Sets the pixel at (x, y) to the specified color (on/off).
37 /// @param x X coordinate
38 /// @param y Y coordinate
39 /// @param color Pixel value (true=on, false=off)
40 void setPixel(size_t x, size_t y, bool color) override {
41 if (buffer.empty()) return;
42 size_t idx = y * width_ + x;
43 size_t byte = idx / 8;
44 size_t bit = idx % 8;
45 if (color)
46 buffer[byte] |= (1 << bit);
47 else
48 buffer[byte] &= ~(1 << bit);
49 }
50
51 /// @brief Gets the value of the pixel at (x, y).
52 /// @param x X coordinate
53 /// @param y Y coordinate
54 /// @return Pixel value (true=on, false=off)
55 bool getPixel(size_t x, size_t y) const override {
56 if (buffer.empty()) return false;
57
58 size_t idx = y * width_ + x;
59 size_t byte = idx / 8;
60 size_t bit = idx % 8;
61 return (buffer[byte] >> bit) & 1;
62 }
63
64 /// @brief Resizes the internal bit-packed buffer to the specified width and height.
65 /// @param w New width
66 /// @param h New height
67 bool resizeBuffer(size_t w, size_t h) override {
68 size_t bytes = (w * h + 7) / 8;
69 buffer.resize(bytes);
70 return buffer.size() == bytes || (bytes == 0 && buffer.empty());
71 }
72
73 /// @brief Returns a pointer to the raw bit-packed data as bytes.
74 /// @return Pointer to the buffer as uint8_t
75 const uint8_t* data() const override { return buffer.data(); }
76
77 /// @brief Returns the size of the buffer in bytes.
78 /// @return Buffer size in bytes
79 size_t size() const override { return buffer.size(); }
80
81 protected:
83};
84
85/// @brief Alias for a monochrome sprite surface.
86using SpriteMonochrome = SurfaceMonochrome;
87
88} // namespace tinygpu
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
In-memory 1-bit (monochrome) bitmap surface with 2D drawing and text rendering.
Definition: SurfaceMonochrome.h:20
bool getPixel(size_t x, size_t y) const override
Gets the value of the pixel at (x, y).
Definition: SurfaceMonochrome.h:55
SurfaceMonochrome(size_t w, size_t h, IFont< bool > &font)
Constructs a monochrome surface with the given width, height, and font.
Definition: SurfaceMonochrome.h:33
SurfaceMonochrome()=default
Default constructor. Creates an empty monochrome surface.
const uint8_t * data() const override
Returns a pointer to the raw bit-packed data as bytes.
Definition: SurfaceMonochrome.h:75
size_t size() const override
Returns the size of the buffer in bytes.
Definition: SurfaceMonochrome.h:79
bool resizeBuffer(size_t w, size_t h) override
Resizes the internal bit-packed buffer to the specified width and height.
Definition: SurfaceMonochrome.h:67
Vector< uint8_t > buffer
Definition: SurfaceMonochrome.h:82
void setPixel(size_t x, size_t y, bool color) override
Sets the pixel at (x, y) to the specified color (on/off).
Definition: SurfaceMonochrome.h:40
Definition: AVIWriter.h:9