| 1 |
{ |
| 2 |
Copyright 2008-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ X3D time. } |
| 17 |
unit X3DTime; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Generics.Collections, |
| 24 |
CastleUtils, CastleTimeUtils; |
| 25 |
|
| 26 |
type |
| 27 |
{ Complete timestamp for X3D events. |
| 28 |
For most purposes, you're interested only in it's @link(Seconds) field, |
| 29 |
this is the amount of continuous time that passed (in double-precision |
| 30 |
seconds). |
| 31 |
|
| 32 |
More precisely, timestamp is actually a sum of @italic(Seconds + |
| 33 |
PlusTicks ticks). PlusTicks is the number of discrete "ticks" that |
| 34 |
occurred with the same Seconds value. A tick is, conceptually, an |
| 35 |
infinitely small amount of time. |
| 36 |
|
| 37 |
Ticks are used when comparing time values: when two timestamps |
| 38 |
have the same @link(Seconds) value, then we can compare the PlusTicks |
| 39 |
field. Timestamps are equal only when both Seconds and PlusTicks values |
| 40 |
are exactly equal. |
| 41 |
|
| 42 |
The practical justification of PlusTicks usage: |
| 43 |
|
| 44 |
@orderedList( |
| 45 |
@item(First, note that |
| 46 |
X3D standard defines that within one timestamp, only one event may |
| 47 |
pass through one route. This mechanism allows to avoid loops in routes.) |
| 48 |
|
| 49 |
@item(Now note that when using TCastleSceneCore, Seconds is increased by |
| 50 |
TCastleSceneCore.IncreaseTime. TCastleSceneCore doesn't require how often |
| 51 |
should IncreaseTime be called, in particular you can call multiple |
| 52 |
times TCastleSceneCore.Press, TCastleSceneCore.Release, TCastleSceneCore.PointingDeviceMove |
| 53 |
without continuously updating time. You can even not update time at all, |
| 54 |
and still call TCastleSceneCore.Press and such. |
| 55 |
|
| 56 |
This is a good thing --- it allows TCastleSceneCore to be very flexible. |
| 57 |
The idea is that sensors are activated when user interface reports |
| 58 |
some event. You don't have to update time before every TCastleSceneCore.Press |
| 59 |
and such.) |
| 60 |
|
| 61 |
@item(The potential problem here is that when you call |
| 62 |
TCastleSceneCore.Press twice, without the TCastleSceneCore.IncreaseTime |
| 63 |
in between, then the second TCastleSceneCore.Press events will have "clogged" routes. |
| 64 |
Events send from the second TCastleSceneCore.Press may be blocked on routes, |
| 65 |
since they will be detected as occuring within the same timestamp, |
| 66 |
so (following VRML/X3D standard) they'll have to be ignored.) |
| 67 |
|
| 68 |
@item(Using "Seconds seconds + PlusTicks ticks" allows to avoid this. |
| 69 |
Each TCastleSceneCore.Press and such increases world time by 1 tick |
| 70 |
-- this way, the second TCastleSceneCore.Press will have one more tick, so will |
| 71 |
always be considered later, and things will work Ok.) |
| 72 |
) } |
| 73 |
TX3DTime = record |
| 74 |
Seconds: TFloatTime; |
| 75 |
PlusTicks: Cardinal; |
| 76 |
|
| 77 |
class function Oldest: TX3DTime; static; |
| 78 |
|
| 79 |
class operator {$ifdef FPC}>{$else}GreaterThan{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 80 |
class operator {$ifdef FPC}>={$else}GreaterThanOrEqual{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 81 |
class operator {$ifdef FPC}<{$else}LessThan{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 82 |
class operator {$ifdef FPC}<={$else}LessThanOrEqual{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 83 |
end; |
| 84 |
PX3DTime = ^TX3DTime; |
| 85 |
|
| 86 |
TFloatTime = CastleTimeUtils.TFloatTime; |
| 87 |
|
| 88 |
const |
| 89 |
OldestX3DTime: TX3DTime = (Seconds: OldestTime; PlusTicks: 0) |
| 90 |
deprecated 'use TX3DTime.Oldest'; |
| 91 |
|
| 92 |
type |
| 93 |
TX3DTimeList = {$ifdef FPC}specialize{$endif} TStructList<TX3DTime>; |
| 94 |
|
| 95 |
implementation |
| 96 |
|
| 97 |
class function TX3DTime.Oldest: TX3DTime; |
| 98 |
const |
| 99 |
R: TX3DTime = (Seconds: OldestTime; PlusTicks: 0); |
| 100 |
begin |
| 101 |
Result := R; |
| 102 |
end; |
| 103 |
|
| 104 |
class operator TX3DTime.{$ifdef FPC}>{$else}GreaterThan{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 105 |
begin |
| 106 |
Result := (Time1.Seconds > Time2.Seconds) or |
| 107 |
( (Time1.Seconds = Time2.Seconds) and |
| 108 |
(Time1.PlusTicks > Time2.PlusTicks) ); |
| 109 |
end; |
| 110 |
|
| 111 |
class operator TX3DTime.{$ifdef FPC}>={$else}GreaterThanOrEqual{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 112 |
begin |
| 113 |
Result := (Time1.Seconds > Time2.Seconds) or |
| 114 |
( (Time1.Seconds = Time2.Seconds) and |
| 115 |
(Time1.PlusTicks >= Time2.PlusTicks) ); |
| 116 |
end; |
| 117 |
|
| 118 |
class operator TX3DTime.{$ifdef FPC}<{$else}LessThan{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 119 |
begin |
| 120 |
Result := (Time1.Seconds < Time2.Seconds) or |
| 121 |
( (Time1.Seconds = Time2.Seconds) and |
| 122 |
(Time1.PlusTicks < Time2.PlusTicks) ); |
| 123 |
end; |
| 124 |
|
| 125 |
class operator TX3DTime.{$ifdef FPC}<={$else}LessThanOrEqual{$endif} (const Time1, Time2: TX3DTime): boolean; |
| 126 |
begin |
| 127 |
Result := (Time1.Seconds < Time2.Seconds) or |
| 128 |
( (Time1.Seconds = Time2.Seconds) and |
| 129 |
(Time1.PlusTicks <= Time2.PlusTicks) ); |
| 130 |
end; |
| 131 |
|
| 132 |
end. |