⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hgedef.pas

📁 此為國外大名鼎鼎的2D遊戲引擎HGE的Delphi版本 原官方是C++的,現在完全改為Delphi可使用,另外再增加許多單元與功能 新增的單元有HGEImages,HGECanvas,HGEDef
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit HGEDef;
(*
** hgeDef unit
** Extension to the HGE engine
** Extension added by DraculaLin
** This extension is NOT part of the original HGE engine.
*)

interface


uses
 Types, Classes, SysUtils, Graphics,Math;

type

TPoint2 = record
     X, Y: Single;
     class operator Add(const a, b: TPoint2): TPoint2;
     class operator Subtract(const a, b: TPoint2): TPoint2;
     class operator Multiply(const v: TPoint2; k: Single): TPoint2;
end;

TPoint4 = array[0..3] of TPoint2;
PPolygon = ^TPolygon;
TPolygon = array of TPoint2;
TBezierPoints  = Array[0..3] of TPoint;
TBezierPoints2 = TPoint4;
function Point2(x, y: Single): TPoint2;
function Angle2(const v: TPoint2): Single;
function Length2(const v: TPoint2): Single;
function DisplaceRB(Color: Cardinal): Cardinal;
function PointInRect(const Point: TPoint; const Rect: TRect): Boolean;
function RectInRect(const Rect1, Rect2: TRect): Boolean;
function OverlapRect(const Rect1, Rect2: TRect): Boolean;
function ShrinkRect(const Rect: TRect; const hIn, vIn: Integer): TRect;
function MoveRect(const Rect: TRect; const DeltaX, DeltaY: Integer): TRect;
function MovePoint(const Point: TPoint; const DeltaX, DeltaY: Integer): TPoint;
function PositionRect(const Rect: TRect; const NewPos: TPoint): TRect;
procedure VPartitionRect(const Rect: TRect; const Theta: Real; out Rect1, Rect2: TRect);
procedure HPartitionRect(const Rect: TRect; const Theta: Real; out Rect1, Rect2: TRect);
function VPartOfRect1(const Rect: TRect; const Theta: Real): TRect;
function VPartOfRect2(const Rect: TRect; const Theta: Real): TRect;
function HPartOfRect1(const Rect: TRect; const Theta: Real): TRect;
function HPartOfRect2(const Rect: TRect; const Theta: Real): TRect;
function HShrinkRect(const Rect: TRect; const LeftAmount, RightAmount: Integer): TRect;
function VShrinkRect(const Rect: TRect; const TopAmount, BottomAmount: Integer): TRect;
function ResizeRect(const Rect: TRect; const nWidth, nHeight: Integer): TRect;
function ScalePoint(const Pt: TPoint; const Theta: Real): TPoint;
function AlignToGridPt(const Pt: TPoint; const GridX, GridY: Integer): TPoint;
function ScaleRect(const Rect: TRect; const Theta: Real): TRect;
function Linear2Sine(Alpha: Real): Real;
function BezierPoints(const OriginPoint, DestPoint, C1Point, C2Point: TPoint): TBezierPoints; overload;
function BezierPoints(const OriginPoint, DestPoint, C1Point, C2Point: TPoint2): TBezierPoints2;overload;
function OverlapQuadrangle(Q1, Q2: TPoint4): Boolean;
function OverlapPolygon(P1, P2: TPolygon): Boolean;
function PtInPolygon(Pt : TPoint2 ; Pg : TPolygon):Boolean;
//---------------------------------------------------------------------------
//  precalculate   Sin Table, Cos Table
//---------------------------------------------------------------------------
function Cos8(i: Integer): Double;
function Sin8(i: Integer): Double;
function Cos16(i: Integer): Double;
function Sin16(i: Integer): Double;
function Cos32(i: Integer): Double;
function Sin32(i: Integer): Double;
function Cos64(i: Integer): Double;
function Sin64(i: Integer): Double;
function Cos128(i: Integer):Double;
function Sin128(i: Integer):Double;
function Cos256(i: Integer):Double;
function Sin256(i: Integer):Double;
function Cos512(i: Integer):Double;
function Sin512(i: Integer):Double;

function Angle360(X, Y: Integer): Real;
function Angle256(X, Y: Integer): Real;
function AngleDiff(SrcAngle, DestAngle: Single): Single;

implementation

class operator TPoint2.Add(const a, b: TPoint2): TPoint2;
begin
 Result.x:= a.x + b.x;
 Result.y:= a.y + b.y;
end;

class operator TPoint2.Subtract(const a, b: TPoint2): TPoint2;
begin
 Result.x:= a.x - b.x;
 Result.y:= a.y - b.y;
end;

class operator TPoint2.Multiply(const v: TPoint2; k: Single): TPoint2;
begin
 Result.x:= v.x * k;
 Result.y:= v.y * k;
end;

function Point2(X, Y: Single): TPoint2;
begin
 Result.X:= x;
 Result.Y:= y;
end;

function Angle2(const v: TPoint2): Single;
begin
 Result:= ArcTan2(v.y, v.x);
end;

function Length2(const v: TPoint2): Single;
begin
 Result:= Hypot(v.x, v.y);
end;

function PointInRect(const Point: TPoint; const Rect: TRect): Boolean;
begin
 Result:= (Point.X >= Rect.Left)and(Point.X <= Rect.Right)and
  (Point.Y >= Rect.Top)and(Point.Y <= Rect.Bottom);
end;

//---------------------------------------------------------------------------
function RectInRect(const Rect1, Rect2: TRect): Boolean;
begin
 Result:= (Rect1.Left >= Rect2.Left)and(Rect1.Right <= Rect2.Right)and
  (Rect1.Top >= Rect2.Top)and(Rect1.Bottom <= Rect2.Bottom);
end;
//---------------------------------------------------------------------------
function OverlapRect(const Rect1, Rect2: TRect): Boolean;
begin
 Result:= (Rect1.Left < Rect2.Right)and(Rect1.Right > Rect2.Left)and
  (Rect1.Top < Rect2.Bottom)and(Rect1.Bottom > Rect2.Top);
end;

//---------------------------------------------------------------------------
function ShrinkRect(const Rect: TRect; const hIn, vIn: Integer): TRect;
begin
 Result.Left:= Rect.Left + hIn;
 Result.Top:= Rect.Top + vIn;
 Result.Right:= Rect.Right - hIn;
 Result.Bottom:= Rect.Bottom - vIn;
end;

//---------------------------------------------------------------------------
function MoveRect(const Rect: TRect; const DeltaX, DeltaY: Integer): TRect;
begin
 Result.Left:= Rect.Left + DeltaX;
 Result.Top:= Rect.Top + DeltaY;
 Result.Right:= Rect.Right + DeltaX;
 Result.Bottom:= Rect.Bottom + DeltaY;
end;

//---------------------------------------------------------------------------
function MovePoint(const Point: TPoint; const DeltaX, DeltaY: Integer): TPoint;
begin
 Result.X:= Point.X + DeltaX;
 Result.Y:= Point.Y + DeltaY;
end;

//---------------------------------------------------------------------------
function PositionRect(const Rect: TRect; const NewPos: TPoint): TRect;
begin
 Result:= MoveRect(Rect, NewPos.X - Rect.Left, NewPos.Y - Rect.Top);
end;

//---------------------------------------------------------------------------
procedure VPartitionRect(const Rect: TRect; const Theta: Real; out Rect1, Rect2: TRect);
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Bottom - Rect.Top) * Theta));

 Rect1:= Bounds(Rect.Left, Rect.Top, Abs(Rect.Right - Rect.Left), Delta - 1);
 Rect2:= Bounds(Rect.Left, Rect.Top + Delta - 1, Abs(Rect.Right - Rect.Left),
  (Rect.Bottom - Rect.Top) - Delta + 1);
end;

//---------------------------------------------------------------------------
procedure HPartitionRect(const Rect: TRect; const Theta: Real; out Rect1, Rect2: TRect);
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Right - Rect.Left) * Theta));

 Rect1:= Bounds(Rect.Left, Rect.Top, Delta - 1, Abs(Rect.Bottom - Rect.Top));
 Rect2:= Bounds(Rect.Left + Delta - 1, Rect.Top, (Rect.Right - Rect.Left) -
  Delta, Abs(Rect.Bottom - Rect.Top));
end;

//---------------------------------------------------------------------------
function VPartOfRect1(const Rect: TRect; const Theta: Real): TRect;
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Bottom - Rect.Top) * Theta));

 Result:= Bounds(Rect.Left, Rect.Top, Abs(Rect.Right - Rect.Left), Delta - 1);
end;

//---------------------------------------------------------------------------
function VPartOfRect2(const Rect: TRect; const Theta: Real): TRect;
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Bottom - Rect.Top) * Theta));

 Result:= Bounds(Rect.Left, Rect.Top + Delta - 1, Abs(Rect.Right - Rect.Left),
  (Rect.Bottom - Rect.Top) - Delta);
end;

//---------------------------------------------------------------------------
function HPartOfRect1(const Rect: TRect; const Theta: Real): TRect;
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Right - Rect.Left) * Theta));

 Result:= Bounds(Rect.Left, Rect.Top, Delta - 1, Abs(Rect.Bottom - Rect.Top));
end;

//---------------------------------------------------------------------------
function HPartOfRect2(const Rect: TRect; const Theta: Real): TRect;
var
 Delta: Integer;
begin
 Delta:= Round(Abs((Rect.Right - Rect.Left) * Theta));

 Result:= Bounds(Rect.Left + Delta - 1, Rect.Top, (Rect.Right - Rect.Left) -
  Delta, Abs(Rect.Bottom - Rect.Top));
end;

//---------------------------------------------------------------------------
function HShrinkRect(const Rect: TRect; const LeftAmount, RightAmount: Integer): TRect;
begin
 Result.Left:= Rect.Left + LeftAmount;
 Result.Top:= Rect.Top;
 Result.Right:= Rect.Right - RightAmount;
 Result.Bottom:= Rect.Bottom;
end;

//---------------------------------------------------------------------------
function VShrinkRect(const Rect: TRect; const TopAmount, BottomAmount: Integer): TRect;
begin
 Result.Left:= Rect.Left;
 Result.Top:= Rect.Top + TopAmount;
 Result.Right:= Rect.Right;
 Result.Bottom:= Rect.Bottom - BottomAmount;
end;

//---------------------------------------------------------------------------
function ResizeRect(const Rect: TRect; const nWidth, nHeight: Integer): TRect;
begin
 Result:= Bounds(Rect.Left, Rect.Top, nWidth, nHeight);
end;

//---------------------------------------------------------------------------
function ScalePoint(const Pt: TPoint; const Theta: Real): TPoint;
begin
 Result.X:= Round(Pt.X * Theta);
 Result.Y:= Round(Pt.Y * Theta);
end;

//---------------------------------------------------------------------------
function AlignToGridPt(const Pt: TPoint; const GridX, GridY: Integer): TPoint;
begin
 Result:= Point((Pt.X div GridX) * GridX, (Pt.Y div GridY) * GridY);
end;

//---------------------------------------------------------------------------
function ScaleRect(const Rect: TRect; const Theta: Real): TRect;
begin
 Result.Left:= Round(Rect.Left * Theta);
 Result.Right:= Round(Rect.Right * Theta);
 Result.Top:= Round(Rect.Top * Theta);
 Result.Bottom:= Round(Rect.Bottom * Theta);
end;

//---------------------------------------------------------------------------
function DisplaceRB(Color: Cardinal): Cardinal; register;
asm
 mov ecx, eax
 mov edx, eax
 and eax, 0FF00FF00h
 and edx, 0000000FFh
 shl edx, 16
 or eax, edx
 mov edx, ecx
 shr edx, 16
 and edx, 0000000FFh
 or eax, edx
end;



//---------------------------------------------------------------------------



function Linear2Sine(Alpha: Real): Real;
const
 PiHalf = Pi / 2.0;
begin
 Result:= (Sin((Alpha * Pi) - PiHalf) + 1.0) / 2.0;
end;

//---------------------------------------------------------------------------

function BezierPoints(const OriginPoint, DestPoint, C1Point, C2Point: TPoint): TBezierPoints;
begin
  Result[0] := OriginPoint;
  Result[1] := C1Point;
  Result[2] := C2Point;
  Result[3] := DestPoint;
end;

function BezierPoints(const OriginPoint, DestPoint, C1Point, C2Point: TPoint2): TBezierPoints2;
begin
  Result[0] := OriginPoint;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -