commit 9ca32aee5ecc329a97ea4253416847a9cd5c5485
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Apr 24 13:57:17 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Apr 24 13:57:17 2021 -0600
Fix bad pixels when drawing shapes off screen
---
source/graphics.cpp | 16 +++++++++++++---
source/graphics.h | 2 +-
test/Makefile | 3 ++-
test/graphicstests.cpp | 27 +++++++++++++++++++++++++++
4 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/source/graphics.cpp b/source/graphics.cpp
index d6e4b15..55d2f8e 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -385,7 +385,7 @@ int Graphics::clampXCoordToClip(int x) {
(int)_memory->drawState.clip_xe - 1);
}
-int Graphics::clampYCoordToCLip(int y) {
+int Graphics::clampYCoordToClip(int y) {
return clamp(
y,
(int)_memory->drawState.clip_yb,
@@ -527,6 +527,11 @@ void Graphics::_private_h_line(int x1, int x2, int y){
return;
}
+ if ((x1 < _memory->drawState.clip_xb && x2 < _memory->drawState.clip_xb) ||
+ (x1 > _memory->drawState.clip_xe && x2 > _memory->drawState.clip_xe)) {
+ return;
+ }
+
int maxx = clampXCoordToClip(std::max(x1, x2));
int minx = clampXCoordToClip(std::min(x1, x2));
@@ -541,8 +546,13 @@ void Graphics::_private_v_line (int y1, int y2, int x){
return;
}
- int maxy = clampYCoordToCLip(std::max(y1, y2));
- int miny = clampYCoordToCLip(std::min(y1, y2));
+ if ((y1 < _memory->drawState.clip_yb && y2 < _memory->drawState.clip_yb) ||
+ (y1 > _memory->drawState.clip_ye && y2 > _memory->drawState.clip_ye)) {
+ return;
+ }
+
+ int maxy = clampYCoordToClip(std::max(y1, y2));
+ int miny = clampYCoordToClip(std::min(y1, y2));
for (int y = miny; y <= maxy; y++){
_setPixelFromPen(x, y);
diff --git a/source/graphics.h b/source/graphics.h
index 14934d8..0090eb6 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -86,7 +86,7 @@ class Graphics {
bool isXWithinClip(int x);
bool isYWithinClip(int y);
int clampXCoordToClip(int x);
- int clampYCoordToCLip(int y);
+ int clampYCoordToClip(int y);
void _setPixelFromSprite(int x, int y, uint8_t col);
void _setPixelFromPen(int x, int y);
diff --git a/test/Makefile b/test/Makefile
index 9fe4053..9896c5d 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,5 +1,6 @@
#match flags from 3ds and switch makefiles. This may not all be necessary
-CFLAGS := -Wall -g -c -O2 \
+# removed -O2 to make debugging tests easier
+CFLAGS := -Wall -g -c \
-fno-rtti -fexceptions -std=gnu++11
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 1b3f75d..d061b83 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -583,6 +583,33 @@ TEST_CASE("graphics class behaves as expected") {
checkPoints(graphics, expectedPoints);
}
+ SUBCASE("rect({x1}, {y1}, {x2}, {y2}) offscreen x draws nothing") {
+ graphics->cls();
+ graphics->color(15);
+ graphics->rect(-50, 40, -40, 42);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {0, 40, 0},
+ {0, 41, 0},
+ {0, 42, 0},
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("rect({x1}, {y1}, {x2}, {y2}) offscreen y draws nothing") {
+ graphics->cls();
+ graphics->color(15);
+ graphics->rect(40, -50, 43, -40);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {40, 0, 0},
+ {41, 0, 0},
+ {42, 0, 0},
+ {43, 0, 0},
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
SUBCASE("rect({x1}, {y1}, {x2}, {y2}, {c}) swapped coords work, color used") {
graphics->cls();
graphics->rect(42, 43, 40, 40, 1);