From 5ba71d2bc039be58efe8a5c7f9ffa6b1798626d6 Mon Sep 17 00:00:00 2001 From: Max CD Date: Mon, 2 May 2022 15:33:13 +0100 Subject: [PATCH] Fix sprite alpha blending for painting on transparent pixels --- olcPixelGameEngine.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/olcPixelGameEngine.h b/olcPixelGameEngine.h index fd077c4..b3b4bb6 100644 --- a/olcPixelGameEngine.h +++ b/olcPixelGameEngine.h @@ -1898,10 +1898,15 @@ namespace olc { Pixel d = pDrawTarget->GetPixel(x, y); float a = (float)(p.a / 255.0f) * fBlendFactor; - float c = 1.0f - a; - float r = a * (float)p.r + c * (float)d.r; - float g = a * (float)p.g + c * (float)d.g; - float b = a * (float)p.b + c * (float)d.b; + float a2 = (float)(d.a / 255.0f); + float a3 = a + a2 * (1.0f - a); + float s = a3 > 0 ? 1 / a3 : 1; + float c = (1.0f - a) * a2; + + float r = (a * (float)p.r + c * (float)d.r) * s; + float g = (a * (float)p.g + c * (float)d.g) * s; + float b = (a * (float)p.b + c * (float)d.b) * s; + return pDrawTarget->SetPixel(x, y, Pixel((uint8_t)r, (uint8_t)g, (uint8_t)b/*, (uint8_t)(p.a * fBlendFactor)*/)); }