hedgewars/uTextures.pas
changeset 10016 59a6d65fcb60
parent 9998 736015b847e3
child 10018 bdf75f0350bd
equal deleted inserted replaced
10014:56d2f2d5aad8 10016:59a6d65fcb60
    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 procedure Surface2GrayScale(surf: PSDL_Surface);
    27 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
    27 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
       
    28 procedure PrettifySurfaceAlpha(surf: PSDL_Surface; p: PLongwordArray);
    28 procedure FreeTexture(tex: PTexture);
    29 procedure FreeTexture(tex: PTexture);
    29 procedure FreeAndNilTexture(var tex: PTexture);
    30 procedure FreeAndNilTexture(var tex: PTexture);
    30 
    31 
    31 procedure initModule;
    32 procedure initModule;
    32 procedure freeModule;
    33 procedure freeModule;
   119         end;
   120         end;
   120     fromP4:= @(fromP4^[Surf^.pitch div 4])
   121     fromP4:= @(fromP4^[Surf^.pitch div 4])
   121     end;
   122     end;
   122 end;
   123 end;
   123 
   124 
       
   125 { this will make invisible pixels that have a visible neighbor have the
       
   126   same color as their visible neighbor, so that bilinear filtering won't
       
   127   display a "wrongly" colored border when zoomed in }
       
   128 procedure PrettifySurfaceAlpha(surf: PSDL_Surface; p: PLongwordArray);
       
   129 var
       
   130     i, lasti: Longword;
       
   131     lpi, cpi, bpi: boolean; // was last/current/bottom neighbor pixel invisible?
       
   132 begin
       
   133     lasti:= surf^.w * surf^.h - 1;
       
   134     for i:=0 to lasti do
       
   135         begin
       
   136         // use first pixel in row as starting point
       
   137         //p^[i]:= p^[i] and (BMask or GMask);
       
   138         if (i mod surf^.w) = 0 then
       
   139             lpi:= ((p^[i] and AMask) = 0)
       
   140         else
       
   141             begin
       
   142             cpi:= ((p^[i] and AMask) = 0);
       
   143             if cpi <> lpi then
       
   144                 begin
       
   145                 // invisible pixels get colors from visible neighbors
       
   146                 if (p^[i] and AMask) = 0 then
       
   147                     begin
       
   148                     p^[i]:= p^[i-1] and not AMask;
       
   149                     // as this pixel is invisible and already colored correctly now, no point in further comparing it
       
   150                     lpi:= cpi;
       
   151                     continue;
       
   152                     end
       
   153                 else
       
   154                     p^[i-1]:= p^[i] and not AMask;
       
   155                 lpi:= cpi;
       
   156                 end;
       
   157             end;
       
   158         // also check bottom neighbor, lpi is now current pixel info
       
   159         if i < lasti - surf^.w then
       
   160             begin
       
   161             bpi:= ((p^[i + surf^.w] and AMask) = 0);
       
   162             if cpi <> bpi then
       
   163                 begin
       
   164                 if cpi then
       
   165                     p^[i]:= p^[i + surf^.w] and not AMask
       
   166                 else
       
   167                     p^[i + surf^.w]:= p^[i] and not AMask;
       
   168                 end;
       
   169             end;
       
   170         end;
       
   171 end;
   124 
   172 
   125 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   173 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   126 var tw, th, x, y: Longword;
   174 var tw, th, x, y: Longword;
   127     tmpp: pointer;
   175     tmpp: pointer;
   128     fromP4, toP4: PLongWordArray;
   176     fromP4, toP4: PLongWordArray;
   146     TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   194     TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   147     Surface2Tex^.id:= 0;
   195     Surface2Tex^.id:= 0;
   148     exit
   196     exit
   149     end;
   197     end;
   150 
   198 
   151 
       
   152 glGenTextures(1, @Surface2Tex^.id);
   199 glGenTextures(1, @Surface2Tex^.id);
   153 
   200 
   154 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   201 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   155 
   202 
   156 if SDL_MustLock(surf) then
   203 if SDL_MustLock(surf) then
   158 
   205 
   159 fromP4:= Surf^.pixels;
   206 fromP4:= Surf^.pixels;
   160 
   207 
   161 if GrayScale then
   208 if GrayScale then
   162     Surface2GrayScale(Surf);
   209     Surface2GrayScale(Surf);
       
   210 
       
   211 PrettifySurfaceAlpha(surf, fromP4);
   163 
   212 
   164 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   213 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   165     begin
   214     begin
   166     tw:= toPowerOf2(Surf^.w);
   215     tw:= toPowerOf2(Surf^.w);
   167     th:= toPowerOf2(Surf^.h);
   216     th:= toPowerOf2(Surf^.h);