hedgewars/uTextures.pas
changeset 10018 bdf75f0350bd
parent 10016 59a6d65fcb60
child 10021 7f36194af01c
equal deleted inserted replaced
10016:59a6d65fcb60 10018:bdf75f0350bd
    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 PrettifySurfaceAlpha(surf: PSDL_Surface; pixels: PLongwordArray);
       
    29 procedure PrettifyAlpha2D(pixels: TLandArray; height, width: LongWord);
    29 procedure FreeTexture(tex: PTexture);
    30 procedure FreeTexture(tex: PTexture);
    30 procedure FreeAndNilTexture(var tex: PTexture);
    31 procedure FreeAndNilTexture(var tex: PTexture);
    31 
    32 
    32 procedure initModule;
    33 procedure initModule;
    33 procedure freeModule;
    34 procedure freeModule;
   123 end;
   124 end;
   124 
   125 
   125 { this will make invisible pixels that have a visible neighbor have the
   126 { 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   same color as their visible neighbor, so that bilinear filtering won't
   127   display a "wrongly" colored border when zoomed in }
   128   display a "wrongly" colored border when zoomed in }
   128 procedure PrettifySurfaceAlpha(surf: PSDL_Surface; p: PLongwordArray);
   129 procedure PrettifyAlpha(row1, row2: PLongwordArray; firsti, lasti: LongWord);
   129 var
   130 var
   130     i, lasti: Longword;
   131     i: Longword;
   131     lpi, cpi, bpi: boolean; // was last/current/bottom neighbor pixel invisible?
   132     lpi, cpi, bpi: boolean; // was last/current/bottom neighbor pixel invisible?
   132 begin
   133 begin
   133     lasti:= surf^.w * surf^.h - 1;
   134     for i:=firsti to lasti do
   134     for i:=0 to lasti do
   135         begin
   135         begin
   136         // use first pixel in row1 as starting point
   136         // use first pixel in row as starting point
   137         if i = firsti then
   137         //p^[i]:= p^[i] and (BMask or GMask);
   138             lpi:= ((row1^[i] and AMask) = 0)
   138         if (i mod surf^.w) = 0 then
       
   139             lpi:= ((p^[i] and AMask) = 0)
       
   140         else
   139         else
   141             begin
   140             begin
   142             cpi:= ((p^[i] and AMask) = 0);
   141             cpi:= ((row1^[i] and AMask) = 0);
   143             if cpi <> lpi then
   142             if cpi <> lpi then
   144                 begin
   143                 begin
   145                 // invisible pixels get colors from visible neighbors
   144                 // invisible pixels get colors from visible neighbors
   146                 if (p^[i] and AMask) = 0 then
   145                 if (row1^[i] and AMask) = 0 then
   147                     begin
   146                     begin
   148                     p^[i]:= p^[i-1] and not AMask;
   147                     row1^[i]:= row1^[i-1] and not AMask;
   149                     // as this pixel is invisible and already colored correctly now, no point in further comparing it
   148                     // as this pixel is invisible and already colored correctly now, no point in further comparing it
   150                     lpi:= cpi;
   149                     lpi:= cpi;
   151                     continue;
   150                     continue;
   152                     end
   151                     end
   153                 else
   152                 else
   154                     p^[i-1]:= p^[i] and not AMask;
   153                     row1^[i-1]:= row1^[i] and not AMask;
   155                 lpi:= cpi;
   154                 lpi:= cpi;
   156                 end;
   155                 end;
   157             end;
   156             end;
   158         // also check bottom neighbor, lpi is now current pixel info
   157         // also check bottom neighbor, lpi is now current pixel info
   159         if i < lasti - surf^.w then
   158         if row2 <> nil then
   160             begin
   159             begin
   161             bpi:= ((p^[i + surf^.w] and AMask) = 0);
   160             bpi:= ((row2^[i] and AMask) = 0);
   162             if cpi <> bpi then
   161             if cpi <> bpi then
   163                 begin
   162                 begin
   164                 if cpi then
   163                 if cpi then
   165                     p^[i]:= p^[i + surf^.w] and not AMask
   164                     row1^[i]:= row2^[i] and not AMask
   166                 else
   165                 else
   167                     p^[i + surf^.w]:= p^[i] and not AMask;
   166                     row2^[i]:= row1^[i] and not AMask;
   168                 end;
   167                 end;
   169             end;
   168             end;
   170         end;
   169         end;
       
   170 end;
       
   171 
       
   172 procedure PrettifySurfaceAlpha(surf: PSDL_Surface; pixels: PLongwordArray);
       
   173 var
       
   174     // current row index, second last row index of array, width and first/last i of row
       
   175     r, slr, w, si, li: LongWord;
       
   176 begin
       
   177     w:= surf^.w;
       
   178     slr:= surf^.h - 2;
       
   179     si:= 0;
       
   180     li:= w - 1;
       
   181     for r:= 0 to slr do
       
   182         begin
       
   183         PrettifyAlpha(pixels, pixels, si, li);
       
   184         // move indices to next row
       
   185         si:= si + w;
       
   186         li:= li + w;
       
   187         end;
       
   188     // don't forget last row
       
   189     PrettifyAlpha(pixels, nil, si, li);
       
   190 end;
       
   191 
       
   192 procedure PrettifyAlpha2D(pixels: TLandArray; height, width: LongWord);
       
   193 var
       
   194     // current y; last x, second last y of array;
       
   195     y, lx, sly: LongWord;
       
   196 begin
       
   197     sly:= height - 2;
       
   198     lx:= width - 1;
       
   199     for y:= 0 to sly do
       
   200         begin
       
   201         PrettifyAlpha(PLongWordArray(pixels[y]), PLongWordArray(pixels[y+1]), 0, lx);
       
   202         end;
       
   203     // don't forget last row
       
   204     PrettifyAlpha(PLongWordArray(pixels[sly+1]), nil, 0, lx);
   171 end;
   205 end;
   172 
   206 
   173 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   207 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   174 var tw, th, x, y: Longword;
   208 var tw, th, x, y: Longword;
   175     tmpp: pointer;
   209     tmpp: pointer;