hedgewars/uLandObjects.pas
changeset 14947 414924c06704
parent 14944 8b53cb1c0ada
child 14981 1a2dcea1732a
equal deleted inserted replaced
14946:23fc5022bdea 14947:414924c06704
   112 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word); inline;
   112 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word); inline;
   113 begin
   113 begin
   114     BlitImageAndGenerateCollisionInfo(cpX, cpY, Width, Image, LandFlags, false);
   114     BlitImageAndGenerateCollisionInfo(cpX, cpY, Width, Image, LandFlags, false);
   115 end;
   115 end;
   116 
   116 
       
   117 function LerpByte(src, dst: Byte; l: LongWord): LongWord; inline;
       
   118 begin
       
   119     LerpByte:= ((255 - l) * src + l * dst) div 255;
       
   120 end;
       
   121 
   117 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word; Flip: boolean);
   122 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word; Flip: boolean);
   118 var p: PLongwordArray;
   123 var p: PLongwordArray;
   119     px, x, y: Longword;
   124     pLandColor: PLongWord;
       
   125     alpha, color, landColor, x, y: LongWord;
   120     bpp: LongInt;
   126     bpp: LongInt;
   121 begin
   127 begin
   122 WriteToConsole('Generating collision info... ');
   128 WriteToConsole('Generating collision info... ');
   123 
   129 
   124 if SDL_MustLock(Image) then
   130 if SDL_MustLock(Image) then
   140     begin
   146     begin
   141     for x:= 0 to Pred(Width) do
   147     for x:= 0 to Pred(Width) do
   142         begin
   148         begin
   143         // map image pixels per line backwards if in flip mode
   149         // map image pixels per line backwards if in flip mode
   144         if Flip then
   150         if Flip then
   145             px:= Pred(Image^.w) - x
   151             color:= p^[Pred(Image^.w) - x]
   146         else
   152         else
   147             px:= x;
   153             color:= p^[x];
   148 
   154 
   149         if (p^[px] and AMask) <> 0 then
   155         if (cReducedQuality and rqBlurryLand) = 0 then
   150             begin
   156             pLandColor:= @LandPixels[cpY + y, cpX + x]
   151             if (cReducedQuality and rqBlurryLand) = 0 then
   157         else
   152                 begin
   158             pLandColor:= @LandPixels[(cpY + y) div 2, (cpX + x) div 2];
   153                 if (LandPixels[cpY + y, cpX + x] = 0)
   159 
   154                 or (((LandPixels[cpY + y, cpX + x] and AMask) shr AShift) < 255) then
   160         landColor:= pLandColor^;
   155                     LandPixels[cpY + y, cpX + x]:= p^[px];
   161         alpha:= (landColor and AMask) shr AShift;
   156                 end
   162 
       
   163         if ((color and AMask) <> 0) and (alpha <> 255)  then
       
   164             begin
       
   165             if alpha = 0 then
       
   166                 pLandColor^:= color
   157             else
   167             else
   158                 if LandPixels[(cpY + y) div 2, (cpX + x) div 2] = 0 then
   168                 pLandColor^:=
   159                     LandPixels[(cpY + y) div 2, (cpX + x) div 2]:= p^[px];
   169                    (LerpByte((color and RMask) shr RShift, (landColor and RMask) shr RShift, alpha) shl RShift)
       
   170                     or (LerpByte((color and GMask) shr GShift, (landColor and GMask) shr GShift, alpha) shl GShift)
       
   171                     or (LerpByte((color and BMask) shr BShift, (landColor and BMask) shr BShift, alpha) shl BShift)
       
   172                     or (LerpByte(alpha, 255, (color and AMask) shr AShift) shl AShift);
   160 
   173 
   161             if Land[cpY + y, cpX + x] <= lfAllObjMask then
   174             if Land[cpY + y, cpX + x] <= lfAllObjMask then
   162                 Land[cpY + y, cpX + x]:= lfObject or LandFlags
   175                 Land[cpY + y, cpX + x]:= lfObject or LandFlags
   163             end;
   176             end;
   164         end;
   177         end;
   166     end;
   179     end;
   167 
   180 
   168 if SDL_MustLock(Image) then
   181 if SDL_MustLock(Image) then
   169     SDL_UnlockSurface(Image);
   182     SDL_UnlockSurface(Image);
   170 WriteLnToConsole(msgOK)
   183 WriteLnToConsole(msgOK)
   171 end;
       
   172 
       
   173 function LerpByte(src, dst: Byte; l: LongWord): LongWord; inline;
       
   174 begin
       
   175     LerpByte:= ((255 - l) * src + l * dst) div 255;
       
   176 end;
   184 end;
   177 
   185 
   178 procedure BlitOverlayAndGenerateCollisionInfo(cpX, cpY: Longword; Image: PSDL_Surface);
   186 procedure BlitOverlayAndGenerateCollisionInfo(cpX, cpY: Longword; Image: PSDL_Surface);
   179 var p: PLongwordArray;
   187 var p: PLongwordArray;
   180     pLandColor: PLongWord;
   188     pLandColor: PLongWord;
   227 WriteLnToConsole(msgOK)
   235 WriteLnToConsole(msgOK)
   228 end;
   236 end;
   229 
   237 
   230 procedure BlitImageUsingMask(cpX, cpY: Longword;  Image, Mask: PSDL_Surface);
   238 procedure BlitImageUsingMask(cpX, cpY: Longword;  Image, Mask: PSDL_Surface);
   231 var p, mp: PLongwordArray;
   239 var p, mp: PLongwordArray;
   232     x, y: Longword;
   240     pLandColor: PLongWord;
       
   241     alpha, color, landColor, x, y: Longword;
   233     bpp: LongInt;
   242     bpp: LongInt;
   234 begin
   243 begin
   235 WriteToConsole('Generating collision info... ');
   244 WriteToConsole('Generating collision info... ');
   236 
   245 
   237 if SDL_MustLock(Image) then
   246 if SDL_MustLock(Image) then
   248 mp:= Mask^.pixels;
   257 mp:= Mask^.pixels;
   249 for y:= 0 to Pred(Image^.h) do
   258 for y:= 0 to Pred(Image^.h) do
   250     begin
   259     begin
   251     for x:= 0 to Pred(Image^.w) do
   260     for x:= 0 to Pred(Image^.w) do
   252         begin
   261         begin
       
   262         color:= p^[x];
       
   263 
   253         if (cReducedQuality and rqBlurryLand) = 0 then
   264         if (cReducedQuality and rqBlurryLand) = 0 then
   254             begin
   265             pLandColor:= @LandPixels[cpY + y, cpX + x]
   255             if (LandPixels[cpY + y, cpX + x] = 0)
       
   256             or (((p^[x] and AMask) <> 0) and (((LandPixels[cpY + y, cpX + x] and AMask) shr AShift) < 255)) then
       
   257                 LandPixels[cpY + y, cpX + x]:= p^[x];
       
   258             end
       
   259         else
   266         else
   260             if LandPixels[(cpY + y) div 2, (cpX + x) div 2] = 0 then
   267             pLandColor:= @LandPixels[(cpY + y) div 2, (cpX + x) div 2];
   261                 LandPixels[(cpY + y) div 2, (cpX + x) div 2]:= p^[x];
   268 
       
   269         landColor:= pLandColor^;
       
   270         alpha:= (landColor and AMask) shr AShift;
       
   271 
       
   272         if ((color and AMask) <> 0) and (alpha <> 255)  then
       
   273         begin
       
   274             if alpha = 0 then
       
   275                 pLandColor^:= color
       
   276             else
       
   277                 pLandColor^:=
       
   278                    (LerpByte((color and RMask) shr RShift, (landColor and RMask) shr RShift, alpha) shl RShift)
       
   279                    or (LerpByte((color and GMask) shr GShift, (landColor and GMask) shr GShift, alpha) shl GShift)
       
   280                    or (LerpByte((color and BMask) shr BShift, (landColor and BMask) shr BShift, alpha) shl BShift)
       
   281                    or (LerpByte(alpha, 255, (color and AMask) shr AShift) shl AShift);
       
   282         end;
   262 
   283 
   263         if (Land[cpY + y, cpX + x] <= lfAllObjMask) or (Land[cpY + y, cpX + x] and lfObject <> 0)  then
   284         if (Land[cpY + y, cpX + x] <= lfAllObjMask) or (Land[cpY + y, cpX + x] and lfObject <> 0)  then
   264             SetLand(Land[cpY + y, cpX + x], mp^[x]);
   285             SetLand(Land[cpY + y, cpX + x], mp^[x]);
   265         end;
   286         end;
       
   287 
   266     p:= PLongwordArray(@(p^[Image^.pitch shr 2]));
   288     p:= PLongwordArray(@(p^[Image^.pitch shr 2]));
   267     mp:= PLongwordArray(@(mp^[Mask^.pitch shr 2]))
   289     mp:= PLongwordArray(@(mp^[Mask^.pitch shr 2]))
   268     end;
   290     end;
   269 
   291 
   270 if SDL_MustLock(Image) then
   292 if SDL_MustLock(Image) then