The Canvas class provides low-level access to the pixel buffer used for rendering. It abstracts the underlying pixel buffer and provides safe, efficient pixel manipulation methods.
All pixels are stored in 32-bit RGBA format (0xAABBGGRR) where:
- AA = Alpha (transparency)
- BB = Blue component
- GG = Green component
- RR = Red component
Constructor
Canvas()
Canvas(uint32_t* buffer, int width, int height)
Creates a canvas that operates on the provided pixel buffer. The buffer must remain valid for the lifetime of the canvas.
Pointer to 32-bit RGBA pixel array
The pixel buffer must be at least (width * height * 4) bytes. The canvas does not take ownership of the buffer.
Methods
clear()
void clear(uint32_t color)
Fills all pixels in the canvas with the specified color value. This is an efficient way to reset the canvas for a new frame.
32-bit RGBA color value (0xAABBGGRR format)
canvas.clear(0xFF000000); // Clear to opaque black
canvas.clear(0x00000000); // Clear to transparent
setPixel()
void setPixel(int x, int y, uint32_t color)
Sets the pixel at the given coordinates to the specified color. Coordinates are automatically bounds-checked for safety.
X-coordinate (0 to width-1)
Y-coordinate (0 to height-1)
Coordinates outside the canvas bounds are silently ignored.
getPixel()
uint32_t getPixel(int x, int y) const
Retrieves the color value of the pixel at the given coordinates.
X-coordinate (0 to width-1)
Y-coordinate (0 to height-1)
32-bit RGBA color value, or 0 if out of bounds
getWidth()
getHeight()
getBuffer()
uint32_t* getBuffer() const
Returns a pointer to the underlying pixel buffer for advanced operations.
Pointer to the pixel buffer array containing (width * height) 32-bit pixels
Direct buffer access bypasses bounds checking. Use with caution and ensure proper bounds checking.
Global Instance
extern Canvas* globalCanvas;
This global canvas instance is used by all drawing operations in Fern. It is automatically initialized when Fern::initialize() is called.
Example Usage
uint32_t* buffer = new uint32_t[800 * 600];
Canvas canvas(buffer, 800, 600);
canvas.clear(0xFF000000); // Clear to black
canvas.setPixel(100, 50, 0xFFFF0000); // Set red pixel at (100, 50)
uint32_t pixel = canvas.getPixel(100, 50); // Read pixel