From bafcee7b1879b61e66302110aecf54f62b672325 Mon Sep 17 00:00:00 2001
From: Jeffrey Pfau <jeffrey@endrift.com>
Date: Thu, 17 Sep 2015 19:42:09 -0700
Subject: [PATCH] GBA Video: Fix regression when fixing sprite blending

---
 src/gba/renderers/software-private.h | 8 ++++----
 src/gba/renderers/video-software.c   | 4 ++--
 src/gba/renderers/video-software.h   | 1 +
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/gba/renderers/software-private.h b/src/gba/renderers/software-private.h
index 4d4efed5d..b24ffdaf4 100644
--- a/src/gba/renderers/software-private.h
+++ b/src/gba/renderers/software-private.h
@@ -42,7 +42,7 @@ static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* render
 		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 			color = _mix(renderer->blda, current, renderer->bldb, color);
 		} else {
-			color = current & (0x00FFFFFF | FLAG_TARGET_1);
+			color = (current & 0x00FFFFFF) | ((current >> 1) & FLAG_REBLEND);
 		}
 	} else {
 		color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
@@ -55,7 +55,7 @@ static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* rend
 		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 			color = _mix(renderer->blda, current, renderer->bldb, color);
 		} else {
-			color = current & (0x00FFFFFF | FLAG_TARGET_1);
+			color = (current & 0x00FFFFFF) | ((current >> 1) & FLAG_REBLEND);
 		}
 	} else {
 		color = color & ~FLAG_TARGET_2;
@@ -69,7 +69,7 @@ static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* rend
 	if (color < current) {
 		color |= (current & FLAG_OBJWIN);
 	} else {
-		color = current & (0x00FFFFFF | FLAG_TARGET_1);
+		color = (current & 0x00FFFFFF) | ((current >> 1) & FLAG_REBLEND);
 	}
 	*pixel = color;
 }
@@ -78,7 +78,7 @@ static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* re
                                              uint32_t current) {
 	UNUSED(renderer);
 	if (color >= current) {
-		color = current & (0x00FFFFFF | FLAG_TARGET_1);
+		color = (current & 0x00FFFFFF) | ((current >> 1) & FLAG_REBLEND);
 	}
 	*pixel = color;
 }
diff --git a/src/gba/renderers/video-software.c b/src/gba/renderers/video-software.c
index 81dd0b560..cf8cf6522 100644
--- a/src/gba/renderers/video-software.c
+++ b/src/gba/renderers/video-software.c
@@ -548,14 +548,14 @@ static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* render
 			if (softwareRenderer->blendEffect == BLEND_DARKEN) {
 				for (; x < end; ++x) {
 					uint32_t color = softwareRenderer->row[x];
-					if (color & FLAG_TARGET_1 && !(color & FLAG_UNWRITTEN)) {
+					if ((color & 0xFF000000) == FLAG_REBLEND) {
 						softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
 					}
 				}
 			} else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
 				for (; x < end; ++x) {
 					uint32_t color = softwareRenderer->row[x];
-					if (color & FLAG_TARGET_1 && !(color & FLAG_UNWRITTEN)) {
+					if ((color & 0xFF000000) == FLAG_REBLEND) {
 						softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
 					}
 				}
diff --git a/src/gba/renderers/video-software.h b/src/gba/renderers/video-software.h
index f88d4d840..a213d32a9 100644
--- a/src/gba/renderers/video-software.h
+++ b/src/gba/renderers/video-software.h
@@ -74,6 +74,7 @@ enum {
 #define FLAG_TARGET_1       0x02000000
 #define FLAG_TARGET_2       0x01000000
 #define FLAG_OBJWIN         0x01000000
+#define FLAG_REBLEND        0x01000000
 #define FLAG_ORDER_MASK     0xF8000000
 
 #define IS_WRITABLE(PIXEL) ((PIXEL) & 0xFE000000)