| 1 |
{%MainUnit castletimeutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2019-2019 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 |
{$ifdef read_interface} |
| 18 |
{ Just like standard GetTickCount64 function, but works on all platforms supported by CGE. |
| 19 |
On Nintendo Switch the standard function will crash now. } |
| 20 |
function CastleGetTickCount64: QWord; |
| 21 |
{$endif} |
| 22 |
|
| 23 |
{$ifdef read_implementation} |
| 24 |
|
| 25 |
{ Windows -------------------------------------------------------------------- } |
| 26 |
|
| 27 |
{$ifdef MSWINDOWS} |
| 28 |
{ GetTickCount64 for Windows, from fpc/3.0.0/src/rtl/win/sysutils.pp } |
| 29 |
|
| 30 |
{$IFNDEF WINCE} |
| 31 |
type |
| 32 |
TGetTickCount64 = function : QWord; stdcall; |
| 33 |
|
| 34 |
var |
| 35 |
WinGetTickCount64: TGetTickCount64 = Nil; |
| 36 |
{$ENDIF} |
| 37 |
|
| 38 |
function CastleGetTickCount64: QWord; |
| 39 |
{$IFNDEF WINCE} |
| 40 |
var |
| 41 |
lib: THandle; |
| 42 |
{$ENDIF} |
| 43 |
begin |
| 44 |
{$IFNDEF WINCE} |
| 45 |
{ on Vista and newer there is a GetTickCount64 implementation } |
| 46 |
if Win32MajorVersion >= 6 then begin |
| 47 |
if not Assigned(WinGetTickCount64) then begin |
| 48 |
lib := LoadLibrary('kernel32.dll'); |
| 49 |
WinGetTickCount64 := TGetTickCount64( |
| 50 |
GetProcAddress(lib, 'GetTickCount64')); |
| 51 |
end; |
| 52 |
Result := WinGetTickCount64(); |
| 53 |
end else |
| 54 |
{$ENDIF} |
| 55 |
Result := Windows.GetTickCount; |
| 56 |
end; |
| 57 |
{$endif MSWINDOWS} |
| 58 |
|
| 59 |
{ Nintendo Switch ------------------------------------------------------------ } |
| 60 |
|
| 61 |
{$ifdef CASTLE_NINTENDO_SWITCH} |
| 62 |
function CgeNxGetTickCount64(): QWord; cdecl; external; |
| 63 |
|
| 64 |
function CastleGetTickCount64: TMilisecTime; |
| 65 |
begin |
| 66 |
Result := CgeNxGetTickCount64; |
| 67 |
end; |
| 68 |
{$endif} |
| 69 |
|
| 70 |
{ Unix (except Nintendo Switch) ---------------------------------------------- } |
| 71 |
|
| 72 |
{$if defined(UNIX) and not defined(CASTLE_NINTENDO_SWITCH)} |
| 73 |
|
| 74 |
{ GetTickCount64 for Unix. |
| 75 |
Not based on, but in fact very similar idea as the one in |
| 76 |
FPC fpc/3.0.0/src/rtl/unix/sysutils.pp } |
| 77 |
|
| 78 |
var |
| 79 |
{$warnings off} // knowingly using deprecated stuff |
| 80 |
LastGetTickCount64: TMilisecTime; |
| 81 |
{$warnings on} |
| 82 |
|
| 83 |
{$ifndef FPC} |
| 84 |
type |
| 85 |
TTimeVal = TimeVal; |
| 86 |
{$endif} |
| 87 |
|
| 88 |
{$I norqcheckbegin.inc} |
| 89 |
function CastleGetTickCount64: TMilisecTime; |
| 90 |
var |
| 91 |
timeval: TTimeVal; |
| 92 |
begin |
| 93 |
{$ifdef FPC} |
| 94 |
FpGettimeofday(@timeval, nil); |
| 95 |
{$else} |
| 96 |
Gettimeofday(timeval, nil); |
| 97 |
{$endif} |
| 98 |
|
| 99 |
{ By doing tv_sec * 1000, we reject 3 most significant digits from tv_sec. |
| 100 |
That's Ok, since these digits change least often. |
| 101 |
And this way we get the 3 least significant digits to fill |
| 102 |
with tv_usec div 1000 (which must be < 1000, because tv_usec must be < 1 million). } |
| 103 |
|
| 104 |
Result := QWord(timeval.tv_sec) * 1000 + (timeval.tv_usec div 1000); |
| 105 |
|
| 106 |
{ We cannot trust some Android systems to return increasing values here |
| 107 |
(Android device "Moto X Play", "XT1562", OS version 5.1.1). |
| 108 |
Maybe they synchronize the time from the Internet, and do not take care |
| 109 |
to keep it monotonic (unlike https://lwn.net/Articles/23313/ says?) } |
| 110 |
|
| 111 |
if Result < LastGetTickCount64 then |
| 112 |
begin |
| 113 |
WritelnLog('Time', 'Detected gettimeofday() going backwards on Unix, workarounding. This is known to happen on some Android devices'); |
| 114 |
Result := LastGetTickCount64; |
| 115 |
end else |
| 116 |
LastGetTickCount64 := Result; |
| 117 |
end; |
| 118 |
{$I norqcheckend.inc} |
| 119 |
{$endif UNIX} |
| 120 |
|
| 121 |
{$endif} |