Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
fill.h File Reference

Detailed Description

Design Philosophy

Fill algorithms are used to fill bounded spaces on the display with a given color. Fill algorithms can be computationally expensive, so a variety are provided to best suit your need.

Usage

fillDisplayArea() is used to fill a rectangular area. It does not care about what is on the display prior.

shadeDisplayArea() is used to shade a rectangular area using

oddEvenFill() is an efficient way to fill areas using the Even–odd rule. It may not work in all cases, but if it does work, it is preferrable to use.

floodFill() is a less efficient way to fill areas using the Flood fill recursive algorithm. It produces better results than oddEvenFill(), but is considerably slower and uses far more stack memory.

Example

// Fill the display area with a dark cyan
fillDisplayArea(0, 0, TFT_WIDTH, TFT_HEIGHT, c123);
// Shade different areas with different intensities, medium brown
for (int i = 0; i < 5; i++)
{
shadeDisplayArea(0, (i * TFT_HEIGHT) / 5, TFT_WIDTH / 2, ((i + 1) * TFT_HEIGHT) / 5, i, c321);
}
// Draw a red circle
drawCircle(200, 50, 20, c500, 0, 0, 1, 1);
// Flood fill the circle with blue
floodFill(200, 50, c005, 200 - 20, 50 - 20, 200 + 50, 50 + 20);
// Draw a green rectangle
drawRect(200, 150, 250, 220, c050, 0, 0, 1, 1);
// Odd-even fill the rectangle with blue
oddEvenFill(190, 140, 260, 230, c050, c005);
void shadeDisplayArea(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t shadeLevel, paletteColor_t color)
Definition fill.c:77
void fillDisplayArea(int16_t x1, int16_t y1, int16_t x2, int16_t y2, paletteColor_t c)
Fill a rectangular area on a display with a single color.
Definition fill.c:36
void oddEvenFill(int x0, int y0, int x1, int y1, paletteColor_t boundaryColor, paletteColor_t fillColor)
Attempt to fill a convex shape bounded by a border of a given color using the even-odd rule.
Definition fill.c:228
void floodFill(uint16_t x, uint16_t y, paletteColor_t col, uint16_t xMin, uint16_t yMin, uint16_t xMax, uint16_t yMax)
Definition fill.c:324
@ c005
r = 0, g = 0, b = 5
Definition palette.h:29
@ c123
r = 1, g = 2, b = 3
Definition palette.h:75
@ c321
r = 3, g = 2, b = 1
Definition palette.h:145
@ c050
r = 0, g = 5, b = 0
Definition palette.h:54
@ c500
r = 5, g = 0, b = 0
Definition palette.h:204
void drawRect(int x0, int y0, int x1, int y1, paletteColor_t col)
Draw the a one pixel wide outline of a rectangle.
Definition shapes.c:476
void drawCircle(int xm, int ym, int r, paletteColor_t col)
Draw the one pixel wide outline of a circle.
Definition shapes.c:1148

Go to the source code of this file.

Functions

void fillDisplayArea (int16_t x1, int16_t y1, int16_t x2, int16_t y2, paletteColor_t c)
 Fill a rectangular area on a display with a single color.
 
void shadeDisplayArea (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t shadeLevel, paletteColor_t color)
 
void oddEvenFill (int x0, int y0, int x1, int y1, paletteColor_t boundaryColor, paletteColor_t fillColor)
 Attempt to fill a convex shape bounded by a border of a given color using the even-odd rule.
 
void floodFill (uint16_t x, uint16_t y, paletteColor_t col, uint16_t xMin, uint16_t yMin, uint16_t xMax, uint16_t yMax)
 
void fillCircleSector (uint16_t x, uint16_t y, uint16_t innerR, uint16_t outerR, uint16_t startAngle, uint16_t endAngle, paletteColor_t col)
 Helper function to draw a filled circle with translation and scaling.
 

Function Documentation

◆ fillDisplayArea()

void fillDisplayArea ( int16_t x1,
int16_t y1,
int16_t x2,
int16_t y2,
paletteColor_t c )

Fill a rectangular area on a display with a single color.

Parameters
x1The x coordinate to start the fill (top left)
y1The y coordinate to start the fill (top left)
x2The x coordinate to stop the fill (bottom right)
y2The y coordinate to stop the fill (bottom right)
cThe color to fill

◆ shadeDisplayArea()

void shadeDisplayArea ( int16_t x1,
int16_t y1,
int16_t x2,
int16_t y2,
uint8_t shadeLevel,
paletteColor_t color )

'Shade' an area by drawing pixels over it in a ordered-dithering way

Parameters
x1The X pixel to start at
y1The Y pixel to start at
x2The X pixel to end at
y2The Y pixel to end at
shadeLevelThe level of shading, Higher means more shaded. Must be 0 to 4
colorthe color to draw with

◆ oddEvenFill()

void oddEvenFill ( int x0,
int y0,
int x1,
int y1,
paletteColor_t boundaryColor,
paletteColor_t fillColor )

Attempt to fill a convex shape bounded by a border of a given color using the even-odd rule.

https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule

WARNING!!! This is very finicky and is not guaranteed to work in all cases.

This iterates over each row in the bounding box, top to bottom, left to right

This assumes that each row starts outside or on the boundary of the shape to be filled.

Each time a pixel of the 'boundary color' is iterated over, the in/out boolean will flip. Thick boundaries will have hysteresis, but shapes with concave features will have undrawn rows because the function checks for an even number of transitions within a row before drawing.

Parameters
x0The left index of the bounding box
y0The top index of the bounding box
x1The right index of the bounding box
y1The bottom index of the bounding box
boundaryColorThe color of the boundary to fill in
fillColorThe color to fill

◆ floodFill()

void floodFill ( uint16_t x,
uint16_t y,
paletteColor_t col,
uint16_t xMin,
uint16_t yMin,
uint16_t xMax,
uint16_t yMax )

This is a recursive flood fill algorithm. It starts at the given coordinate, and will replace the color at that coordinate, and all adjacent pixels with the same color, with the fill color.

The flood is also bounded wthin the given rectangle.

Note that the recursive algorithm is relatively slow and uses a lot of stack memory, so try not to use it when possible.

This is adapted from http://www.adammil.net/blog/v126_A_More_Efficient_Flood_Fill.html

Parameters
xThe X coordinate to start the fill at
yThe Y coordinate to start the fill at
colThe color to fill in
xMinThe minimum X coordinate to bound the fill
yMinThe minimum Y coordinate to bound the fill
xMaxThe maximum X coordinate to bound the fill
yMaxThe maximum Y coordinate to bound the fill

◆ fillCircleSector()

void fillCircleSector ( uint16_t x,
uint16_t y,
uint16_t innerR,
uint16_t outerR,
uint16_t startAngle,
uint16_t endAngle,
paletteColor_t col )

Helper function to draw a filled circle with translation and scaling.

Parameters
xThe X coordinate of the center of the circle
yThe Y coordinate of the center of the circle
innerRThe radius of the inside of the sector, or 0 for the whole circle
outerRThe radius of the circle
startAngleThe starting angle of the sector, in degrees CCW
endAngleThe ending angle of the sector, in degrees CCW
colThe color to fill