TinyGPU
Loading...
Searching...
No Matches
Classes | 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]

Classes

struct  Rect
 
struct  SpriteInfo
 

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.
 
SpriteInfoaddSprite (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.
 
SpriteInfoaddSprite (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 &spriteInfo)
 Removes a sprite and restores the pixels behind it.
 
void moveSprite (SpriteInfo &spriteInfo, size_t newX, size_t newY)
 Moves a sprite to a new position and redraws it.
 
void scaleSprite (SpriteInfo &spriteInfo, float scale)
 Scales a sprite image and redraws it at its current position.
 
void rotateSprite (SpriteInfo &spriteInfo, float angleDegrees)
 Rotates a sprite image and redraws it at its current position.
 
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.
 
- 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 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.
 

Protected Member Functions

IFont< RGB_T > & activeFont ()
 Returns the currently active font used by the framebuffer.
 
void applyTransformedSprite (SpriteInfo &spriteInfo, Surface< RGB_T > &&transformedSprite)
 
void restoreExposedPixels (const SpriteInfo &spriteInfo, const Rect &oldBounds, const Rect &overlap)
 
Surface< RGB_T > captureUpdatedBackground (const SpriteInfo &spriteInfo, const Rect &oldBounds, const Rect &newBounds, const Rect &overlap)
 Captures the updated background pixels after a sprite move/transform.
 
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 &spriteInfo)
 Restores the original background pixels behind a sprite.
 
auto findSprite (SpriteInfo &spriteInfo)
 Finds the iterator to a sprite in the internal sprite list.
 

Static Protected Member Functions

static Rect intersect (const Rect &first, const Rect &second)
 Returns the intersection rectangle of two rectangles.
 
static bool isEmpty (const Rect &rect)
 Returns true if the rectangle is empty (zero width or height).
 
static size_t centeredCoordinate (size_t oldPosition, size_t oldSize, size_t newSize)
 Calculates the coordinate to center a new size over an old position/size.
 
static void copySpriteRegion (const ISurface< RGB_T > &source, size_t sourceX, size_t sourceY, Surface< RGB_T > &destination, size_t destX, size_t destY, size_t width, size_t height)
 
static Surface< RGB_T > scaleSpriteImage (const ISurface< RGB_T > &source, float scale, IFont< RGB_T > &font)
 Returns a scaled copy of a sprite image.
 
static Surface< RGB_T > rotateSpriteImage (const ISurface< RGB_T > &source, float angleDegrees, RGB_T fillColor, IFont< RGB_T > &font)
 

Protected Attributes

SurfaceT surface_
 
Vector< std::unique_ptr< SpriteInfo > > 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 & 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 & 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 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.

◆ captureUpdatedBackground()

Surface< RGB_T > captureUpdatedBackground ( const SpriteInfo spriteInfo,
const Rect oldBounds,
const Rect newBounds,
const Rect overlap 
)
inlineprotected

Captures the updated background pixels after a sprite move/transform.

◆ 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 >.

◆ copySprite()

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

Copies a sprite.

Implements ISurface< RGB_T >.

◆ copySpriteRegion()

static void copySpriteRegion ( const ISurface< RGB_T > &  source,
size_t  sourceX,
size_t  sourceY,
Surface< RGB_T > &  destination,
size_t  destX,
size_t  destY,
size_t  width,
size_t  height 
)
inlinestaticprotected

Copies a rectangular region from a source surface to a destination surface.

◆ 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 >.

◆ findSprite()

auto findSprite ( SpriteInfo spriteInfo)
inlineprotected

Finds the iterator to a sprite in the internal sprite list.

◆ 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 >.

◆ height()

size_t height ( ) const
inlineoverridevirtual

Returns the framebuffer height in pixels.

Implements ISurface< RGB_T >.

◆ intersect()

static Rect intersect ( const Rect first,
const Rect second 
)
inlinestaticprotected

Returns the intersection rectangle of two rectangles.

◆ isEmpty()

static bool isEmpty ( const Rect rect)
inlinestaticprotected

Returns true if the rectangle is empty (zero width or height).

◆ 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 spriteInfo,
size_t  newX,
size_t  newY 
)
inline

Moves a sprite to a new position and redraws it.

◆ removeSprite()

void removeSprite ( SpriteInfo 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 >.

◆ restoreExposedPixels()

void restoreExposedPixels ( const SpriteInfo spriteInfo,
const Rect oldBounds,
const Rect overlap 
)
inlineprotected

Restores only the pixels exposed by moving a sprite, using the overlap region.

◆ restoreOriginalPixels()

void restoreOriginalPixels ( const SpriteInfo spriteInfo)
inlineprotected

Restores the original background pixels behind a sprite.

◆ rotateSprite()

void rotateSprite ( SpriteInfo spriteInfo,
float  angleDegrees 
)
inline

Rotates a sprite image and redraws it at its current position.

◆ rotateSpriteImage()

static Surface< RGB_T > rotateSpriteImage ( const ISurface< RGB_T > &  source,
float  angleDegrees,
RGB_T  fillColor,
IFont< RGB_T > &  font 
)
inlinestaticprotected

Returns a rotated copy of a sprite image, filling empty space with fillColor.

◆ scaleSprite()

void scaleSprite ( SpriteInfo spriteInfo,
float  scale 
)
inline

Scales a sprite image and redraws it at its current position.

◆ scaleSpriteImage()

static Surface< RGB_T > scaleSpriteImage ( const ISurface< RGB_T > &  source,
float  scale,
IFont< RGB_T > &  font 
)
inlinestaticprotected

Returns a scaled copy of a sprite image.

◆ 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.

◆ 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

◆ sprites_

Vector<std::unique_ptr<SpriteInfo> > sprites_
protected

◆ surface_

SurfaceT surface_
protected

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