DEADSOFTWARE

smoother camera on stairs/slopes
[d2df-sdl.git] / src / shared / utils.pas
index 9e136495bc8792dc661c3e4fa254ee58d24974a6..49f7ef1e4f1a9b2ec39be9cdd79760cb929ba538 100644 (file)
@@ -122,6 +122,43 @@ function readInt64BE (st: TStream): Int64;
 function readUInt64BE (st: TStream): UInt64;
 
 
+function nmin (a, b: Byte): Byte; inline; overload;
+function nmin (a, b: ShortInt): ShortInt; inline; overload;
+function nmin (a, b: Word): Word; inline; overload;
+function nmin (a, b: SmallInt): SmallInt; inline; overload;
+function nmin (a, b: LongWord): LongWord; inline; overload;
+function nmin (a, b: LongInt): LongInt; inline; overload;
+function nmin (a, b: Int64): Int64; inline; overload;
+function nmin (a, b: UInt64): UInt64; inline; overload;
+function nmin (a, b: Single): Single; inline; overload;
+function nmin (a, b: Double): Double; inline; overload;
+function nmin (a, b: Extended): Extended; inline; overload;
+
+function nmax (a, b: Byte): Byte; inline; overload;
+function nmax (a, b: ShortInt): ShortInt; inline; overload;
+function nmax (a, b: Word): Word; inline; overload;
+function nmax (a, b: SmallInt): SmallInt; inline; overload;
+function nmax (a, b: LongWord): LongWord; inline; overload;
+function nmax (a, b: LongInt): LongInt; inline; overload;
+function nmax (a, b: Int64): Int64; inline; overload;
+function nmax (a, b: UInt64): UInt64; inline; overload;
+function nmax (a, b: Single): Single; inline; overload;
+function nmax (a, b: Double): Double; inline; overload;
+function nmax (a, b: Extended): Extended; inline; overload;
+
+function nclamp (v, a, b: Byte): Byte; inline; overload;
+function nclamp (v, a, b: ShortInt): ShortInt; inline; overload;
+function nclamp (v, a, b: Word): Word; inline; overload;
+function nclamp (v, a, b: SmallInt): SmallInt; inline; overload;
+function nclamp (v, a, b: LongWord): LongWord; inline; overload;
+function nclamp (v, a, b: LongInt): LongInt; inline; overload;
+function nclamp (v, a, b: Int64): Int64; inline; overload;
+function nclamp (v, a, b: UInt64): UInt64; inline; overload;
+function nclamp (v, a, b: Single): Single; inline; overload;
+function nclamp (v, a, b: Double): Double; inline; overload;
+function nclamp (v, a, b: Extended): Extended; inline; overload;
+
+
 type
   TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
 
@@ -143,24 +180,25 @@ function quoteStr (const s: AnsiString): AnsiString;
 type
   generic TSimpleList<ItemT> = class
   private
-    type PItemT = ^ItemT;
+    //type PItemT = ^ItemT;
+    type TItemArr = array of ItemT;
 
   public
     type
       TEnumerator = record
       private
-        mItems: PItemT;
+        mItems: TItemArr;
         mCount: Integer;
         mCurrent: Integer;
       public
-        constructor Create (aitems: PItemT; acount: Integer);
+        constructor Create (const aitems: TItemArr; acount: Integer);
         function MoveNext: Boolean;
         function getCurrent (): ItemT;
         property Current: ItemT read getCurrent;
       end;
 
   private
-    mItems: array of ItemT;
+    mItems: TItemArr;
     mCount: Integer; // can be less than `mItems` size
 
   private
@@ -193,11 +231,11 @@ implementation
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-constructor TSimpleList.TEnumerator.Create (aitems: PItemT; acount: Integer);
+constructor TSimpleList.TEnumerator.Create (const aitems: TItemArr; acount: Integer);
 begin
   mItems := aitems;
-  mCount := acount;
   mCurrent := -1;
+  mCount := acount;
 end;
 
 function TSimpleList.TEnumerator.MoveNext: Boolean;
@@ -243,7 +281,7 @@ end;
 
 function TSimpleList.GetEnumerator (): TEnumerator;
 begin
-  if (Length(mItems) > 0) then result := TEnumerator.Create(@mItems[0], mCount)
+  if (Length(mItems) > 0) then result := TEnumerator.Create(mItems, mCount)
   else result := TEnumerator.Create(nil, -1);
 end;
 
@@ -974,6 +1012,44 @@ function readInt64BE (st: TStream): Int64; begin readIntegerBE(st, @result, 8);
 function readUInt64BE (st: TStream): UInt64; begin readIntegerBE(st, @result, 8); end;
 
 
+// ////////////////////////////////////////////////////////////////////////// //
+function nmin (a, b: Byte): Byte; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: ShortInt): ShortInt; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: Word): Word; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: SmallInt): SmallInt; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: LongWord): LongWord; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: LongInt): LongInt; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: Int64): Int64; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: UInt64): UInt64; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: Single): Single; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: Double): Double; inline; overload; begin if (a < b) then result := a else result := b; end;
+function nmin (a, b: Extended): Extended; inline; overload; begin if (a < b) then result := a else result := b; end;
+
+function nmax (a, b: Byte): Byte; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: ShortInt): ShortInt; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: Word): Word; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: SmallInt): SmallInt; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: LongWord): LongWord; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: LongInt): LongInt; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: Int64): Int64; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: UInt64): UInt64; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: Single): Single; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: Double): Double; inline; overload; begin if (a > b) then result := a else result := b; end;
+function nmax (a, b: Extended): Extended; inline; overload; begin if (a > b) then result := a else result := b; end;
+
+function nclamp (v, a, b: Byte): Byte; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: ShortInt): ShortInt; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: Word): Word; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: SmallInt): SmallInt; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: LongWord): LongWord; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: LongInt): LongInt; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: Int64): Int64; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: UInt64): UInt64; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: Single): Single; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: Double): Double; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+function nclamp (v, a, b: Extended): Extended; inline; overload; begin if (v < a) then result := a else if (v > b) then result := b else result := v; end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 {$IFDEF WINDOWS}
 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'msvcrt.dll' name '_snprintf';