DEADSOFTWARE

smoother camera on stairs/slopes
[d2df-sdl.git] / src / shared / utils.pas
index 49408d77a7ac88ab8481e9b1e7bb5e3d8be19e8a..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);
 
@@ -130,6 +167,7 @@ function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFor
 
 function wchar2win (wc: WideChar): AnsiChar; inline;
 function utf2win (const s: AnsiString): AnsiString;
+function win2utf (const s: AnsiString): AnsiString;
 function digitInBase (ch: AnsiChar; base: Integer): Integer;
 
 // returns string in single or double quotes
@@ -142,33 +180,39 @@ 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
     function getAt (idx: Integer): ItemT; inline;
+    procedure setAt (idx: Integer; const it: ItemT); inline;
+
+    function getCapacity (): Integer; inline;
+    procedure setCapacity (v: Integer); inline;
 
   public
-    constructor Create ();
+    constructor Create (acapacity: Integer=-1);
     destructor Destroy (); override;
 
+    //WARNING! don't change list contents in `for ... in`!
     function GetEnumerator (): TEnumerator;
 
     procedure reset (); inline; // won't resize `mItems`
@@ -178,7 +222,8 @@ type
 
   public
     property count: Integer read mCount;
-    property at[idx: Integer]: ItemT read getAt; default;
+    property capacity: Integer read getCapacity write setCapacity;
+    property at[idx: Integer]: ItemT read getAt write setAt; default;
   end;
 
 
@@ -186,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;
@@ -206,9 +251,10 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-constructor TSimpleList.Create ();
+constructor TSimpleList.Create (acapacity: Integer=-1);
 begin
   mItems := nil;
+  if (acapacity > 0) then SetLength(mItems, acapacity);
   mCount := 0;
 end;
 
@@ -220,9 +266,22 @@ begin
 end;
 
 
+function TSimpleList.getCapacity (): Integer; inline;
+begin
+  result := Length(mItems);
+end;
+
+
+procedure TSimpleList.setCapacity (v: Integer); inline;
+begin
+  if (v < mCount) then v := mCount;
+  if (v <> Length(mItems)) then SetLength(mItems, v);
+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;
 
@@ -246,6 +305,12 @@ begin
 end;
 
 
+procedure TSimpleList.setAt (idx: Integer; const it: ItemT); inline;
+begin
+  if (idx >= 0) and (idx < mCount) then mItems[idx] := it;
+end;
+
+
 procedure TSimpleList.append (constref it: ItemT); inline;
 begin
   if (mCount = Length(mItems)) then
@@ -264,7 +329,6 @@ var
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-procedure initShitMap ();
 const
   cp1251: array[0..127] of Word = (
     $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
@@ -276,6 +340,9 @@ const
     $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
     $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
   );
+
+
+procedure initShitMap ();
 var
   f: Integer;
 begin
@@ -374,6 +441,65 @@ begin
 end;
 
 
+function win2utf (const s: AnsiString): AnsiString;
+var
+  f, c: Integer;
+
+  function utf8Encode (code: Integer): AnsiString;
+  begin
+    if (code < 0) or (code > $10FFFF) then begin result := '?'; exit; end;
+    if (code <= $7f) then
+    begin
+      result := Char(code and $ff);
+    end
+    else if (code <= $7FF) then
+    begin
+      result := Char($C0 or (code shr 6));
+      result += Char($80 or (code and $3F));
+    end
+    else if (code <= $FFFF) then
+    begin
+      result := Char($E0 or (code shr 12));
+      result += Char($80 or ((code shr 6) and $3F));
+      result += Char($80 or (code and $3F));
+    end
+    else if (code <= $10FFFF) then
+    begin
+      result := Char($F0 or (code shr 18));
+      result += Char($80 or ((code shr 12) and $3F));
+      result += Char($80 or ((code shr 6) and $3F));
+      result += Char($80 or (code and $3F));
+    end
+    else
+    begin
+      result := '?';
+    end;
+  end;
+
+begin
+  for f := 1 to Length(s) do
+  begin
+    if (Byte(s[f]) > 127) then
+    begin
+      result := '';
+      for c := 1 to Length(s) do
+      begin
+        if (Byte(s[c]) < 128) then
+        begin
+          result += s[c];
+        end
+        else
+        begin
+          result += utf8Encode(cp1251[Byte(s[c])-128])
+        end;
+      end;
+      exit;
+    end;
+  end;
+  result := s;
+end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 function digitInBase (ch: AnsiChar; base: Integer): Integer;
 begin
@@ -886,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';
@@ -951,7 +1115,7 @@ var
       while (len > 0) do
       begin
         if (len > 255) then slen := 255 else slen := Integer(len);
-        Move(b^, ss[1], len);
+        Move(b^, ss[1], slen);
         ss[0] := AnsiChar(slen);
         result += ss;
         b += slen;