Changeset 167 for trunk


Ignore:
Timestamp:
Nov 23, 2017, 9:57:48 AM (7 years ago)
Author:
chronos
Message:
  • Fixed: Generate map only once on application start.
  • Modified: Use const function parameter modifier in Geometry unit.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormNew.pas

    r166 r167  
    103103
    104104uses
    105   UFormPlayer, UCore;
     105  UFormPlayer;
    106106
    107107resourcestring
  • trunk/Packages/Common/UGeometry.pas

    r166 r167  
    2020    P1: TPoint;
    2121    P2: TPoint;
    22     function Create(P1, P2: TPoint): TLine;
     22    function Create(const P1, P2: TPoint): TLine;
    2323    function GetMiddle: TPoint;
    2424    function GetAngle: Double;
     
    2626    function ToRect: TRect;
    2727    function DotProduct: Double;
    28     procedure Rotate(Angle: Double);
    29     class operator Equal(A, B: TLine): Boolean;
     28    procedure Rotate(const Angle: Double);
     29    class operator Equal(const A, B: TLine): Boolean;
    3030    property Distance: Double read GetDistance write SetDistance;
    3131  end;
     
    3535  TPolygon = record
    3636    Points: TPointArray;
    37     function IsPointInside(P: TPoint): Boolean;
    38     function Create(Points: TPointArray): TPolygon; overload;
    39     function Create(Rect: TRect): TPolygon; overload;
    40     procedure AddPoint(P: TPoint);
     37    function IsPointInside(const P: TPoint): Boolean;
     38    function Create(const Points: TPointArray): TPolygon; overload;
     39    function Create(const Rect: TRect): TPolygon; overload;
     40    procedure AddPoint(const P: TPoint);
    4141    procedure Clear;
    42     procedure CutLine(Vector: TLine; PointInside: TPoint);
    43   end;
    44 
    45 function Distance(P1, P2: TPoint): Integer;
     42    procedure CutLine(const Vector: TLine; const PointInside: TPoint);
     43  end;
     44
     45function Distance(const P1, P2: TPoint): Integer;
    4646function Dot(const P1, P2: TPoint): Double;
    4747function AddPoint(const P1, P2: TPoint): TPoint;
    4848function SubPoint(const P1, P2: TPoint): TPoint;
    4949function PointToLineDistance(const P, V, W: TPoint): Integer;
    50 function ComparePoint(P1, P2: TPoint): Boolean;
    51 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
    52 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
    53 function LineIntersect(LineA, LineB: TLine; out Intersection: TPoint): Boolean;
    54 function ArcTan2Point(Point: TPoint): Float;
    55 function ArcTanPoint(Point: TPoint): Float;
    56 function RectEquals(A, B: TRect): Boolean;
    57 function RectEnlarge(Rect: TRect; Value: Integer): TRect;
    58 function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
    59 function PointsToRect(P1, P2: TPoint): TRect;
    60 function PointInRect(P: TPoint; aRect: TRect): Boolean;
    61 function HalfDistancePoint(P1, P2: TPoint): TPoint;
    62 function NormalizeAngle(Angle: Double): Double;
     50function ComparePoint(const P1, P2: TPoint): Boolean;
     51function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
     52function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
     53function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
     54function ArcTan2Point(const Point: TPoint): Float;
     55function ArcTanPoint(const Point: TPoint): Float;
     56function RectEquals(const A, B: TRect): Boolean;
     57function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
     58function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
     59function PointsToRect(const P1, P2: TPoint): TRect;
     60function PointInRect(const P: TPoint; aRect: TRect): Boolean;
     61function HalfDistancePoint(const P1, P2: TPoint): TPoint;
     62function NormalizeAngle(const Angle: Double): Double;
    6363function SubAngle(A1, A2: Double): Double;
    6464
    6565implementation
    6666
    67 function Distance(P1, P2: TPoint): Integer;
     67function Distance(const P1, P2: TPoint): Integer;
    6868begin
    6969  Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
     
    116116end;
    117117
    118 function ComparePoint(P1, P2: TPoint): Boolean;
     118function ComparePoint(const P1, P2: TPoint): Boolean;
    119119begin
    120120  Result := (P1.X = P2.X) and (P1.Y = P2.Y);
    121121end;
    122122
    123 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
    124 begin
    125   P := Point(P.X - Center.X, P.Y - Center.Y);
    126   Result := Point(Center.X + Round(P.X * Cos(Angle) - P.Y * Sin(Angle)),
    127     Center.Y + Round(P.X * Sin(Angle) + P.Y * Cos(Angle)));
    128 end;
    129 
    130 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
     123function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
     124var
     125  D: TPoint;
     126begin
     127  D := Point(P.X - Center.X, P.Y - Center.Y);
     128  Result := Point(Center.X + Round(D.X * Cos(Angle) - D.Y * Sin(Angle)),
     129    Center.Y + Round(D.X * Sin(Angle) + D.Y * Cos(Angle)));
     130end;
     131
     132function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
    131133var
    132134  I: Integer;
     
    137139end;
    138140
    139 function LineIntersect(LineA, LineB: TLine; out Intersection: TPoint): Boolean;
     141function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
    140142Var
    141143  LDetLineA, LDetLineB, LDetDivInv: Double;
     
    143145  D: Integer;
    144146begin
    145   if (LineA.P1 = LineA.P2) or (LineB.P1 = LineB.P2) then begin
     147  if ComparePoint(LineA.P1, LineA.P2) or ComparePoint(LineB.P1, LineB.P2) then begin
    146148    Result := False;
    147149    Exit;
     
    166168end;
    167169
    168 function ArcTan2Point(Point: TPoint): Float;
     170function ArcTan2Point(const Point: TPoint): Float;
    169171begin
    170172  Result := ArcTan2(Point.Y, Point.X);
    171173end;
    172174
    173 function ArcTanPoint(Point: TPoint): Float;
     175function ArcTanPoint(const Point: TPoint): Float;
    174176begin
    175177  if Point.Y = 0 then Result := Infinity
     
    177179end;
    178180
    179 function RectEquals(A, B: TRect): Boolean;
     181function RectEquals(const A, B: TRect): Boolean;
    180182begin
    181183  Result := (A.Left = B.Left) and (A.Top = B.Top) and
     
    183185end;
    184186
    185 function RectEnlarge(Rect: TRect; Value: Integer): TRect;
     187function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
    186188begin
    187189  Result.Left := Rect.Left - Value;
     
    191193end;
    192194
    193 function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
     195function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
    194196begin
    195197  Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
     
    197199end;
    198200
    199 function PointsToRect(P1, P2: TPoint): TRect;
     201function PointsToRect(const P1, P2: TPoint): TRect;
    200202begin
    201203  if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
     
    205207end;
    206208
    207 function PointInRect(P: TPoint; aRect: TRect): Boolean;
     209function PointInRect(const P: TPoint; aRect: TRect): Boolean;
    208210begin
    209211  Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
     
    211213end;
    212214
    213 function HalfDistancePoint(P1, P2: TPoint): TPoint;
     215function HalfDistancePoint(const P1, P2: TPoint): TPoint;
    214216begin
    215217  Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
    216218end;
    217219
    218 function NormalizeAngle(Angle: Double): Double;
     220function NormalizeAngle(const Angle: Double): Double;
    219221begin
    220222  if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
     
    233235{ TPolygon }
    234236
    235 function TPolygon.IsPointInside(P: TPoint): Boolean;
     237function TPolygon.IsPointInside(const P: TPoint): Boolean;
    236238var
    237239  I, J: Integer;
     
    273275
    274276
    275 function TPolygon.Create(Points: TPointArray): TPolygon;
     277function TPolygon.Create(const Points: TPointArray): TPolygon;
    276278var
    277279  I: Integer;
     
    282284end;
    283285
    284 function TPolygon.Create(Rect: TRect): TPolygon;
     286function TPolygon.Create(const Rect: TRect): TPolygon;
    285287begin
    286288  SetLength(Result.Points, 4);
     
    291293end;
    292294
    293 procedure TPolygon.AddPoint(P: TPoint);
     295procedure TPolygon.AddPoint(const P: TPoint);
    294296begin
    295297  SetLength(Points, Length(Points) + 1);
     
    302304end;
    303305
    304 procedure TPolygon.CutLine(Vector: TLine; PointInside: TPoint);
     306procedure TPolygon.CutLine(const Vector: TLine; const PointInside: TPoint);
    305307var
    306308  I: Integer;
     
    367369end;
    368370
    369 function TLine.Create(P1, P2: TPoint): TLine;
     371function TLine.Create(const P1, P2: TPoint): TLine;
    370372begin
    371373  Result.P1 := P1;
     
    401403end;
    402404
    403 procedure TLine.Rotate(Angle: Double);
     405procedure TLine.Rotate(const Angle: Double);
    404406begin
    405407  P2 := RotatePoint(P1, P2, Angle);
    406408end;
    407409
    408 class operator TLine.Equal(A, B: TLine): Boolean;
    409 begin
    410   Result := (A.P1 = B.P1) and (A.P2 = B.P2);
    411 end;
    412 
     410class operator TLine.Equal(const A, B: TLine): Boolean;
     411begin
     412  Result := ComparePoint(A.P1, B.P1) and ComparePoint(A.P2, B.P2);
     413end;
    413414
    414415end.
  • trunk/UGame.pas

    r166 r167  
    188188  private
    189189    FSize: TPoint;
    190     FUpdateCount: Integer;
    191190    function GetSize: TPoint; virtual;
    192191    procedure PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string; View: TView;
     
    229228    procedure CreateLinks;
    230229    procedure Clear;
    231     procedure BeginUpdate;
    232     procedure EndUpdate;
    233230    constructor Create; virtual;
    234231    destructor Destroy; override;
     
    13881385  if (FSize.X <> AValue.X) or (FSize.Y <> AValue.Y) then begin
    13891386    FSize := AValue;
    1390     if FUpdateCount = 0 then Generate;
    13911387  end;
    13921388end;
     
    15041500//  I: Integer;
    15051501begin
    1506   FUpdateCount := Source.FUpdateCount;
    15071502  MaxPower := Source.MaxPower;
    15081503  Game := Source.Game;
     
    17841779  Cells.Clear;
    17851780  FNewCellId := 1;
    1786 end;
    1787 
    1788 procedure TMap.BeginUpdate;
    1789 begin
    1790   Inc(FUpdateCount);
    1791 end;
    1792 
    1793 procedure TMap.EndUpdate;
    1794 begin
    1795   if FUpdateCount > 0 then Dec(FUpdateCount);
    1796   if FUpdateCount = 0 then Generate;
    17971781end;
    17981782
     
    33173301begin
    33183302  with Config do begin
    3319     try
    3320       Map.BeginUpdate;
    3321       MapType := TMapType(GetValue(DOMString(Path + '/GridType'), Integer(mtHexagon)));
    3322       Map.Size := Point(GetValue(DOMString(Path + '/MapSizeX'), 10),
    3323         GetValue(DOMString(Path + '/MapSizeY'), 10));
    3324     finally
    3325       Map.EndUpdate;
    3326     end;
     3303    MapType := TMapType(GetValue(DOMString(Path + '/GridType'), Integer(mtHexagon)));
     3304    Map.Size := Point(GetValue(DOMString(Path + '/MapSizeX'), 10),
     3305      GetValue(DOMString(Path + '/MapSizeY'), 10));
    33273306    MapImageFileName := string(GetValue(DOMString(Path + '/MapImage'), DOMString(MapImageFileName)));
    33283307    SymetricMap := GetValue(DOMString(Path + '/SymetricMap'), False);
  • trunk/UMap.pas

    r166 r167  
    296296  LeftCellCommon: TCell;
    297297  RightCellCommon: TCell;
    298   LeftText: string;
    299   RightText: string;
     298  //LeftText: string;
     299  //RightText: string;
    300300  NeighborCell: TCell;
    301301begin
    302302  Clear;
    303   RandSeed := 1234;
     303  //RandSeed := 1234;
    304304
    305305  // Allocate and init new cells
Note: See TracChangeset for help on using the changeset viewer.