aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UTime.pas
blob: 87d4f922b3c523a6fe21969c55ea5ba3b2974cb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
unit UTime;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

type
  TTime = class
    constructor Create;
    function GetTime: real;
  end;

procedure CountSkipTimeSet;
procedure CountSkipTime;
procedure CountMidTime;
procedure TimeSleep(ms: real);

var
  USTime:   TTime;
  
  TimeFreq: int64;
  TimeNew:  int64;
  TimeOld:  int64;
  TimeSkip: real;
  TimeMid:  real;
  TimeMidTemp:  int64;

implementation

uses
  {$IFDEF win32}
    windows,
  {$ELSE}
  libc,
  time,
  {$ENDIF}
  ucommon;


// -- ON Linux it MAY Be better to use ...   clock_gettime() instead of  CurrentSec100OfDay
// who knows how fast or slow that function is !
// but this gets a compile for now .. :)

constructor TTime.Create;
begin
  CountSkipTimeSet;
end;

procedure CountSkipTimeSet;
begin
  {$IFDEF win32}
  QueryPerformanceFrequency(TimeFreq);
  QueryPerformanceCounter(TimeNew);
  {$ELSE}
  TimeNew  := CurrentSec100OfDay();     // TODO - JB_Linux will prob need looking at
  TimeFreq := 0;
  {$ENDIF}
end;

procedure CountSkipTime;
begin
  TimeOld := TimeNew;
  
  {$IFDEF win32}
  QueryPerformanceCounter(TimeNew);
  {$ELSE}
  TimeNew := CurrentSec100OfDay();    // TODO - JB_Linux will prob need looking at
  {$ENDIF}
  
  TimeSkip := (TimeNew-TimeOld)/TimeFreq;
end;

procedure CountMidTime;
begin
  {$IFDEF win32}
  QueryPerformanceCounter(TimeMidTemp);
  TimeMid := (TimeMidTemp-TimeNew)/TimeFreq;
  {$ELSE}
  TimeMidTemp := CurrentSec100OfDay();
  TimeMid     := (TimeMidTemp-TimeNew);      // TODO - JB_Linux will prob need looking at
  {$ENDIF}
end;

procedure TimeSleep(ms: real);
var
  TimeStart:  int64;
  TimeHalf:   int64;
  Time:       real;
  Stop:       boolean;
begin
  {$IFDEF win32}
  QueryPerformanceCounter(TimeStart);
  {$ELSE}
  TimeStart := CurrentSec100OfDay();   // TODO - JB_Linux will prob need looking at
  {$ENDIF}


  Stop := false;
  while (not Stop) do
  begin
    {$IFDEF win32}
    QueryPerformanceCounter(TimeHalf);
    Time := 1000 * (TimeHalf-TimeStart)/TimeFreq;
    {$ELSE}
    TimeHalf := CurrentSec100OfDay();
    Time := 1000 * (TimeHalf-TimeStart); // TODO - JB_Linux will prob need looking at
    {$ENDIF}

    if Time > ms then
      Stop := true;
  end;

end;

function TTime.GetTime: real;
var
  TimeTemp:   int64;
begin
  {$IFDEF win32}
    QueryPerformanceCounter(TimeTemp);
    Result := TimeTemp / TimeFreq;
  {$ELSE}
    TimeTemp := CurrentSec100OfDay();
    Result   := TimeTemp;  // TODO - JB_Linux will prob need looking at
  {$ENDIF}
end;


end.