| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2017-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Vector types using SmallInt components. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Vector of 2 SmallInt values. } |
| 23 |
TVector2SmallInt = record |
| 24 |
public |
| 25 |
type |
| 26 |
TIndex = 0..1; |
| 27 |
strict private |
| 28 |
function GetItems(const Index: TIndex): SmallInt; inline; |
| 29 |
public |
| 30 |
{ Get vector components by index. |
| 31 |
This is a default property, so you can write @code(MyVector[0]) |
| 32 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 33 |
|
| 34 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 35 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 36 |
Use @code(MyVector.Data[0] := 123) if you want to set by index, |
| 37 |
or @code(MyVector.X := 123). } |
| 38 |
property AsArray [const Index: TIndex]: SmallInt read GetItems; default; |
| 39 |
|
| 40 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2SmallInt): TVector2SmallInt; inline; |
| 41 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2SmallInt): TVector2SmallInt; inline; |
| 42 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2SmallInt): TVector2SmallInt; inline; |
| 43 |
|
| 44 |
function ToString: string; |
| 45 |
|
| 46 |
class function Zero: TVector2SmallInt; static; inline; |
| 47 |
|
| 48 |
case Integer of |
| 49 |
0: (X, Y: SmallInt); |
| 50 |
1: (Data: array [TIndex] of SmallInt); |
| 51 |
end; |
| 52 |
|
| 53 |
PVector2SmallInt = ^TVector2SmallInt; |
| 54 |
|
| 55 |
function Vector2SmallInt(const X, Y: SmallInt): TVector2SmallInt; inline; |
| 56 |
|
| 57 |
{$endif read_interface} |
| 58 |
|
| 59 |
{$ifdef read_implementation} |
| 60 |
|
| 61 |
{ TVector2SmallInt ------------------------------------------------------------ } |
| 62 |
|
| 63 |
class operator TVector2SmallInt. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2SmallInt): TVector2SmallInt; |
| 64 |
begin |
| 65 |
Result.Data[0] := A.Data[0] + B.Data[0]; |
| 66 |
Result.Data[1] := A.Data[1] + B.Data[1]; |
| 67 |
end; |
| 68 |
|
| 69 |
class operator TVector2SmallInt. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2SmallInt): TVector2SmallInt; |
| 70 |
begin |
| 71 |
Result.Data[0] := A.Data[0] - B.Data[0]; |
| 72 |
Result.Data[1] := A.Data[1] - B.Data[1]; |
| 73 |
end; |
| 74 |
|
| 75 |
class operator TVector2SmallInt. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2SmallInt): TVector2SmallInt; |
| 76 |
begin |
| 77 |
Result.Data[0] := - V.Data[0]; |
| 78 |
Result.Data[1] := - V.Data[1]; |
| 79 |
end; |
| 80 |
|
| 81 |
function TVector2SmallInt.ToString: string; |
| 82 |
begin |
| 83 |
Result := Format('%d %d', [Data[0], Data[1]]); |
| 84 |
end; |
| 85 |
|
| 86 |
function TVector2SmallInt.GetItems(const Index: TIndex): SmallInt; |
| 87 |
begin |
| 88 |
Result := Data[Index]; |
| 89 |
end; |
| 90 |
|
| 91 |
class function TVector2SmallInt.Zero: TVector2SmallInt; |
| 92 |
begin |
| 93 |
FillChar(Result, SizeOf(Result), 0); |
| 94 |
end; |
| 95 |
|
| 96 |
{ global routines ------------------------------------------------------------ } |
| 97 |
|
| 98 |
function Vector2SmallInt(const X, Y: SmallInt): TVector2SmallInt; |
| 99 |
begin |
| 100 |
Result.Data[0] := X; |
| 101 |
Result.Data[1] := Y; |
| 102 |
end; |
| 103 |
|
| 104 |
{$endif read_implementation} |