hedgewars/uLandObjects.pas
branchtransitional_engine
changeset 15900 128ace913837
parent 15360 34eb5cc72241
child 15901 f39f0f614dbf
equal deleted inserted replaced
15899:73cdc306888f 15900:128ace913837
    23 uses SDLh;
    23 uses SDLh;
    24 
    24 
    25 procedure AddObjects();
    25 procedure AddObjects();
    26 procedure FreeLandObjects();
    26 procedure FreeLandObjects();
    27 procedure LoadThemeConfig;
    27 procedure LoadThemeConfig;
    28 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface); inline;
    28 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface);
    29 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word); inline;
    29 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word);
    30 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word; Flip: boolean);
    30 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface; LandFlags: Word; Flip: boolean);
    31 procedure BlitOverlayAndGenerateCollisionInfo(cpX, cpY: Longword; Image: PSDL_Surface);
    31 procedure BlitOverlayAndGenerateCollisionInfo(cpX, cpY: Longword; Image: PSDL_Surface);
    32 procedure BlitImageUsingMask(cpX, cpY: Longword;  Image, Mask: PSDL_Surface);
    32 procedure BlitImageUsingMask(cpX, cpY: Longword;  Image, Mask: PSDL_Surface);
    33 procedure AddOnLandObjects(Surface: PSDL_Surface);
    33 procedure AddOnLandObjects(Surface: PSDL_Surface);
    34 procedure SetLand(var LandWord: Word; Pixel: LongWord); inline;
    34 procedure SetLand(y, x: LongInt; Pixel: LongWord);
    35 
    35 
    36 implementation
    36 implementation
    37 uses uStore, uConsts, uConsole, uRandom, uSound
    37 uses uStore, uConsts, uConsole, uRandom, uSound
    38      , uTypes, uVariables, uDebug, uUtils
    38      , uTypes, uVariables, uDebug, uUtils
    39      , uPhysFSLayer, uRenderUtils;
    39      , uPhysFSLayer, uRenderUtils, uLandUtils;
    40 
    40 
    41 const MaxRects = 512;
    41 const MaxRects = 512;
    42       MAXOBJECTRECTS = 16;
    42       MAXOBJECTRECTS = 16;
    43       MAXTHEMEOBJECTS = 32;
    43       MAXTHEMEOBJECTS = 32;
    44       cThemeCFGFilename = 'theme.cfg';
    44       cThemeCFGFilename = 'theme.cfg';
    82 var Rects: PRectArray;
    82 var Rects: PRectArray;
    83     RectCount: Longword;
    83     RectCount: Longword;
    84     ThemeObjects: TThemeObjects;
    84     ThemeObjects: TThemeObjects;
    85     SprayObjects: TSprayObjects;
    85     SprayObjects: TSprayObjects;
    86 
    86 
    87 procedure SetLand(var LandWord: Word; Pixel: LongWord); inline;
    87 procedure SetLand(y, x: LongInt; Pixel: LongWord);
    88 begin
    88 begin
    89     // this an if instead of masking colours to avoid confusing map creators
    89     // this an if instead of masking colours to avoid confusing map creators
    90     if ((AMask and Pixel) = 0) then
    90     if ((AMask and Pixel) = 0) then
    91         LandWord:= 0
    91         LandSet(y, x, 0)
    92     else if (Pixel and AMask > 0) and (Pixel and RMask > 0) and (Pixel and GMask > 0) and (Pixel and BMask > 0) then // whiteish
    92     else if (Pixel and AMask > 0) and (Pixel and RMask > 0) and (Pixel and GMask > 0) and (Pixel and BMask > 0) then // whiteish
    93         LandWord:= lfObject
    93         LandSet(y, x, lfObject)
    94     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask = 0) and (Pixel and BMask = 0) then // blackish
    94     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask = 0) and (Pixel and BMask = 0) then // blackish
    95         begin
    95         begin
    96         LandWord:= lfBasic;
    96         LandSet(y, x, lfBasic);
    97         disableLandBack:= false
    97         disableLandBack:= false
    98         end
    98         end
    99     else if (Pixel and AMask > 0) and (Pixel and RMask > 0) and (Pixel and GMask = 0) and (Pixel and BMask = 0) then // reddish
    99     else if (Pixel and AMask > 0) and (Pixel and RMask > 0) and (Pixel and GMask = 0) and (Pixel and BMask = 0) then // reddish
   100         LandWord:= lfIndestructible
   100         LandSet(y, x, lfIndestructible)
   101     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask = 0) and (Pixel and BMask > 0) then // blueish
   101     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask = 0) and (Pixel and BMask > 0) then // blueish
   102         LandWord:= lfObject or lfIce
   102         LandSet(y, x, lfObject or lfIce)
   103     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask > 0) and (Pixel and BMask = 0) then // greenish
   103     else if (Pixel and AMask > 0) and (Pixel and RMask = 0) and (Pixel and GMask > 0) and (Pixel and BMask = 0) then // greenish
   104         LandWord:= lfObject or lfBouncy
   104         LandSet(y, x, lfObject or lfBouncy)
   105 end;
   105 end;
   106 
   106 
   107 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface); inline;
   107 procedure BlitImageAndGenerateCollisionInfo(cpX, cpY, Width: Longword; Image: PSDL_Surface);
   108 begin
   108 begin
   109     BlitImageAndGenerateCollisionInfo(cpX, cpY, Width, Image, 0, false);
   109     BlitImageAndGenerateCollisionInfo(cpX, cpY, Width, Image, 0, false);
   110 end;
   110 end;
   111 
   111 
   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);
   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;
   117 function LerpByte(src, dst: Byte; l: LongWord): LongWord;
   118 begin
   118 begin
   119     LerpByte:= ((255 - l) * src + l * dst) div 255;
   119     LerpByte:= ((255 - l) * src + l * dst) div 255;
   120 end;
   120 end;
   121 
   121 
   122 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);
   171                     or (LerpByte((color and BMask) shr BShift, (landColor and BMask) shr BShift, alpha) shl BShift)
   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);
   172                     or (LerpByte(alpha, 255, (color and AMask) shr AShift) shl AShift);
   173 
   173 
   174             end;
   174             end;
   175 
   175 
   176         if ((color and AMask) <> 0) and (Land[cpY + y, cpX + x] <= lfAllObjMask) then
   176         if ((color and AMask) <> 0) and (LandGet(cpY + y, cpX + x) <= lfAllObjMask) then
   177             Land[cpY + y, cpX + x]:= lfObject or LandFlags
   177             LandSet(cpY + y, cpX + x, lfObject or LandFlags)
   178         end;
   178         end;
   179     p:= PLongwordArray(@(p^[Image^.pitch shr 2]))
   179     p:= PLongwordArray(@(p^[Image^.pitch shr 2]))
   180     end;
   180     end;
   181 
   181 
   182 if SDL_MustLock(Image) then
   182 if SDL_MustLock(Image) then
   222                  or (LerpByte((landColor and BMask) shr BShift, (color and BMask) shr BShift, alpha) shl BShift)
   222                  or (LerpByte((landColor and BMask) shr BShift, (color and BMask) shr BShift, alpha) shl BShift)
   223                  or (LerpByte(alpha, 255, (landColor and AMask) shr AShift) shl AShift)
   223                  or (LerpByte(alpha, 255, (landColor and AMask) shr AShift) shl AShift)
   224                 end;
   224                 end;
   225             pLandColor^:= color;
   225             pLandColor^:= color;
   226 
   226 
   227             if Land[cpY + y, cpX + x] <= lfAllObjMask then
   227             if LandGet(cpY + y, cpX + x) <= lfAllObjMask then
   228                 Land[cpY + y, cpX + x]:= lfObject
   228                 LandSet(cpY + y, cpX + x, lfObject)
   229             end;
   229             end;
   230         end;
   230         end;
   231     p:= PLongwordArray(@(p^[Image^.pitch shr 2]))
   231     p:= PLongwordArray(@(p^[Image^.pitch shr 2]))
   232     end;
   232     end;
   233 
   233 
   280                    or (LerpByte((color and GMask) shr GShift, (landColor and GMask) shr GShift, alpha) shl GShift)
   280                    or (LerpByte((color and GMask) shr GShift, (landColor and GMask) shr GShift, alpha) shl GShift)
   281                    or (LerpByte((color and BMask) shr BShift, (landColor and BMask) shr BShift, alpha) shl BShift)
   281                    or (LerpByte((color and BMask) shr BShift, (landColor and BMask) shr BShift, alpha) shl BShift)
   282                    or (LerpByte(alpha, 255, (color and AMask) shr AShift) shl AShift);
   282                    or (LerpByte(alpha, 255, (color and AMask) shr AShift) shl AShift);
   283         end;
   283         end;
   284 
   284 
   285         if (Land[cpY + y, cpX + x] <= lfAllObjMask) or (Land[cpY + y, cpX + x] and lfObject <> 0)  then
   285         if (LandGet(cpY + y, cpX + x) <= lfAllObjMask) or (LandGet(cpY + y, cpX + x) and lfObject <> 0)  then
   286             SetLand(Land[cpY + y, cpX + x], mp^[x]);
   286             SetLand(cpY + y, cpX + x, mp^[x]);
   287         end;
   287         end;
   288 
   288 
   289     p:= PLongwordArray(@(p^[Image^.pitch shr 2]));
   289     p:= PLongwordArray(@(p^[Image^.pitch shr 2]));
   290     mp:= PLongwordArray(@(mp^[Mask^.pitch shr 2]))
   290     mp:= PLongwordArray(@(mp^[Mask^.pitch shr 2]))
   291     end;
   291     end;
   339 var i: LongInt;
   339 var i: LongInt;
   340     lRes: Longword;
   340     lRes: Longword;
   341 begin
   341 begin
   342     lRes:= 0;
   342     lRes:= 0;
   343     for i:= y to Pred(y + h) do
   343     for i:= y to Pred(y + h) do
   344         if Land[i, x] <> 0 then
   344         if LandGet(i, x) <> 0 then
   345             inc(lRes);
   345             inc(lRes);
   346     CountNonZeroz:= lRes;
   346     CountNonZeroz:= lRes;
   347 end;
   347 end;
   348 
   348 
   349 function AddGirder(gX: LongInt; var girSurf: PSDL_Surface): boolean;
   349 function AddGirder(gX: LongInt; var girSurf: PSDL_Surface): boolean;
   423 tmpx2:= bx;
   423 tmpx2:= bx;
   424 while (tmpx <= bx - rect.w div 2 - 1) and bRes do
   424 while (tmpx <= bx - rect.w div 2 - 1) and bRes do
   425     begin
   425     begin
   426     bRes:= ((rect.y and LAND_HEIGHT_MASK) = 0) and ((by and LAND_HEIGHT_MASK) = 0)
   426     bRes:= ((rect.y and LAND_HEIGHT_MASK) = 0) and ((by and LAND_HEIGHT_MASK) = 0)
   427     and ((tmpx and LAND_WIDTH_MASK) = 0) and ((tmpx2 and LAND_WIDTH_MASK) = 0)
   427     and ((tmpx and LAND_WIDTH_MASK) = 0) and ((tmpx2 and LAND_WIDTH_MASK) = 0)
   428     and (Land[rect.y, tmpx] = Color) and (Land[by, tmpx] = Color)
   428     and (LandGet(rect.y, tmpx) = Color) and (LandGet(by, tmpx) = Color)
   429     and (Land[rect.y, tmpx2] = Color) and (Land[by, tmpx2] = Color);
   429     and (LandGet(rect.y, tmpx2) = Color) and (LandGet(by, tmpx2) = Color);
   430     inc(tmpx);
   430     inc(tmpx);
   431     dec(tmpx2)
   431     dec(tmpx2)
   432     end;
   432     end;
   433 tmpy:= rect.y+1;
   433 tmpy:= rect.y+1;
   434 tmpy2:= by-1;
   434 tmpy2:= by-1;
   435 while (tmpy <= by - rect.h div 2 - 1) and bRes do
   435 while (tmpy <= by - rect.h div 2 - 1) and bRes do
   436     begin
   436     begin
   437     bRes:= ((tmpy and LAND_HEIGHT_MASK) = 0) and ((tmpy2 and LAND_HEIGHT_MASK) = 0)
   437     bRes:= ((tmpy and LAND_HEIGHT_MASK) = 0) and ((tmpy2 and LAND_HEIGHT_MASK) = 0)
   438     and ((rect.x and LAND_WIDTH_MASK) = 0) and ((bx and LAND_WIDTH_MASK) = 0)
   438     and ((rect.x and LAND_WIDTH_MASK) = 0) and ((bx and LAND_WIDTH_MASK) = 0)
   439     and (Land[tmpy, rect.x] = Color) and (Land[tmpy, bx] = Color)
   439     and (LandGet(tmpy, rect.x) = Color) and (LandGet(tmpy, bx) = Color)
   440     and (Land[tmpy2, rect.x] = Color) and (Land[tmpy2, bx] = Color);
   440     and (LandGet(tmpy2, rect.x) = Color) and (LandGet(tmpy2, bx) = Color);
   441     inc(tmpy);
   441     inc(tmpy);
   442     dec(tmpy2)
   442     dec(tmpy2)
   443     end;
   443     end;
   444 {$WARNINGS ON}
   444 {$WARNINGS ON}
   445 CheckLand:= bRes;
   445 CheckLand:= bRes;
   457     if (((rect.x and LAND_WIDTH_MASK) or (bx and LAND_WIDTH_MASK) or
   457     if (((rect.x and LAND_WIDTH_MASK) or (bx and LAND_WIDTH_MASK) or
   458          (rect.y and LAND_HEIGHT_MASK) or (by and LAND_HEIGHT_MASK)) = 0) then
   458          (rect.y and LAND_HEIGHT_MASK) or (by and LAND_HEIGHT_MASK)) = 0) then
   459     begin
   459     begin
   460         for tmpx := rect.x to bx do
   460         for tmpx := rect.x to bx do
   461         begin
   461         begin
   462             if (((Land[rect.y, tmpx] and LandType) or (Land[by, tmpx] and LandType)) <> 0) then
   462             if (((LandGet(rect.y, tmpx) and LandType) or (LandGet(by, tmpx) and LandType)) <> 0) then
   463             begin
   463             begin
   464                 CheckLandAny := true;
   464                 CheckLandAny := true;
   465                 exit;
   465                 exit;
   466             end
   466             end
   467         end;
   467         end;
   468         for tmpy := rect.y to by do
   468         for tmpy := rect.y to by do
   469         begin
   469         begin
   470             if (((Land[tmpy, rect.x] and LandType) or (Land[tmpy, bx] and LandType)) <> 0) then
   470             if (((LandGet(tmpy, rect.x) and LandType) or (LandGet(tmpy, bx) and LandType)) <> 0) then
   471             begin
   471             begin
   472                 CheckLandAny := true;
   472                 CheckLandAny := true;
   473                 exit;
   473                 exit;
   474             end
   474             end
   475         end;
   475         end;