foxygit / fake-08-xbox360 Log in
commit 015a986dfb99eb0660b87600818f5090bd8667ae
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Aug 28 15:57:55 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Aug 28 15:57:55 2021 -0600

    fix bugs in sprite blitting
---
 source/graphics.cpp | 79 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 49 insertions(+), 30 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 553f346..61a6496 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -151,9 +151,16 @@ void Graphics::copySpriteToScreen(
 	uint8_t lastScreenBuffByte = 0;
 	int lastScreenBuffIdx = -1;

+	bool startWithHalf = false;
+	//starting with odd pixel
+	if (BITMASK(0) & spr_x) {
+		startWithHalf = true;
+	}
+
 	//since we get 2 pixels at a time, don't go through this whole loop every pixel
 	for (int y = 0; y < scr_h; y++) {
-		for (int x = 0; x < scr_w; x++) {
+		int x = 0;
+		while (x < scr_w) {
 			int abs_spr_x = spr_x + (flip_x ? spr_w - (x + 1) : x);
 			int abs_spr_y = spr_y + (flip_y ? spr_h - (y + 1) : y);
 			uint8_t bothPix = spritebuffer[COMBINED_IDX(abs_spr_x, abs_spr_y)];
@@ -165,46 +172,58 @@ void Graphics::copySpriteToScreen(
 			uint8_t rc = bothPix >> 4;

 			const int finaly = scr_y + y;
-			const int finalx0 = scr_x + (flip_x ? x + 1 : x);
-			const int finalx1 = scr_x + (flip_x ? x : x + 1);
+			int finalx = scr_x + (flip_x ? x + 1 : x);
+
+			if (x > 0 || !startWithHalf){
+				if (!(drawState.drawPaletteMap[lc] >> 4)){
+					lc = drawState.drawPaletteMap[lc] & 0x0f;
+
+					int screenPixelIdx = COMBINED_IDX(finalx, finaly);
+					if (lastScreenBuffIdx != screenPixelIdx) {
+						lastScreenBuffByte = screenBuffer[screenPixelIdx];
+						lastScreenBuffIdx = screenPixelIdx;
+					}

-			if (!(drawState.drawPaletteMap[lc] >> 4)){
-				lc = drawState.drawPaletteMap[lc] & 0x0f;
+					uint8_t source = (BITMASK(0) & finalx) == 0
+						? lastScreenBuffByte & 0x0f //just first 4 bits
+						: lastScreenBuffByte >> 4;
+
+					lc = (source & ~writeMask) | (lc & writeMask & readMask);

-				int screenPixelIdx = COMBINED_IDX(finalx0, finaly);
-				if (lastScreenBuffIdx != screenPixelIdx) {
-					lastScreenBuffByte = screenBuffer[screenPixelIdx];
-					lastScreenBuffIdx = screenPixelIdx;
+					setPixelNibble(finalx, finaly, lc, screenBuffer);
+				}
+				++x;
+				if (flip_x){
+					--finalx;
+				}
+				else {
+					++finalx;
 				}
+			}

-				uint8_t source = (BITMASK(0) & finalx0) == 0
-					? lastScreenBuffByte & 0x0f //just first 4 bits
-					: lastScreenBuffByte >> 4;
+			if (x < scr_w) {
+				if (!(drawState.drawPaletteMap[rc] >> 4)){
+					rc = drawState.drawPaletteMap[rc] & 0x0f;

-				lc = (source & ~writeMask) | (lc & writeMask & readMask);
+					int screenPixelIdx = COMBINED_IDX(finalx, finaly);
+					if (lastScreenBuffIdx != screenPixelIdx) {
+						lastScreenBuffByte = screenBuffer[screenPixelIdx];
+						lastScreenBuffIdx = screenPixelIdx;
+					}

-				setPixelNibble(finalx0, finaly, lc, screenBuffer);
-			}
+					uint8_t source = (BITMASK(0) & finalx) == 0
+						? lastScreenBuffByte & 0x0f //just first 4 bits
+						: lastScreenBuffByte >> 4;

-			if (!(drawState.drawPaletteMap[rc] >> 4)){
-				rc = drawState.drawPaletteMap[rc] & 0x0f;
+					rc = (source & ~writeMask) | (rc & writeMask & readMask);

-				int screenPixelIdx = COMBINED_IDX(finalx1, finaly);
-				if (lastScreenBuffIdx != screenPixelIdx) {
-					lastScreenBuffByte = screenBuffer[screenPixelIdx];
-					lastScreenBuffIdx = screenPixelIdx;
+					setPixelNibble(finalx, finaly, rc, screenBuffer);
 				}

-				uint8_t source = (BITMASK(0) & finalx1) == 0
-					? lastScreenBuffByte & 0x0f //just first 4 bits
-					: lastScreenBuffByte >> 4;
-
-				rc = (source & ~writeMask) | (rc & writeMask & readMask);
-
-				setPixelNibble(finalx1, finaly, rc, screenBuffer);
+				//we did two pixels so do an extra increment
+				++x;
 			}
-			//we did two pixels so do an extra increment
-			++x;
+
 		}

 	}