foxygit / fake-08-xbox360 Log in
commit 5b95951efdbf68b7caeccbc051a2ca3ecfa59fc7
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Thu Feb 11 10:45:54 2021 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Feb 11 10:45:54 2021 -0700

    Finish implementing fillpattern
---
 source/graphics.cpp                      | 111 ++++++++++++++-----------------
 source/graphics.h                        |   7 +-
 test/carts/fillptest.p8                  |  43 ++++++++++++
 test/carts/screenshots/fillptest_f01.png | Bin 0 -> 1054 bytes
 test/endtoendtests.cpp                   |  14 ++++
 test/graphicstests.cpp                   |  43 ++++++++++++
 6 files changed, 154 insertions(+), 64 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index ba8d7fe..7494239 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -381,21 +381,12 @@ int Graphics::clampYCoordToCLip(int y) {
 }


-void Graphics::_private_safe_pset(int x, int y, uint8_t col) {
+void Graphics::_safeSetPixelFromPen(int x, int y) {
 	if (isWithinClip(x, y)){
-		_private_pset(x, y, col);
+		_setPixelFromPen(x, y);
 	}
 }

-void Graphics::_private_pset(int x, int y, uint8_t col) {
-	x = x & 127;
-	y = y & 127;
-
-	col = getDrawPalMappedColor(col);
-
-	setPixelNibble(x, y, col, _memory->screenBuffer);
-}
-
 void Graphics::_setPixelFromSprite(int x, int y, uint8_t col) {
 	x = x & 127;
 	y = y & 127;
@@ -417,12 +408,13 @@ void Graphics::_setPixelFromPen(int x, int y) {
 	uint8_t finalC = col0;


-	uint8_t gridX = x % 4;
-	uint8_t gridY = y % 4;
-	uint8_t bitPlace = gridY * 4 + gridX;
+	//uint8_t gridX = x % 4;
+	//uint8_t gridY = y % 4;
+	//uint8_t bitPlace = gridY * 4 + gridX;
+	uint8_t bitPlace = 15 - ((x & 3) + 4 * (y & 3));

 	uint16_t fillp = ((uint16_t)_memory->drawState.fillPattern[1] << 8) + _memory->drawState.fillPattern[0];
-	bool altColor = fillp >> bitPlace & 1;
+	bool altColor = (fillp >> bitPlace) & 0x1;
 	if (altColor) {
 		if (_memory->drawState.fillPatternTransparencyBit & 1){
 			return;
@@ -432,7 +424,6 @@ void Graphics::_setPixelFromPen(int x, int y) {
 	}

 	setPixelNibble(x, y, getDrawPalMappedColor(finalC), _memory->screenBuffer);
-
 }
 //end helper methods

@@ -459,7 +450,7 @@ void Graphics::pset(int x, int y, uint8_t col){
 	applyCameraToPoint(&x, &y);

 	if (isWithinClip(x, y)){
-		_private_pset(x, y, col);
+		_setPixelFromPen(x, y);
 	}
 }

@@ -510,7 +501,7 @@ void Graphics::line (int x1, int y1, int x2, int y2){
 	this->line(x1, y1, x2, y2, _memory->drawState.color);
 }

-void Graphics::_private_h_line(int x1, int x2, int y, uint8_t col){
+void Graphics::_private_h_line(int x1, int x2, int y){
 	if (!isYWithinClip(y)){
 		return;
 	}
@@ -523,7 +514,7 @@ void Graphics::_private_h_line(int x1, int x2, int y, uint8_t col){
 	}
 }

-void Graphics::_private_v_line (int y1, int y2, int x, uint8_t col){
+void Graphics::_private_v_line (int y1, int y2, int x){
 	//save draw calls if its out
 	if (!isXWithinClip(x)){
 		return;
@@ -533,7 +524,7 @@ void Graphics::_private_v_line (int y1, int y2, int x, uint8_t col){
 	int miny = clampYCoordToCLip(std::min(y1, y2));

 	for (int y = miny; y <= maxy; y++){
-		_private_pset(x, y, col);
+		_setPixelFromPen(x, y);
 	}
 }

@@ -549,10 +540,10 @@ void Graphics::line(int x0, int y0, int x1, int y1, uint8_t col) {

 	//vertical line
 	if (x0 == x1) {
-		_private_v_line(y0, y1, x0, col);
+		_private_v_line(y0, y1, x0);
 	}
 	else if (y0 == y1) {
-		_private_h_line(x0, x1, y0, col);
+		_private_h_line(x0, x1, y0);
 	}
 	else {
 		//tac08 line impl for diagonals (this should work for horizontal and vertical as well,
@@ -562,7 +553,7 @@ void Graphics::line(int x0, int y0, int x1, int y1, uint8_t col) {
 		int err = dx + dy, e2; /* error value e_xy */

 		for (;;) { /* loop */
-			_private_safe_pset(x0, y0, col);
+			_safeSetPixelFromPen(x0, y0);
 			if (x0 == x1 && y0 == y1)
 				break;
 			e2 = 2 * err;
@@ -689,15 +680,15 @@ void Graphics::circ(int ox, int oy, int r, uint8_t col){
 	int decisionOver2 = 1-x;

 	while (y <= x) {
-		_private_safe_pset(ox + x, oy + y, col);
-		_private_safe_pset(ox + y, oy + x, col);
-		_private_safe_pset(ox - x, oy + y, col);
-		_private_safe_pset(ox - y, oy + x, col);
+		_safeSetPixelFromPen(ox + x, oy + y);
+		_safeSetPixelFromPen(ox + y, oy + x);
+		_safeSetPixelFromPen(ox - x, oy + y);
+		_safeSetPixelFromPen(ox - y, oy + x);

-		_private_safe_pset(ox - x, oy - y, col);
-		_private_safe_pset(ox - y, oy - x, col);
-		_private_safe_pset(ox + x, oy - y, col);
-		_private_safe_pset(ox + y, oy - x, col);
+		_safeSetPixelFromPen(ox - x, oy - y);
+		_safeSetPixelFromPen(ox - y, oy - x);
+		_safeSetPixelFromPen(ox + x, oy - y);
+		_safeSetPixelFromPen(ox + y, oy - x);

 		y += 1;
 		if (decisionOver2 < 0) {
@@ -725,18 +716,18 @@ void Graphics::circfill(int ox, int oy, int r, uint8_t col){
 	applyCameraToPoint(&ox, &oy);

 	if (r == 0) {
-		_private_safe_pset(ox, oy, col);
+		_safeSetPixelFromPen(ox, oy);
 	}
 	else if (r == 1) {
-		_private_safe_pset(ox, oy - 1, col);
-		_private_h_line(ox-1, ox+1, oy, col);
-		_private_safe_pset(ox, oy + 1, col);
+		_safeSetPixelFromPen(ox, oy - 1);
+		_private_h_line(ox-1, ox+1, oy);
+		_safeSetPixelFromPen(ox, oy + 1);
 	}
 	else if (r > 0) {
 		int x = -r, y = 0, err = 2 - 2 * r;
 		do {
-			_private_h_line(ox - x, ox + x, oy + y, col);
-			_private_h_line(ox - x, ox + x, oy - y, col);
+			_private_h_line(ox - x, ox + x, oy + y);
+			_private_h_line(ox - x, ox + x, oy - y);
 			r = err;
 			if (r > x)
 				err += ++x * 2 + 1;
@@ -773,8 +764,8 @@ void Graphics::oval(int x0, int y0, int x1, int y1, uint8_t col) {
     int bsq = yr * yr;
     int xa, ya;

-	_private_safe_pset(xc, yc+yr, col);
-    _private_safe_pset(xc, yc-yr, col);
+	_safeSetPixelFromPen(xc, yc+yr);
+    _safeSetPixelFromPen(xc, yc-yr);

     wx = 0;
     wy = yr;
@@ -798,14 +789,14 @@ void Graphics::oval(int x0, int y0, int x1, int y1, uint8_t col) {
           break;


-        _private_safe_pset(xc+wx, yc-wy, col);
-        _private_safe_pset(xc-wx, yc-wy, col);
-        _private_safe_pset(xc+wx, yc+wy, col);
-        _private_safe_pset(xc-wx, yc+wy, col);
+        _safeSetPixelFromPen(xc+wx, yc-wy);
+        _safeSetPixelFromPen(xc-wx, yc-wy);
+        _safeSetPixelFromPen(xc+wx, yc+wy);
+        _safeSetPixelFromPen(xc-wx, yc+wy);
     }

-    _private_safe_pset(xc+xr, yc, col);
-    _private_safe_pset(xc-xr, yc, col);
+    _safeSetPixelFromPen(xc+xr, yc);
+    _safeSetPixelFromPen(xc-xr, yc);

     wx = xr;
     wy = 0;
@@ -829,10 +820,10 @@ void Graphics::oval(int x0, int y0, int x1, int y1, uint8_t col) {
         if (ya > xa)
           break;

-        _private_safe_pset(xc+wx, yc-wy, col);
-        _private_safe_pset(xc-wx, yc-wy, col);
-        _private_safe_pset(xc+wx, yc+wy, col);
-        _private_safe_pset(xc-wx, yc+wy, col);
+        _safeSetPixelFromPen(xc+wx, yc-wy);
+        _safeSetPixelFromPen(xc-wx, yc-wy);
+        _safeSetPixelFromPen(xc+wx, yc+wy);
+        _safeSetPixelFromPen(xc-wx, yc+wy);
     }
 }

@@ -863,7 +854,7 @@ void Graphics::ovalfill(int x0, int y0, int x1, int y1, uint8_t col){
     int bsq = yr * yr;
     int xa, ya;

-	_private_v_line(yc+yr, yc-yr, xc, col);
+	_private_v_line(yc+yr, yc-yr, xc);

     wx = 0;
     wy = yr;
@@ -886,11 +877,11 @@ void Graphics::ovalfill(int x0, int y0, int x1, int y1, uint8_t col){
         if (xa >= ya)
           break;

-		_private_h_line(xc+wx, xc-wx, yc-wy, col);
-		_private_h_line(xc+wx, xc-wx, yc+wy, col);
+		_private_h_line(xc+wx, xc-wx, yc-wy);
+		_private_h_line(xc+wx, xc-wx, yc+wy);
     }

-	_private_h_line(xc+xr, xc-xr, yc, col);
+	_private_h_line(xc+xr, xc-xr, yc);

     wx = xr;
     wy = 0;
@@ -914,8 +905,8 @@ void Graphics::ovalfill(int x0, int y0, int x1, int y1, uint8_t col){
         if (ya > xa)
           break;

-		_private_h_line(xc+wx, xc-wx, yc-wy, col);
-		_private_h_line(xc+wx, xc-wx, yc+wy, col);
+		_private_h_line(xc+wx, xc-wx, yc-wy);
+		_private_h_line(xc+wx, xc-wx, yc+wy);
     }
 	/*
 	//this algo doesn't like up correctly
@@ -950,11 +941,11 @@ void Graphics::rect(int x1, int y1, int x2, int y2, uint8_t col) {

 	sortCoordsForRect(&x1, &y1, &x2, &y2);

-	_private_h_line(x1, x2, y1, col);
-	_private_h_line(x1, x2, y2, col);
+	_private_h_line(x1, x2, y1);
+	_private_h_line(x1, x2, y2);

-	_private_v_line(y1, y2, x1, col);
-	_private_v_line(y1, y2, x2, col);
+	_private_v_line(y1, y2, x1);
+	_private_v_line(y1, y2, x2);
 }

 void Graphics::rectfill(int x1, int y1, int x2, int y2) {
@@ -970,7 +961,7 @@ void Graphics::rectfill(int x1, int y1, int x2, int y2, uint8_t col) {
 	sortCoordsForRect(&x1, &y1, &x2, &y2);

 	for (int y = y1; y <= y2; y++) {
-		_private_h_line(x1, x2, y, col);
+		_private_h_line(x1, x2, y);
 	}
 }

diff --git a/source/graphics.h b/source/graphics.h
index a4e3029..b5cdf37 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -89,12 +89,11 @@ class Graphics {
 	int clampXCoordToClip(int x);
 	int clampYCoordToCLip(int y);

-	void _private_pset(int x, int y, uint8_t col);
 	void _setPixelFromSprite(int x, int y, uint8_t col);
 	void _setPixelFromPen(int x, int y);
-	void _private_safe_pset(int x, int y, uint8_t col);
-	void _private_h_line (int x1, int x2, int y, uint8_t col);
-	void _private_v_line (int y1, int y2, int x, uint8_t col);
+	void _safeSetPixelFromPen(int x, int y);
+	void _private_h_line (int x1, int x2, int y);
+	void _private_v_line (int y1, int y2, int x);

 	public:
 	Graphics(std::string fontdata, PicoRam* memory);
diff --git a/test/carts/fillptest.p8 b/test/carts/fillptest.p8
new file mode 100644
index 0000000..e68cefb
--- /dev/null
+++ b/test/carts/fillptest.p8
@@ -0,0 +1,43 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _draw()
+cls()
+fillp()
+rectfill(0,0,127,127,1)
+fillp(0b0011001111001100)
+color (2)
+print (peek(0x5f31))
+print (peek(0x5f32))
+print (peek(0x5f33))
+circfill(40, 40, 10, 8)
+fillp()
+circ(40, 40, 10, 7)
+
+circfill(80, 80, 10, 6)
+fillp(0b0011001111001100.1)
+color (2)
+print (peek(0x5f31))
+print (peek(0x5f32))
+print (peek(0x5f33))
+circfill(80, 80, 10, 8)
+fillp()
+circ(80, 80, 10, 7)
+
+fillp(0b0011011011000110)
+rectfill(20, 100, 108, 120, 0xe8)
+
+-- get the current fill pattern from its memory-mapped addresses
+pattern = peek2(0x5f31)+peek(0x5f33)/2
+color (2)
+print (peek(0x5f31))
+print (peek(0x5f32))
+print (peek(0x5f33))
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/fillptest_f01.png b/test/carts/screenshots/fillptest_f01.png
new file mode 100644
index 0000000..c672f67
Binary files /dev/null and b/test/carts/screenshots/fillptest_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 7398a6d..7ee4757 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -422,6 +422,20 @@ TEST_CASE("Loading and running carts") {
             CHECK(vm->GetBiosError() == "");
         }
     }
+    SUBCASE("Fill pattern test cart"){
+        vm->LoadCart("carts/fillptest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/fillptest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
 }
\ No newline at end of file
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 043de8d..9b0ac1f 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1804,6 +1804,49 @@ TEST_CASE("graphics class behaves as expected") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("fill pattern oriented correctly"){
+        //311 = 0b0000000100110111
+        //0000
+        //0001
+        //0011
+        //0111
+        graphics->cls(0);
+        graphics->fillp(311);
+
+        //e8: light pink alt color, pink normal color
+        graphics->rectfill(0, 0, 3, 3, 0xe8);
+
+        //black and yellow 4x4 checkerboard, with pink elsewhere
+        std::vector<coloredPoint> expectedPoints = {
+            { 0, 0, 8},
+            { 1, 0, 8},
+            { 2, 0, 8},
+            { 3, 0, 8},
+            { 4, 0, 0},
+            { 0, 1, 8},
+            { 1, 1, 8},
+            { 2, 1, 8},
+            { 3, 1,14},
+            { 4, 1, 0},
+            { 0, 2, 8},
+            { 1, 2, 8},
+            { 2, 2,14},
+            { 3, 2,14},
+            { 4, 2, 0},
+            { 0, 3, 8},
+            { 1, 3,14},
+            { 2, 3,14},
+            { 3, 3,14},
+            { 4, 3, 0},
+            { 0, 4, 0},
+            { 1, 4, 0},
+            { 2, 4, 0},
+            { 3, 4, 0},
+            { 4, 4, 0}
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }


     //general teardown