foxygit / fake-08-xbox360 Log in
commit 5fc10239c822a4e07a63537d001dbe2c6daf00f6
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 17 08:56:10 2026 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 17 08:56:10 2026 -0700

    bitmask sprite color values to ensure fix transparency issues
---
 source/graphics.cpp    | 19 +++++++++++++++----
 test/graphicstests.cpp | 28 ++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 141cadc..d03029e 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -155,12 +155,13 @@ void Graphics::copySpriteToScreen(
 			//			? bothPix & 0x0f //just first 4 bits
 			//			: bothPix >> 4;  //just last 4 bits
 			uint8_t lc = bothPix & 0x0f;
-			uint8_t rc = bothPix >> 4;
+			uint8_t rc = (bothPix >> 4) & 0x0f;

 			const int finaly = scr_y + y;
 			int finalx = scr_x + (flip_x ? x + 1 : x);

 			if (x > 0 || !startWithHalf){
+				lc &= 0x0f; // Ensure color is only 4 bits before palette lookup
 				if (!(drawState.drawPaletteMap[lc] >> 4)){
 					lc = drawState.drawPaletteMap[lc] & 0x0f;

@@ -188,6 +189,7 @@ void Graphics::copySpriteToScreen(
 			}

 			if (x < scr_w) {
+				rc &= 0x0f; // Ensure color is only 4 bits before palette lookup
 				if (!(drawState.drawPaletteMap[rc] >> 4)){
 					rc = drawState.drawPaletteMap[rc] & 0x0f;

@@ -347,7 +349,8 @@ void Graphics::copyStretchSpriteToScreen(

 					uint8_t c = shiftedPixIndex % 2 == 0
 						? bothPix & 0x0f //just first 4 bits
-						: bothPix >> 4;  //just last 4 bits
+						: (bothPix >> 4) & 0x0f;  //just last 4 bits
+					c &= 0x0f; // Ensure color is only 4 bits before palette lookup

 					if (_memory->drawState.drawPaletteMap[c] >> 4){
 						continue;
@@ -397,7 +400,8 @@ void Graphics::copyStretchSpriteToScreen(

 					uint8_t c = shiftedPixIndex % 2 == 0
 						? bothPix & 0x0f //just first 4 bits
-						: bothPix >> 4;  //just last 4 bits
+						: (bothPix >> 4) & 0x0f;  //just last 4 bits
+					c &= 0x0f; // Ensure color is only 4 bits before palette lookup

 					if (_memory->drawState.drawPaletteMap[c] >> 4){
 						continue;
@@ -1840,7 +1844,14 @@ void Graphics::pal() {
 void Graphics::pal(uint8_t p) {
 	for (uint8_t c = 0; c < 16; c++) {
 		if (p == 0) {
-			_memory->drawState.drawPaletteMap[c] = c;
+			// Preserve transparency bit when resetting palette mapping
+			// But ensure color 0 is transparent by default if it wasn't set
+			uint8_t transparencyBit = _memory->drawState.drawPaletteMap[c] & 0x10;
+			if (c == 0 && transparencyBit == 0) {
+				// Color 0 should be transparent by default
+				transparencyBit = 0x10;
+			}
+			_memory->drawState.drawPaletteMap[c] = c | transparencyBit;
 		} else if (p == 1) {
 			_memory->drawState.screenPaletteMap[c] = c;
 		}
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 70a3846..04cc294 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -2779,6 +2779,34 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(graphics->pget(10, 11), 5); // below should be color 5
         CHECK_EQ(graphics->pget(17, 17), 5); // bottom-right should be color 5
     }
+    SUBCASE("Sprite transparency with upper memory mapping") {
+        //map sprite sheet to upper memory
+        picoRam.hwState.spriteSheetMemMapping = 0xC0;
+
+        graphics->palt();
+
+        //create a sprite with alternating pixels
+        for (int y = 0; y < 8; y++) {
+            for (int x = 0; x < 8; x++) {
+                //left pixel is opaque (5), right pixel is transparent (0)
+                uint8_t color = x % 2 == 0 ? 5 : 0;
+                graphics->sset(x, y, color);
+            }
+        }
+
+        graphics->cls(2);
+        graphics->spr(0, 10, 10, 1.0, 1.0, false, false);
+
+        // Even X positions should be opaque (color 5)
+        CHECK_EQ(graphics->pget(10, 10), 5);
+        CHECK_EQ(graphics->pget(12, 10), 5);
+        CHECK_EQ(graphics->pget(14, 10), 5);
+
+        // Odd X positions should be background color (color 2)
+        CHECK_EQ(graphics->pget(11, 10), 2);
+        CHECK_EQ(graphics->pget(13, 10), 2);
+        CHECK_EQ(graphics->pget(15, 10), 2);
+    }


     //general teardown