foxygit / fake-08-xbox360 Log in
commit c3b1840861b8daffea45324c3efce2b9fad37263
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Feb 10 20:17:00 2021 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Feb 10 20:17:00 2021 -0700

    WIP implementing fill pattern
---
 source/graphics.cpp    | 53 ++++++++++++++++++++++++++++-----
 source/graphics.h      |  2 ++
 source/picoluaapi.cpp  |  2 ++
 test/graphicstests.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 129 insertions(+), 8 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index e541ebf..ba8d7fe 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -162,7 +162,7 @@ void Graphics::copySpriteToScreen(
 					: bothPix >> 4;  //just last 4 bits

 				if (isColorTransparent(c) == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
-					_private_pset(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
+					_setPixelFromSprite(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
 				}
 			}
 		} else {
@@ -176,7 +176,7 @@ void Graphics::copySpriteToScreen(
 					: bothPix & 0x0f;  //just last 4 bits

 				if (isColorTransparent(c) == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
-					_private_pset(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
+					_setPixelFromSprite(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
 				}
 			}
 		}
@@ -269,7 +269,7 @@ void Graphics::copyStretchSpriteToScreen(
 					? bothPix & 0x0f //just first 4 bits
 					: bothPix >> 4;  //just last 4 bits
 				if (isColorTransparent(c) == false) {
-					_private_pset(scr_x + x, scr_y + y, c);
+					_setPixelFromSprite(scr_x + x, scr_y + y, c);
 				}
 			}
 		} else {
@@ -282,7 +282,7 @@ void Graphics::copyStretchSpriteToScreen(
 					? bothPix >> 4 //just first 4 bits
 					: bothPix & 0x0f;  //just last 4 bits
 				if (isColorTransparent(c) == false) {
-					_private_pset(scr_x + x, scr_y + y, c);
+					_setPixelFromSprite(scr_x + x, scr_y + y, c);
 				}
 			}
 		}
@@ -395,6 +395,45 @@ void Graphics::_private_pset(int x, int y, uint8_t col) {

 	setPixelNibble(x, y, col, _memory->screenBuffer);
 }
+
+void Graphics::_setPixelFromSprite(int x, int y, uint8_t col) {
+	x = x & 127;
+	y = y & 127;
+
+	col = getDrawPalMappedColor(col);
+
+	setPixelNibble(x, y, col, _memory->screenBuffer);
+}
+
+void Graphics::_setPixelFromPen(int x, int y) {
+	x = x & 127;
+	y = y & 127;
+
+	uint8_t col = _memory->drawState.color;
+
+	uint8_t col0 = col & 0x0f;
+	uint8_t col1 = (col & 0xf0) >> 4;
+
+	uint8_t finalC = col0;
+
+
+	uint8_t gridX = x % 4;
+	uint8_t gridY = y % 4;
+	uint8_t bitPlace = gridY * 4 + gridX;
+
+	uint16_t fillp = ((uint16_t)_memory->drawState.fillPattern[1] << 8) + _memory->drawState.fillPattern[0];
+	bool altColor = fillp >> bitPlace & 1;
+	if (altColor) {
+		if (_memory->drawState.fillPatternTransparencyBit & 1){
+			return;
+		}
+
+		finalC = col1;
+	}
+
+	setPixelNibble(x, y, getDrawPalMappedColor(finalC), _memory->screenBuffer);
+
+}
 //end helper methods

 void Graphics::cls() {
@@ -471,7 +510,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, uint8_t col){
 	if (!isYWithinClip(y)){
 		return;
 	}
@@ -480,7 +519,7 @@ void Graphics::_private_h_line (int x1, int x2, int y, uint8_t col){
 	int minx = clampXCoordToClip(std::min(x1, x2));

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

@@ -606,7 +645,7 @@ void Graphics::tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my, fix32 m
 				_memory->spriteSheetData);

             if (!isColorTransparent(col)) {
-                _private_pset(x, y, getDrawPalMappedColor(col));
+                _setPixelFromSprite(x, y, col);
             }
         }

diff --git a/source/graphics.h b/source/graphics.h
index 1366e96..a4e3029 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -90,6 +90,8 @@ class Graphics {
 	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);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 44b650c..9e36b71 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -597,6 +597,8 @@ int fillp(lua_State *L) {
     }

     _graphicsForLuaApi->fillp(pat);
+
+    return 0;
 }

 int flip(lua_State *L) {
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 58cf551..043de8d 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1715,7 +1715,7 @@ TEST_CASE("graphics class behaves as expected") {
         checkPoints(graphics, expectedPoints);
     }
     SUBCASE("fillp(pat) sets values in memory"){
-        //0011001111001100 - checkerboard
+        //0011001111001100 - 2x2 checkerboard
         graphics->fillp(0x33cc);

         CHECK_EQ(picoRam.drawState.fillPattern[0], 0xcc);
@@ -1726,6 +1726,84 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(picoRam.data[0x5f32], 0x33);
         CHECK_EQ(picoRam.data[0x5f33], 0);
     }
+    SUBCASE("fill pattern affects rectfill"){
+        //0b0101101001011010 - 1x1 checkerboard
+        graphics->fillp(0x5A5A);
+        graphics->cls(8);
+
+        graphics->rectfill(0, 0, 4, 4, 10);
+
+        //black and yellow 4x4 checkerboard, with pink elsewhere
+        std::vector<coloredPoint> expectedPoints = {
+            { 0, 0,10},
+            { 1, 0, 0},
+            { 2, 0,10},
+            { 3, 0, 0},
+            { 4, 0,10},
+            { 5, 0, 8},
+            { 0, 1, 0},
+            { 1, 1,10},
+            { 2, 1, 0},
+            { 3, 1,10},
+            { 4, 1, 0},
+            { 5, 1, 8},
+            { 0, 2,10},
+            { 1, 2, 0},
+            { 2, 2,10},
+            { 3, 2, 0},
+            { 4, 2,10},
+            { 5, 2, 8},
+            { 0, 3, 0},
+            { 1, 3,10},
+            { 2, 3, 0},
+            { 3, 3,10},
+            { 4, 3, 0},
+            { 5, 3, 8},
+            { 0, 4,10},
+            { 1, 4, 0},
+            { 2, 4,10},
+            { 3, 4, 0},
+            { 4, 4,10},
+            { 5, 4, 8},
+            { 0, 5, 8},
+            { 1, 5, 8},
+            { 2, 5, 8},
+            { 3, 5, 8},
+            { 4, 5, 8},
+            { 5, 5, 8},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("fill pattern with transparency"){
+        //0b0101101001011010.1 - 1x1 checkerboard with transparency
+        graphics->fillp(23130.5);
+        graphics->cls(3);
+
+        graphics->rectfill(0, 0, 2, 2, 11);
+
+        //3x3 checkerboard, with alt set to transparent
+        std::vector<coloredPoint> expectedPoints = {
+            { 0, 0,11},
+            { 1, 0, 3},
+            { 2, 0,11},
+            { 3, 0, 3},
+            { 0, 1, 3},
+            { 1, 1,11},
+            { 2, 1, 3},
+            { 3, 1, 3},
+            { 0, 2,11},
+            { 1, 2, 3},
+            { 2, 2,11},
+            { 3, 2, 3},
+            { 0, 3, 3},
+            { 1, 3, 3},
+            { 2, 3, 3},
+            { 3, 3, 3}
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }


     //general teardown