hedgewars/uTextures.pas
changeset 10016 59a6d65fcb60
parent 9998 736015b847e3
child 10018 bdf75f0350bd
--- a/hedgewars/uTextures.pas	Sun Jan 19 00:18:28 2014 +0400
+++ b/hedgewars/uTextures.pas	Sun Jan 19 13:41:11 2014 +0100
@@ -25,6 +25,7 @@
 function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
 procedure Surface2GrayScale(surf: PSDL_Surface);
 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
+procedure PrettifySurfaceAlpha(surf: PSDL_Surface; p: PLongwordArray);
 procedure FreeTexture(tex: PTexture);
 procedure FreeAndNilTexture(var tex: PTexture);
 
@@ -121,6 +122,53 @@
     end;
 end;
 
+{ this will make invisible pixels that have a visible neighbor have the
+  same color as their visible neighbor, so that bilinear filtering won't
+  display a "wrongly" colored border when zoomed in }
+procedure PrettifySurfaceAlpha(surf: PSDL_Surface; p: PLongwordArray);
+var
+    i, lasti: Longword;
+    lpi, cpi, bpi: boolean; // was last/current/bottom neighbor pixel invisible?
+begin
+    lasti:= surf^.w * surf^.h - 1;
+    for i:=0 to lasti do
+        begin
+        // use first pixel in row as starting point
+        //p^[i]:= p^[i] and (BMask or GMask);
+        if (i mod surf^.w) = 0 then
+            lpi:= ((p^[i] and AMask) = 0)
+        else
+            begin
+            cpi:= ((p^[i] and AMask) = 0);
+            if cpi <> lpi then
+                begin
+                // invisible pixels get colors from visible neighbors
+                if (p^[i] and AMask) = 0 then
+                    begin
+                    p^[i]:= p^[i-1] and not AMask;
+                    // as this pixel is invisible and already colored correctly now, no point in further comparing it
+                    lpi:= cpi;
+                    continue;
+                    end
+                else
+                    p^[i-1]:= p^[i] and not AMask;
+                lpi:= cpi;
+                end;
+            end;
+        // also check bottom neighbor, lpi is now current pixel info
+        if i < lasti - surf^.w then
+            begin
+            bpi:= ((p^[i + surf^.w] and AMask) = 0);
+            if cpi <> bpi then
+                begin
+                if cpi then
+                    p^[i]:= p^[i + surf^.w] and not AMask
+                else
+                    p^[i + surf^.w]:= p^[i] and not AMask;
+                end;
+            end;
+        end;
+end;
 
 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
 var tw, th, x, y: Longword;
@@ -148,7 +196,6 @@
     exit
     end;
 
-
 glGenTextures(1, @Surface2Tex^.id);
 
 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
@@ -161,6 +208,8 @@
 if GrayScale then
     Surface2GrayScale(Surf);
 
+PrettifySurfaceAlpha(surf, fromP4);
+
 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
     begin
     tw:= toPowerOf2(Surf^.w);