hedgewars/uCollisions.pas
changeset 15322 361e79c6c428
parent 14552 e0af4ce7d8bc
child 15323 9299f43ba0ec
equal deleted inserted replaced
15321:a3823f0916b8 15322:361e79c6c428
    94 // returns: negative sign if going downhill to left, value is steepness (noslope/error = _0, 45 = _0_5)
    94 // returns: negative sign if going downhill to left, value is steepness (noslope/error = _0, 45 = _0_5)
    95 function  CalcSlopeBelowGear(Gear: PGear): hwFloat;
    95 function  CalcSlopeBelowGear(Gear: PGear): hwFloat;
    96 function  CalcSlopeNearGear(Gear: PGear; dirX, dirY: LongInt): hwFloat;
    96 function  CalcSlopeNearGear(Gear: PGear; dirX, dirY: LongInt): hwFloat;
    97 function  CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
    97 function  CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
    98 
    98 
       
    99 function CheckGearsUnderSprite(Sprite: TSprite; sprX, sprY, Frame: LongInt): boolean;
       
   100 
    99 implementation
   101 implementation
   100 uses uConsts, uLandGraphics, uVariables;
   102 uses uConsts, uLandGraphics, uVariables, SDLh, uLandTexture, uDebug;
   101 
   103 
   102 type TCollisionEntry = record
   104 type TCollisionEntry = record
   103     X, Y, Radius: LongInt;
   105     X, Y, Radius: LongInt;
   104     cGear: PGear;
   106     cGear: PGear;
   105     end;
   107     end;
  1018     end;
  1020     end;
  1019 
  1021 
  1020 CalcSlopeBelowGear := _0;
  1022 CalcSlopeBelowGear := _0;
  1021 end;
  1023 end;
  1022 
  1024 
       
  1025 function CheckGearsUnderSprite(Sprite: TSprite; sprX, sprY, Frame: LongInt): boolean;
       
  1026 var x, y, bpp, h, w, row, col, gx, gy, r, numFramesFirstCol: LongInt;
       
  1027     p: PByteArray;
       
  1028     Image: PSDL_Surface;
       
  1029     Gear: PGear;
       
  1030 begin
       
  1031     CheckGearsUnderSprite := false;
       
  1032     if checkFails(SpritesData[Sprite].Surface <> nil, 'Assert SpritesData[Sprite].Surface failed', true) then exit;
       
  1033 
       
  1034     numFramesFirstCol:= SpritesData[Sprite].imageHeight div SpritesData[Sprite].Height;
       
  1035     Image:= SpritesData[Sprite].Surface;
       
  1036 
       
  1037     if SDL_MustLock(Image) then
       
  1038         if SDLCheck(SDL_LockSurface(Image) >= 0, 'CheckGearsUnderSprite', true) then exit;
       
  1039 
       
  1040     bpp:= Image^.format^.BytesPerPixel;
       
  1041 
       
  1042     if checkFails(bpp = 4, 'It should be 32 bpp sprite', true) then
       
  1043         begin
       
  1044         if SDL_MustLock(Image) then
       
  1045             SDL_UnlockSurface(Image);
       
  1046         exit
       
  1047         end;
       
  1048 
       
  1049     w:= SpritesData[Sprite].Width;
       
  1050     h:= SpritesData[Sprite].Height;
       
  1051 
       
  1052     row:= Frame mod numFramesFirstCol;
       
  1053     col:= Frame div numFramesFirstCol;
       
  1054     p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
       
  1055     Gear:= GearsList;
       
  1056 
       
  1057     while Gear <> nil do
       
  1058         begin
       
  1059         if (Gear^.Kind = gtAirMine) or ((Gear^.Kind = gtHedgehog) and (Gear^.CollisionIndex <> 0)) then
       
  1060             begin
       
  1061             gx:= hwRound(Gear^.X);
       
  1062             gy:= hwRound(Gear^.Y);
       
  1063             r:= Gear^.Radius;
       
  1064             if (gx + r >= sprX) and (gx - r < sprX + w) and (gy + r >= sprY) and (gy - r < sprY + h) then
       
  1065                 for y := gy - r to gy + r do
       
  1066                     for x := gx - r to gx + r do
       
  1067                         begin
       
  1068                         if (x >= sprX) and (x < sprX + w) and (y >= sprY) and (y < sprY + h)
       
  1069                         and (Sqr(x - gx) + Sqr(y - gy) < Sqr(r))
       
  1070                         and (((PLongword(@(p^[Image^.pitch * y + x * 4]))^) and AMask) <> 0) then
       
  1071                             begin
       
  1072                             CheckGearsUnderSprite := true;
       
  1073                             if SDL_MustLock(Image) then
       
  1074                                 SDL_UnlockSurface(Image);
       
  1075                             exit
       
  1076                             end
       
  1077                         end
       
  1078             end;
       
  1079 
       
  1080         Gear := Gear^.NextGear
       
  1081         end;
       
  1082 end;
       
  1083 
  1023 procedure initModule;
  1084 procedure initModule;
  1024 begin
  1085 begin
  1025     Count:= 0;
  1086     Count:= 0;
  1026 end;
  1087 end;
  1027 
  1088