-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.System.Cadencer.pas
116 lines (89 loc) · 2.34 KB
/
GS.System.Cadencer.pas
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
unit GS.System.Cadencer;
interface
Uses Classes,
Sysutils,
GS.System.CPU;
Type
TGSCadencer = class(TObject)
private
FLastCall : Cardinal;
FTimeScaler: Double;
FDeltaTime : Cardinal;
FTic : Cardinal;
FCallBySec : Cardinal;
FInternalCallBySec : Cardinal;
FMCSec : Cardinal;
FSecFromStart : Cardinal;
FStartTime : Cardinal;
FTotalCallBySecond : Cardinal;
function GetSystemTick: Cardinal;
function GetStartTime: Cardinal;
Public
constructor Create;
Procedure Update;
Procedure Reset;
Function TimeSliceValue(aValueToSlice : Double) : Double;
property TimeScaler : Double read FTimeScaler Write FTimeScaler;
Property Tic : Cardinal read FTic;
Property DeltaTime : Cardinal read FDeltaTime;
Property CallBySecond : Cardinal read FCallBySec;
property TimeElapsedSinceStart : Cardinal read GetStartTime;
property TotalCallSinceStart : Cardinal read FTotalCallBySecond;
end;
var
GSGlobalCadencer : TGSCadencer;
Implementation
{ TGSCadencer }
constructor TGSCadencer.Create;
begin
Inherited;
Reset;
FSecFromStart := 0;
FTotalCallBySecond := 0;
FStartTime := gsGetTickCount;
end;
function TGSCadencer.TimeSliceValue(aValueToSlice: Double): Double;
begin
Result := aValueToSlice * FDeltaTime / 1000; //div by 1000, cause we assume that value is by seconds.
end;
procedure TGSCadencer.Update;
var h : cardinal;
begin
FTic := gsGetTickCount;
FDeltaTime := Trunc((FTic - FLastCall) * FTimeScaler);
FLastCall := FTic;
h := Round(Ftic/1000);
if h<>FMCSec then
begin
FMCSec := h;
FCallBySec := FInternalCallBySec;
FTotalCallBySecond := FTotalCallBySecond + FInternalCallBySec;
FInternalCallBySec:=0;
Inc(FSecFromStart);
end
else
begin
Inc(FInternalCallBySec);
end;
end;
procedure TGSCadencer.Reset;
begin
TimeScaler := 1; //Realtime by Default. 1 sec real world = 1 sec in game.
FLastCall := gsGetTickCount;
FInternalCallBySec := 0;
FMCSec := 0;
end;
function TGSCadencer.GetStartTime: Cardinal;
begin
result := gsGetTickCount - FStartTime;
end;
function TGSCadencer.GetSystemTick: Cardinal;
begin
result := Ftic;
end;
Initialization;
GSGlobalCadencer := TGSCadencer.Create;
GSGlobalCadencer.Reset;
finalization
GSGlobalCadencer.Free;
end.