hedgewars/uTextures.pas
branchhedgeroid
changeset 6328 d14adf1c7721
parent 6305 5f7480c2a08d
child 6380 1ff5ad1d771b
equal deleted inserted replaced
6236:1998ff75321a 6328:d14adf1c7721
    21 unit uTextures;
    21 unit uTextures;
    22 interface
    22 interface
    23 uses SDLh, uTypes;
    23 uses SDLh, uTypes;
    24 
    24 
    25 function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
    25 function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
       
    26 procedure Surface2GrayScale(surf: PSDL_Surface);
    26 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
    27 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
    27 procedure FreeTexture(tex: PTexture);
    28 procedure FreeTexture(tex: PTexture);
    28 
    29 
    29 procedure initModule;
    30 procedure initModule;
    30 procedure freeModule;
    31 procedure freeModule;
    96 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
    97 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
    97 
    98 
    98 SetTextureParameters(true);
    99 SetTextureParameters(true);
    99 end;
   100 end;
   100 
   101 
       
   102 procedure Surface2GrayScale(surf: PSDL_Surface);
       
   103 var tw, x, y: Longword;
       
   104     fromP4: PLongWordArray;
       
   105 begin
       
   106 fromP4:= Surf^.pixels;
       
   107 for y:= 0 to Pred(Surf^.h) do
       
   108     begin
       
   109     for x:= 0 to Pred(Surf^.w) do 
       
   110         begin
       
   111         tw:= fromP4^[x];
       
   112         tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
       
   113               (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
       
   114               (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
       
   115         if tw > 255 then tw:= 255;
       
   116         tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
       
   117         fromP4^[x]:= tw;
       
   118         end;
       
   119     fromP4:= @(fromP4^[Surf^.pitch div 4])
       
   120     end;
       
   121 end;
   101 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   122 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   102 var tw, th, x, y: Longword;
   123 var tw, th, x, y: Longword;
   103     tmpp: pointer;
   124     tmpp: pointer;
   104     fromP4, toP4: PLongWordArray;
   125     fromP4, toP4: PLongWordArray;
   105 begin
   126 begin
   131 if SDL_MustLock(surf) then
   152 if SDL_MustLock(surf) then
   132     SDLTry(SDL_LockSurface(surf) >= 0, true);
   153     SDLTry(SDL_LockSurface(surf) >= 0, true);
   133 
   154 
   134 fromP4:= Surf^.pixels;
   155 fromP4:= Surf^.pixels;
   135 
   156 
   136 if cGrayScale then
   157 if cGrayScale then Surface2GrayScale(Surf);
   137     for y:= 0 to Pred(Surf^.h) do
   158 
   138         begin
       
   139         for x:= 0 to Pred(Surf^.w) do 
       
   140             begin
       
   141             tw:= fromP4^[x];
       
   142             tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
       
   143                   (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
       
   144                   (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
       
   145             if tw > 255 then tw:= 255;
       
   146             tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
       
   147             fromP4^[x]:= tw;
       
   148             end;
       
   149         fromP4:= @(fromP4^[Surf^.pitch div 4])
       
   150         end;
       
   151         
       
   152 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   159 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   153     begin
   160     begin
   154     tw:= toPowerOf2(Surf^.w);
   161     tw:= toPowerOf2(Surf^.w);
   155     th:= toPowerOf2(Surf^.h);
   162     th:= toPowerOf2(Surf^.h);
   156 
   163