| 1 |
{%MainUnit castletimeutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2000-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 |
{ Part of CastleTimeUtils unit: small and miscellaneous definitions. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Time in seconds. This is used throughout my engine to represent time |
| 23 |
as a floating-point value with good accuracy in seconds. |
| 24 |
|
| 25 |
Using the "double" precision (not just "single") is good to guarantee |
| 26 |
good accuracy. It is also the precision required for storing time in X3D. |
| 27 |
See also: |
| 28 |
https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/ |
| 29 |
https://twitter.com/ID_AA_Carmack/status/418158611664097280 |
| 30 |
|
| 31 |
To test that "single" is not enough, open some animation in |
| 32 |
castle-model-viewer, and change "on display" time pass to 1000. |
| 33 |
It goes even better if AutoRedisplay is @false, and LimitFps is 0.0, |
| 34 |
and model is still for some time --- then we do many OnUpdate calls with |
| 35 |
very small SecondsPassed values. } |
| 36 |
TFloatTime = Double; |
| 37 |
|
| 38 |
const |
| 39 |
OldestTime = -MaxDouble; |
| 40 |
|
| 41 |
type |
| 42 |
{ @deprecated |
| 43 |
To measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds } |
| 44 |
TMilisecTime = QWord |
| 45 |
{$ifdef FPC} |
| 46 |
// This works in Delphi too, but is too noisy |
| 47 |
deprecated 'To measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds' |
| 48 |
{$endif}; |
| 49 |
|
| 50 |
{ Check is SecondTime larger by at least TimeDelay than FirstTime. |
| 51 |
|
| 52 |
Simple implementation of this would be @code(SecondTime - FirstTime >= TimeDelay). |
| 53 |
|
| 54 |
FirstTime and SecondTime are milisecond times from some initial point. |
| 55 |
For example, they may be taken from a function like 32-bit GetTickCount |
| 56 |
(but you actually should use GetTickCount64 with new compilers, never 32-bit GetTickCount). |
| 57 |
Such time may "wrap". |
| 58 |
This function checks these times intelligently, using the assumption that |
| 59 |
the SecondTime is always "later" than the FirstTime, and only having to check |
| 60 |
if it's later by at least TimeDelay. |
| 61 |
|
| 62 |
Always TimeTickSecond(X, X, 0) = @true. that is, when both times |
| 63 |
are actually equal, it's correctly "later by zero miliseconds". } |
| 64 |
function TimeTickSecondLater(const FirstTime, SecondTime, TimeDelay: TMilisecTime): boolean; |
| 65 |
deprecated 'to measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds'; |
| 66 |
|
| 67 |
{ Difference in times between SecondTime and FirstTime. |
| 68 |
|
| 69 |
Naive implementation would be just @code(SecondTime - FirstTime), |
| 70 |
this function does a little better: takes into account that times may "wrap" |
| 71 |
(see TimeTickSecondLater), and uses the assumption that |
| 72 |
the SecondTime for sure "later", to calculate hopefully correct difference. } |
| 73 |
function TimeTickDiff(const FirstTime, SecondTime: TMilisecTime): TMilisecTime; |
| 74 |
deprecated 'to measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds. Also, this function has non-intuitive argument order, inconsistent with ProcessTimerSeconds and TimerSeconds'; |
| 75 |
|
| 76 |
{ Simply add and subtract two TMilisecTime values. |
| 77 |
|
| 78 |
These don't care if TMilisecTime is a point in time, or time interval. |
| 79 |
They simply add / subtract values. It's your problem if adding / subtracting |
| 80 |
them is sensible. |
| 81 |
|
| 82 |
Range checking is ignored. In particular, this means that if you subtract |
| 83 |
smaller value from larger value, the result will be like the time "wrapped" |
| 84 |
in between (since TMilisecTime range is limited). |
| 85 |
|
| 86 |
@groupBegin } |
| 87 |
function MilisecTimesAdd(const t1, t2: TMilisecTime): TMilisecTime; |
| 88 |
deprecated 'to measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds'; |
| 89 |
function MilisecTimesSubtract(const t1, t2: TMilisecTime): TMilisecTime; |
| 90 |
deprecated 'to measure time, better use Timer + TimerSeconds or ProcessTimer + ProcessTimerSeconds'; |
| 91 |
{ @groupEnd } |
| 92 |
|
| 93 |
const |
| 94 |
MinDateTime: TDateTime = MinDouble; |
| 95 |
|
| 96 |
{ Convert DateTime to string in the form "date at time". } |
| 97 |
function DateTimeToAtStr(const DateTime: TDateTime): string; |
| 98 |
|
| 99 |
{$endif read_interface} |
| 100 |
|
| 101 |
{$ifdef read_implementation} |
| 102 |
|
| 103 |
function TimeTickSecondLater(const FirstTime, SecondTime, TimeDelay: TMilisecTime): boolean; |
| 104 |
var |
| 105 |
SecondTimeMinusDelay: Int64; |
| 106 |
begin |
| 107 |
if FirstTime > SecondTime then |
| 108 |
WritelnLog('Time', 'FirstTime > SecondTime for TimeTickSecondLater. Maybe 32-bit GetTickCount just wrapped (Windows XP? Otherwise, 64-bit GetTickCount64 should always be used), or maybe you swapped arguments for TimeTickSecondLater.'); |
| 109 |
{ Need 64 bit signed int to hold the result of QWord - QWord } |
| 110 |
{$I norqcheckbegin.inc} |
| 111 |
SecondTimeMinusDelay := SecondTime - TimeDelay; |
| 112 |
{$I norqcheckend.inc} |
| 113 |
if SecondTimeMinusDelay < 0 then |
| 114 |
begin |
| 115 |
// detected Windows with 32-bit GetTickCount, it just wrapped, fix |
| 116 |
SecondTimeMinusDelay := SecondTimeMinusDelay + High(UInt32); |
| 117 |
result := (FirstTime > SecondTime) and (FirstTime <= SecondTimeMinusDelay); |
| 118 |
end else |
| 119 |
result := FirstTime <= SecondTimeMinusDelay; |
| 120 |
end; |
| 121 |
|
| 122 |
function TimeTickDiff(const FirstTime, SecondTime: TMilisecTime): TMilisecTime; |
| 123 |
begin |
| 124 |
{$warnings off} // knowingly using deprecated stuff in another deprecated |
| 125 |
result := MilisecTimesSubtract(SecondTime, FirstTime); |
| 126 |
{$warnings on} |
| 127 |
{old implementation : |
| 128 |
|
| 129 |
if FirstTime <= SecondTime then |
| 130 |
result := SecondTime-FirstTime else |
| 131 |
result := High(UInt32) -FirstTime +SecondTime; |
| 132 |
} |
| 133 |
end; |
| 134 |
|
| 135 |
{$I norqcheckbegin.inc} |
| 136 |
function MilisecTimesAdd(const t1, t2: TMilisecTime): TMilisecTime; |
| 137 |
begin result := t1+t2 end; |
| 138 |
|
| 139 |
function MilisecTimesSubtract(const t1, t2: TMilisecTime): TMilisecTime; |
| 140 |
begin result := t1-t2 end; |
| 141 |
{$I norqcheckend.inc} |
| 142 |
|
| 143 |
function DateTimeToAtStr(const DateTime: TDateTime): string; |
| 144 |
begin |
| 145 |
Result := FormatDateTime('yyyy"-"mm"-"dd" at "tt', DateTime); |
| 146 |
end; |
| 147 |
|
| 148 |
{$endif read_implementation} |