Engine:
authorsmxx
Thu, 15 Apr 2010 14:24:26 +0000
changeset 3341 3de60a5986e9
parent 3340 96dd168b080b
child 3342 b4f01613dcd7
Engine: * Added real alpha blending to CopyToXY
hedgewars/SDLh.pas
hedgewars/uStore.pas
--- a/hedgewars/SDLh.pas	Tue Apr 13 22:45:46 2010 +0000
+++ b/hedgewars/SDLh.pas	Thu Apr 15 14:24:26 2010 +0000
@@ -617,6 +617,7 @@
 function  SDL_Flip(Screen: PSDL_Surface): LongInt; cdecl; external SDLLibName;
 
 procedure SDL_GetRGB(pixel: Longword; fmt: PSDL_PixelFormat; r, g, b: PByte); cdecl; external SDLLibName;
+procedure SDL_GetRGBA(pixel: Longword; fmt: PSDL_PixelFormat; r, g, b, a: PByte); cdecl; external SDLLibName;
 function  SDL_MapRGB(format: PSDL_PixelFormat; r, g, b: Byte): Longword; cdecl; external SDLLibName;
 function  SDL_MapRGBA(format: PSDL_PixelFormat; r, g, b, a: Byte): Longword; cdecl; external SDLLibName;
 
--- a/hedgewars/uStore.pas	Tue Apr 13 22:45:46 2010 +0000
+++ b/hedgewars/uStore.pas	Thu Apr 15 14:24:26 2010 +0000
@@ -1310,6 +1310,7 @@
 procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt);
 var srcX, srcY, i, j, maxDest: LongInt;
     srcPixels, destPixels: PLongWordArray;
+    r0, g0, b0, a0, r1, g1, b1, a1: Byte;
 begin
 maxDest:= (dest^.pitch div 4) * dest^.h;
 srcPixels:= src^.pixels;
@@ -1320,8 +1321,12 @@
       begin
       i:= (destY + srcY) * (dest^.pitch div 4) + destX + srcX;
       j:= srcY * (src^.pitch div 4) + srcX;
-      // basic skip of transparent pixels - cleverness would be to do true alpha
-      if (i < maxDest) and (AMask and srcPixels^[j] <> 0) then destPixels^[i]:= srcPixels^[j];
+      if (i < maxDest) and (srcPixels^[j] and AMask <> 0) then
+         begin
+         SDL_GetRGBA(destPixels^[i], dest^.format, @r0, @g0, @b0, @a0);
+         SDL_GetRGBA(srcPixels^[j], src^.format, @r1, @g1, @b1, @a1);
+         destPixels^[i]:= SDL_MapRGBA(dest^.format, (r0 * (255 - a1) + r1 * a1) div 255, (g0 * (255 - a1) + g1 * a1) div 255, (b0 * (255 - a1) + b1 * a1) div 255, (a0 * (255 - a1) + a1 * a1) div 255);
+         end;
       end;
 end;