hedgewars/uTextures.pas
changeset 6303 3edb3c857995
parent 5441 39962b855540
child 6305 5f7480c2a08d
--- a/hedgewars/uTextures.pas	Sun Nov 06 12:40:35 2011 -0500
+++ b/hedgewars/uTextures.pas	Sun Nov 06 13:00:25 2011 -0500
@@ -23,6 +23,7 @@
 uses SDLh, uTypes;
 
 function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
+procedure Surface2GrayScale(surf: PSDL_Surface);
 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
 procedure FreeTexture(tex: PTexture);
 
@@ -98,6 +99,27 @@
 SetTextureParameters(true);
 end;
 
+procedure Surface2GrayScale(surf: PSDL_Surface);
+var tw, th, x, y: Longword;
+    tmpp: pointer;
+    fromP4: PLongWordArray;
+begin
+fromP4:= Surf^.pixels;
+for y:= 0 to Pred(Surf^.h) do
+    begin
+    for x:= 0 to Pred(Surf^.w) do 
+        begin
+        tw:= fromP4^[x];
+        tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
+              (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
+              (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
+        if tw > 255 then tw:= 255;
+        tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
+        fromP4^[x]:= tw;
+        end;
+    fromP4:= @(fromP4^[Surf^.pitch div 4])
+    end;
+end;
 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
 var tw, th, x, y: Longword;
     tmpp: pointer;
@@ -133,22 +155,8 @@
 
 fromP4:= Surf^.pixels;
 
-if cGrayScale then
-    for y:= 0 to Pred(Surf^.h) do
-        begin
-        for x:= 0 to Pred(Surf^.w) do 
-            begin
-            tw:= fromP4^[x];
-            tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
-                  (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
-                  (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
-            if tw > 255 then tw:= 255;
-            tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
-            fromP4^[x]:= tw;
-            end;
-        fromP4:= @(fromP4^[Surf^.pitch div 4])
-        end;
-        
+if cGrayScale then Surface2GrayScale(Surf);
+
 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
     begin
     tw:= toPowerOf2(Surf^.w);