TinyGPU
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
FrameBuffer< RGB_T, SurfaceT > Class Template Reference

Framebuffer with sprite placement and background restoration support. More...

#include <FrameBuffer.h>

Inheritance diagram for FrameBuffer< RGB_T, SurfaceT >:
Inheritance graph
[legend]
Collaboration diagram for FrameBuffer< RGB_T, SurfaceT >:
Collaboration graph
[legend]

Public Member Functions

 FrameBuffer ()=default
 Creates an empty framebuffer.
 
 FrameBuffer (size_t width, size_t height, IFont< RGB_T > &font)
 Creates a framebuffer with the specified size and font.
 
bool begin () override
 Initializes the framebuffer surface.
 
void end () override
 Closes the framebuffer and releases resources.
 
void setTouchDriver (TouchDriver &touch)
 
SpriteInfo< RGB_T, SurfaceT > & addSprite (size_t x, size_t y, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T(0))
 Adds a sprite to the framebuffer and draws it at the given position.
 
SpriteInfo< RGB_T, SurfaceT > & addSprite (size_t x, size_t y, size_t maxX, size_t maxY, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T(0))
 Adds a sprite with a preallocated max buffer size for transformations.
 
void removeSprite (SpriteInfo< RGB_T, SurfaceT > &spriteInfo)
 Removes a sprite and restores the pixels behind it.
 
SpriteInfo< RGB_T, SurfaceT > * getSprite (size_t x, size_t y)
 Returns a pointer to the sprite at the given position, if any.
 
size_t getSpriteCount () const
 Returns the number of sprites currently managed by the framebuffer.
 
SpriteInfo< RGB_T, SurfaceT > * getSprite (int idx)
 Returns a pointer to the sprite at the given index, if valid.
 
SpriteInfo< RGB_T, SurfaceT > * getSpritePtr (int idx)
 
void moveSprite (SpriteInfo< RGB_T, SurfaceT > &spriteInfo, size_t newX, size_t newY)
 Moves a sprite to a new position and redraws it.
 
void scaleSprite (SpriteInfo< RGB_T, SurfaceT > &spriteInfo, float scale)
 
void rotateSprite (SpriteInfo< RGB_T, SurfaceT > &spriteInfo, float angleDegrees)
 
void setPixel (size_t x, size_t y, RGB_T color) override
 ISurface<RGB_T> interface delegation.
 
RGB_T getPixel (size_t x, size_t y) const override
 Returns the pixel at the given position.
 
bool resize (size_t w, size_t h) override
 Resizes the framebuffer surface.
 
size_t width () const override
 Returns the framebuffer width in pixels.
 
size_t height () const override
 Returns the framebuffer height in pixels.
 
void setFont (IFont< RGB_T > &font)
 Sets the font for text rendering.
 
IFont< RGB_T > & font ()
 Returns the currently set font for text rendering.
 
void clear (RGB_T color=RGB_T())
 Clears the framebuffer with a single color.
 
void scroll (int dx, int dy) override
 Scrolls the framebuffer content by the specified offsets.
 
void drawLine (size_t x0, size_t y0, size_t x1, size_t y1, RGB_T color)
 Draws a line between two points.
 
void drawRect (size_t x, size_t y, size_t w, size_t h, RGB_T color)
 Draws a rectangle outline.
 
void fillRect (size_t x, size_t y, size_t w, size_t h, RGB_T color)
 Fills a rectangle.
 
void drawCircle (size_t x, size_t y, size_t r, RGB_T color)
 Draws a circle outline.
 
void fillCircle (size_t x, size_t y, size_t r, RGB_T color)
 Fills a circle.
 
void drawSprite (size_t x, size_t y, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T())
 Draws a sprite.
 
void clearSprite (size_t x, size_t y, ISurface< RGB_T > &sprite, RGB_T clearColor=RGB_T())
 Clears a sprite.
 
void copySprite (size_t x, size_t y, const ISurface< RGB_T > &sprite)
 Copies a sprite.
 
void drawText (int16_t x, int16_t y, const char *text, RGB_T foreground, RGB_T background=RGB_T(), bool opaque=false, uint8_t scale=1, uint8_t spacing=1, uint8_t lineSpacing=1)
 Draws UTF-8 text.
 
LinePrinter< RGB_T > & linePrinter ()
 Returns the line printer for text rendering.
 
bool isInBounds (size_t x, size_t y) const
 Checks if the given coordinates are within the surface bounds.
 
void setPixelClipped (size_t x, size_t y, RGB_T color)
 Sets a pixel with clipping.
 
void drawHorizontalLineClipped (int x0, int x1, int y, RGB_T color)
 Draws a horizontal line with clipping.
 
const uint8_t * data () const override
 Returns the raw pixel buffer as bytes.
 
size_t size () const override
 Returns the size of the buffer in bytes.
 
bool setData (uint8_t *data, size_t dataSize)
 Sets the framebuffer pixel data directly, replacing the current content.
 
bool contains (size_t x, size_t y) override
 checks if the given coordinates are within the surface bounds.
 
void processTouch ()
 Processes touch input and dispatches events to sprites if touched.
 
- Public Member Functions inherited from ISurface< RGB_T >
virtual ~ISurface ()=default
 Destroys the framebuffer interface.
 
virtual bool begin ()=0
 Initializes the framebuffer surface.
 
virtual void end ()=0
 Closes the framebuffer and releases resources.
 
virtual bool resize (size_t newWidth, size_t newHeight)=0
 Resizes the framebuffer surface.
 
virtual void setPixel (size_t x, size_t y, RGB_T color)=0
 Sets a pixel at the given position.
 
virtual RGB_T getPixel (size_t x, size_t y) const =0
 Returns the pixel at the given position.
 
virtual void scroll (int dx, int dy)=0
 Scrolls the framebuffer content by the specified offsets.
 
virtual void clear (RGB_T color=RGB_T(0))=0
 Clears the framebuffer with a single color.
 
virtual void drawLine (size_t x0, size_t y0, size_t x1, size_t y1, RGB_T color)=0
 Draws a line between two points.
 
virtual void drawRect (size_t x, size_t y, size_t w, size_t h, RGB_T color)=0
 Draws a rectangle outline.
 
virtual void fillRect (size_t x, size_t y, size_t w, size_t h, RGB_T color)=0
 Fills a rectangle.
 
virtual void drawCircle (size_t x, size_t y, size_t r, RGB_T color)=0
 Draws a circle outline.
 
virtual void fillCircle (size_t x, size_t y, size_t r, RGB_T color)=0
 Fills a circle.
 
virtual void drawRoundRect (size_t x, size_t y, size_t w, size_t h, size_t radius, RGB_T color)
 
virtual void fillRoundRect (size_t x, size_t y, size_t w, size_t h, size_t radius, RGB_T color)
 
virtual void drawArc (size_t cx, size_t cy, size_t r, float startDeg, float endDeg, RGB_T color, uint8_t thickness=1)
 
virtual void drawSprite (size_t x, size_t y, const ISurface< RGB_T > &sprite, RGB_T invisibleColor=RGB_T(0))=0
 Draws a sprite using an optional transparent color.
 
virtual void clearSprite (size_t x, size_t y, ISurface< RGB_T > &sprite, RGB_T clearColor=RGB_T(0))=0
 Clears the area covered by a sprite.
 
virtual void copySprite (size_t x, size_t y, const ISurface< RGB_T > &sprite)=0
 Copies pixels from the framebuffer into a sprite.
 
virtual size_t width () const =0
 Returns the framebuffer width in pixels.
 
virtual size_t height () const =0
 Returns the framebuffer height in pixels.
 
virtual void drawText (int16_t x, int16_t y, const char *text, RGB_T foreground, RGB_T background=RGB_T(0), bool opaque=false, uint8_t scale=1, uint8_t spacing=1, uint8_t lineSpacing=1)=0
 Draws UTF-8 text using the configured font.
 
virtual IFont< RGB_T > & font ()=0
 Returns the currently set font for text rendering.
 
virtual const uint8_t * data () const =0
 Provides access to the framebuffer as a byte buffer.
 
virtual size_t size () const =0
 Provides the total data size in bytes.
 
virtual bool contains (size_t x, size_t y)=0
 Checks if the given coordinates are within the surface bounds.
 
virtual void pushClipRect (size_t x, size_t y, size_t w, size_t h)
 
virtual void popClipRect ()
 
virtual bool isClipVisible (size_t x, size_t y) const
 

Protected Member Functions

IFont< RGB_T > & activeFont ()
 Returns the currently active font used by the framebuffer.
 
void applyTransformedSprite (SpriteInfo< RGB_T, SurfaceT > &spriteInfo, Surface< RGB_T > &&transformedSprite)
 
void restoreFullBounds (const SpriteInfo< RGB_T, SurfaceT > &spriteInfo, const Rect &oldBounds)
 
void drawSpriteRegion (const ISurface< RGB_T > &source, size_t sourceX, size_t sourceY, size_t destX, size_t destY, size_t width, size_t height)
 Draws a rectangular region from a source surface onto the framebuffer.
 
void captureFramebufferRegion (Surface< RGB_T > &destination, size_t destX, size_t destY, size_t sourceX, size_t sourceY, size_t width, size_t height)
 
void restoreOriginalPixels (const SpriteInfo< RGB_T, SurfaceT > &spriteInfo)
 Restores the original background pixels behind a sprite.
 

Static Protected Member Functions

static size_t centeredCoordinate (size_t oldPosition, size_t oldSize, size_t newSize)
 

Protected Attributes

SurfaceT surface_
 
TouchDriver * p_touchDriver = nullptr
 
Vector< std::unique_ptr< SpriteInfo< RGB_T, SurfaceT > > > sprites_
 

Detailed Description

template<typename RGB_T = RGB565, typename SurfaceT = Surface<RGB_T>>
class tinygpu::FrameBuffer< RGB_T, SurfaceT >

Framebuffer with sprite placement and background restoration support.

The class extends TinyGPU with sprite bookkeeping so sprites can be added, moved, scaled, and removed while preserving the pixels behind them.

Constructor & Destructor Documentation

◆ FrameBuffer() [1/2]

FrameBuffer ( )
default

Creates an empty framebuffer.

◆ FrameBuffer() [2/2]

FrameBuffer ( size_t  width,
size_t  height,
IFont< RGB_T > &  font 
)
inline

Creates a framebuffer with the specified size and font.

Member Function Documentation

◆ activeFont()

IFont< RGB_T > & activeFont ( )
inlineprotected

Returns the currently active font used by the framebuffer.

◆ addSprite() [1/2]

SpriteInfo< RGB_T, SurfaceT > & addSprite ( size_t  x,
size_t  y,
const ISurface< RGB_T > &  sprite,
RGB_T  invisibleColor = RGB_T(0) 
)
inline

Adds a sprite to the framebuffer and draws it at the given position.

◆ addSprite() [2/2]

SpriteInfo< RGB_T, SurfaceT > & addSprite ( size_t  x,
size_t  y,
size_t  maxX,
size_t  maxY,
const ISurface< RGB_T > &  sprite,
RGB_T  invisibleColor = RGB_T(0) 
)
inline

Adds a sprite with a preallocated max buffer size for transformations.

◆ applyTransformedSprite()

void applyTransformedSprite ( SpriteInfo< RGB_T, SurfaceT > &  spriteInfo,
Surface< RGB_T > &&  transformedSprite 
)
inlineprotected

Applies a transformed sprite image to the framebuffer and updates bookkeeping.

◆ begin()

bool begin ( )
inlineoverridevirtual

Initializes the framebuffer surface.

Implements ISurface< RGB_T >.

◆ captureFramebufferRegion()

void captureFramebufferRegion ( Surface< RGB_T > &  destination,
size_t  destX,
size_t  destY,
size_t  sourceX,
size_t  sourceY,
size_t  width,
size_t  height 
)
inlineprotected

Captures a rectangular region from the framebuffer into a destination surface.

◆ centeredCoordinate()

static size_t centeredCoordinate ( size_t  oldPosition,
size_t  oldSize,
size_t  newSize 
)
inlinestaticprotected

Calculates the coordinate to center a new size over an old position/size.

◆ clear()

void clear ( RGB_T  color = RGB_T())
inlinevirtual

Clears the framebuffer with a single color.

Implements ISurface< RGB_T >.

◆ clearSprite()

void clearSprite ( size_t  x,
size_t  y,
ISurface< RGB_T > &  sprite,
RGB_T  clearColor = RGB_T() 
)
inlinevirtual

Clears a sprite.

Implements ISurface< RGB_T >.

◆ contains()

bool contains ( size_t  x,
size_t  y 
)
inlineoverridevirtual

checks if the given coordinates are within the surface bounds.

Implements ISurface< RGB_T >.

◆ copySprite()

void copySprite ( size_t  x,
size_t  y,
const ISurface< RGB_T > &  sprite 
)
inlinevirtual

Copies a sprite.

Implements ISurface< RGB_T >.

◆ data()

const uint8_t * data ( ) const
inlineoverridevirtual

Returns the raw pixel buffer as bytes.

Implements ISurface< RGB_T >.

◆ drawCircle()

void drawCircle ( size_t  x,
size_t  y,
size_t  r,
RGB_T  color 
)
inlinevirtual

Draws a circle outline.

Implements ISurface< RGB_T >.

◆ drawHorizontalLineClipped()

void drawHorizontalLineClipped ( int  x0,
int  x1,
int  y,
RGB_T  color 
)
inline

Draws a horizontal line with clipping.

◆ drawLine()

void drawLine ( size_t  x0,
size_t  y0,
size_t  x1,
size_t  y1,
RGB_T  color 
)
inlinevirtual

Draws a line between two points.

Implements ISurface< RGB_T >.

◆ drawRect()

void drawRect ( size_t  x,
size_t  y,
size_t  w,
size_t  h,
RGB_T  color 
)
inlinevirtual

Draws a rectangle outline.

Implements ISurface< RGB_T >.

◆ drawSprite()

void drawSprite ( size_t  x,
size_t  y,
const ISurface< RGB_T > &  sprite,
RGB_T  invisibleColor = RGB_T() 
)
inlinevirtual

Draws a sprite.

Implements ISurface< RGB_T >.

◆ drawSpriteRegion()

void drawSpriteRegion ( const ISurface< RGB_T > &  source,
size_t  sourceX,
size_t  sourceY,
size_t  destX,
size_t  destY,
size_t  width,
size_t  height 
)
inlineprotected

Draws a rectangular region from a source surface onto the framebuffer.

◆ drawText()

void drawText ( int16_t  x,
int16_t  y,
const char *  text,
RGB_T  foreground,
RGB_T  background = RGB_T(),
bool  opaque = false,
uint8_t  scale = 1,
uint8_t  spacing = 1,
uint8_t  lineSpacing = 1 
)
inlinevirtual

Draws UTF-8 text.

Implements ISurface< RGB_T >.

◆ end()

void end ( )
inlineoverridevirtual

Closes the framebuffer and releases resources.

Implements ISurface< RGB_T >.

◆ fillCircle()

void fillCircle ( size_t  x,
size_t  y,
size_t  r,
RGB_T  color 
)
inlinevirtual

Fills a circle.

Implements ISurface< RGB_T >.

◆ fillRect()

void fillRect ( size_t  x,
size_t  y,
size_t  w,
size_t  h,
RGB_T  color 
)
inlinevirtual

Fills a rectangle.

Implements ISurface< RGB_T >.

◆ font()

IFont< RGB_T > & font ( )
inlinevirtual

Returns the currently set font for text rendering.

Implements ISurface< RGB_T >.

◆ getPixel()

RGB_T getPixel ( size_t  x,
size_t  y 
) const
inlineoverridevirtual

Returns the pixel at the given position.

Implements ISurface< RGB_T >.

◆ getSprite() [1/2]

SpriteInfo< RGB_T, SurfaceT > * getSprite ( int  idx)
inline

Returns a pointer to the sprite at the given index, if valid.

◆ getSprite() [2/2]

SpriteInfo< RGB_T, SurfaceT > * getSprite ( size_t  x,
size_t  y 
)
inline

Returns a pointer to the sprite at the given position, if any.

◆ getSpriteCount()

size_t getSpriteCount ( ) const
inline

Returns the number of sprites currently managed by the framebuffer.

◆ getSpritePtr()

SpriteInfo< RGB_T, SurfaceT > * getSpritePtr ( int  idx)
inline

◆ height()

size_t height ( ) const
inlineoverridevirtual

Returns the framebuffer height in pixels.

Implements ISurface< RGB_T >.

◆ isInBounds()

bool isInBounds ( size_t  x,
size_t  y 
) const
inline

Checks if the given coordinates are within the surface bounds.

◆ linePrinter()

LinePrinter< RGB_T > & linePrinter ( )
inline

Returns the line printer for text rendering.

◆ moveSprite()

void moveSprite ( SpriteInfo< RGB_T, SurfaceT > &  spriteInfo,
size_t  newX,
size_t  newY 
)
inline

Moves a sprite to a new position and redraws it.

◆ processTouch()

void processTouch ( )
inline

Processes touch input and dispatches events to sprites if touched.

◆ removeSprite()

void removeSprite ( SpriteInfo< RGB_T, SurfaceT > &  spriteInfo)
inline

Removes a sprite and restores the pixels behind it.

◆ resize()

bool resize ( size_t  w,
size_t  h 
)
inlineoverridevirtual

Resizes the framebuffer surface.

Implements ISurface< RGB_T >.

◆ restoreFullBounds()

void restoreFullBounds ( const SpriteInfo< RGB_T, SurfaceT > &  spriteInfo,
const Rect &  oldBounds 
)
inlineprotected

Unconditionally restores the entire old sprite footprint to its saved background pixels.

This used to be an optimization that only restored the "ring" of oldBounds exposed by moving/transforming a sprite, reasoning that the geometric overlap between oldBounds and newBounds didn't need restoring since the upcoming drawSprite() call would cover it anyway. That's only true if the sprite's content is unchanged there - but scaling/rotating can turn a pixel that was opaque at some position within the overlap into a background/invisible one (e.g. scaling down shrinks the opaque area), and drawSprite() skips writing wherever the new image is background-colored. Any such pixel was never restored, so it stayed stuck showing the old, larger sprite's color - visible as the old and new sprite sizes overlaid on top of each other. Always restoring the full footprint costs a few dozen extra pixel writes (bounded by the sprite's small max size) in exchange for being correct regardless of what changed.

◆ restoreOriginalPixels()

void restoreOriginalPixels ( const SpriteInfo< RGB_T, SurfaceT > &  spriteInfo)
inlineprotected

Restores the original background pixels behind a sprite.

◆ rotateSprite()

void rotateSprite ( SpriteInfo< RGB_T, SurfaceT > &  spriteInfo,
float  angleDegrees 
)
inline

Rotates a sprite image and redraws it at its current position. Always re-renders from the pristine original at (currentScale, angleDegrees)

◆ scaleSprite()

void scaleSprite ( SpriteInfo< RGB_T, SurfaceT > &  spriteInfo,
float  scale 
)
inline

Scales a sprite image and redraws it at its current position. Always re-renders from the pristine original at (scale, currentAngleDegrees)

◆ scroll()

void scroll ( int  dx,
int  dy 
)
inlineoverridevirtual

Scrolls the framebuffer content by the specified offsets.

Implements ISurface< RGB_T >.

◆ setData()

bool setData ( uint8_t *  data,
size_t  dataSize 
)
inline

Sets the framebuffer pixel data directly, replacing the current content.

◆ setFont()

void setFont ( IFont< RGB_T > &  font)
inline

Sets the font for text rendering.

◆ setPixel()

void setPixel ( size_t  x,
size_t  y,
RGB_T  color 
)
inlineoverridevirtual

ISurface<RGB_T> interface delegation.

Implements ISurface< RGB_T >.

◆ setPixelClipped()

void setPixelClipped ( size_t  x,
size_t  y,
RGB_T  color 
)
inline

Sets a pixel with clipping.

◆ setTouchDriver()

void setTouchDriver ( TouchDriver &  touch)
inline

◆ size()

size_t size ( ) const
inlineoverridevirtual

Returns the size of the buffer in bytes.

Implements ISurface< RGB_T >.

◆ width()

size_t width ( ) const
inlineoverridevirtual

Returns the framebuffer width in pixels.

Implements ISurface< RGB_T >.

Member Data Documentation

◆ p_touchDriver

TouchDriver* p_touchDriver = nullptr
protected

◆ sprites_

Vector<std::unique_ptr<SpriteInfo<RGB_T, SurfaceT> > > sprites_
protected

◆ surface_

SurfaceT surface_
protected

The documentation for this class was generated from the following file: