Skip to content

Commit

Permalink
More aggressive short-circuiting for #8 and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-random committed May 9, 2021
1 parent 86c4349 commit 4a3fc20
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
52 changes: 46 additions & 6 deletions ESP_8_BIT_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ int16_t ESP_8_BIT_GFX::clampY(int16_t inputY)
*/
void ESP_8_BIT_GFX::drawPixel(int16_t x, int16_t y, uint16_t color)
{
x = clampX(x);
y = clampY(y);
if (x < 0 || x > MAX_X ||
y < 0 || y > MAX_Y )
{
// This pixel is off screen, nothing to draw.
return;
}

_pVideo->getFrameBufferLines()[y][x] = getColor8(color);
}
Expand All @@ -198,7 +202,18 @@ void ESP_8_BIT_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t colo
return;
}

int16_t clampedX = clampX(x);
if (x < 0 || x > MAX_X)
{
// This vertical line is off screen left or right, nothing to draw.
return;
}

if (y+h < 0 || y > MAX_Y )
{
// This vertical line is off screen top or bottom, nothing to draw.
return;
}

int16_t clampedY = clampY(y);
int16_t clampedYH = clampY(y+h-1)+1;

Expand All @@ -208,7 +223,7 @@ void ESP_8_BIT_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t colo
startWrite();
for(int16_t vertical = clampedY; vertical < clampedYH; vertical++)
{
lines[vertical][clampedX] = color8;
lines[vertical][x] = color8;
}
endWrite();
}
Expand All @@ -230,16 +245,28 @@ void ESP_8_BIT_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t colo
// Don't draw anything for zero or negative width
return;
}

if (y < 0 || y > MAX_Y)
{
// This horizontal line is off screen top or bottom, nothing to draw.
return;
}

if (x+w < 0 || x > MAX_X)
{
// This horizontal line is off screen left or right, nothing to draw.
return;
}

int16_t clampedX = clampX(x);
int16_t clampedY = clampY(y);
int16_t clampedXW = clampX(x+w-1);
int16_t fillWidth = clampedXW-clampedX+1;

uint8_t color8 = getColor8(color);
uint8_t** lines = _pVideo->getFrameBufferLines();

startWrite();
memset(&(lines[clampedY][clampedX]), color8, fillWidth);
memset(&(lines[y][clampedX]), color8, fillWidth);
endWrite();
}

Expand All @@ -260,12 +287,25 @@ void ESP_8_BIT_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_
// Don't draw anything for zero or negative height
return;
}

if (w < 1)
{
// Don't draw anything for zero or negative width
return;
}

if (x+w < 0 || x > MAX_X)
{
// This rectangle is off screen left or right, nothing to draw.
return;
}

if (y+h < 0 || y > MAX_Y )
{
// This rectangle is off screen top or bottom, nothing to draw.
return;
}

int16_t clampedX = clampX(x);
int16_t clampedXW = clampX(x+w-1);
int16_t fillWidth = clampedXW-clampedX+1;
Expand Down
6 changes: 3 additions & 3 deletions examples/GFX_Screen_Fillers/GFX_Screen_Fillers.ino
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void loop() {
break;
case 1:
// A circle that grows from the center of the screen, then shrinks.
videoOut.fillCircle(128,120,120*progress,nextHue());
videoOut.fillCircle(128,120,130*progress,nextHue());
break;
case 2:
// Draws horizontally across the screen with lots of vertical lines.
Expand All @@ -202,8 +202,8 @@ void loop() {
break;
case 4:
// Regression test for https://github.com/Roger-random/ESP_8_BIT_composite/issues/8
videoOut.fillCircle(255*progress, 120, 40+20*progress, nextHue());
videoOut.fillCircle(128, 239*progress, 40-20*progress, hues[(hueIndex+17)%34]);
videoOut.fillCircle(335*progress-40, 120, 40+20*progress, nextHue());
videoOut.fillCircle(128, 319*progress-40, 40-20*progress, hues[(hueIndex+17)%34]);
break;
default:
stage = 0;
Expand Down

0 comments on commit 4a3fc20

Please sign in to comment.