-
Notifications
You must be signed in to change notification settings - Fork 2
/
PCTimer.cpp
190 lines (153 loc) · 2.92 KB
/
PCTimer.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "PCTimer.h"
#include "non_win32.h"
#include "utils.h"
#include "validate.h"
EXPORT i32 gPcTimerPaused;
EXPORT u32 gTimerInitOne;
EXPORT u32 gTimerInitTwo;
#ifndef _WIN32
#define LPTIMECALLBACK void*
#endif
EXPORT LPTIMECALLBACK fptc;
EXPORT double gTimerMsInterval;
EXPORT STimerInfo gTimerInfo;
EXPORT double gTimerVblankRelated;
// @FIXME
#ifndef _WIN32
struct TIMECAPS
{
u32 wPeriodMin;
u32 wPeriodMax;
};
void onexit(...)
{
}
void timeKillEvent(UINT)
{
}
u32 timeSetEvent(...)
{
return 69;
}
void timeBeginPeriod(...)
{
}
bool timeGetDevCaps(...)
{
}
#endif
TIMECAPS ptc;
// @Ok
void PCTIMER_Init(void)
{
gTimerInfo.uTimerID = 0;
gTimerInfo.field_4 = 0;
ptc.wPeriodMin = 0;
gTimerInfo.uPeriod = 0;
gTimerInfo.field_C = 0;
ptc.wPeriodMax = 0;
fptc = 0;
if (timeGetDevCaps(&ptc, sizeof(ptc)))
{
gTimerInfo.uTimerID = 0;
print_if_false(0, "\t\tD3DTimer init error!");
return;
}
UINT wPeriodMin = ptc.wPeriodMin;
if (wPeriodMin <= 16)
wPeriodMin = 16;
UINT wPeriodMax;
if (wPeriodMin < ptc.wPeriodMax)
{
if (ptc.wPeriodMin > 16)
{
wPeriodMax = ptc.wPeriodMin;
}
else
{
wPeriodMax = 16;
}
}
else
{
wPeriodMax = ptc.wPeriodMax;
}
gTimerInfo.uPeriod = wPeriodMax;
printf(
"t\tPCTimer - Min: %i, Max: %i, Ideal: %i, Used:%i\r\n",
ptc.wPeriodMin,
ptc.wPeriodMax,
16,
wPeriodMax);
u32 uResolution = gTimerInfo.uPeriod;
u32 uDelay = 16;
gTimerInfo.field_4 = 16;
// make clang happy :)
#ifdef _WIN32
fptc = TimerCallback;
#else
fptc = reinterpret_cast<void*>(TimerCallback);
#endif
if (gTimerInfo.uPeriod > 0x10)
{
print_if_false(0, "Timer low resolution error!");
uResolution = gTimerInfo.uPeriod;
uDelay = gTimerInfo.uPeriod;
gTimerInfo.field_4 = gTimerInfo.uPeriod;
}
gTimerInfo.uTimerID = timeSetEvent(uDelay, uResolution, fptc, (DWORD)&gTimerInfo, 1);
gTimerMsInterval = (double)gTimerInfo.field_4 * 60.0 / 1000.0;
timeBeginPeriod(gTimerInfo.uPeriod);
onexit(PCTIMER_Kill);
}
// @Ok
// @Matching
i32 PCTIMER_Kill(void)
{
if (gTimerInfo.uTimerID)
{
timeKillEvent(gTimerInfo.uTimerID);
gTimerInfo.uTimerID = 0;
}
return 0;
}
// @Ok
// @Matching
void PCTIMER_Pause(void)
{
gPcTimerPaused = 1;
}
// @Ok
// @Matching
void PCTIMER_Resume(void)
{
gPcTimerPaused = 0;
}
// @Ok
// @Matching
void CALLBACK TimerCallback(
UINT uTimerID,
UINT uMsg,
unsigned long dwUser,
unsigned long dw1,
unsigned long dw2)
{
STimerInfo* pTimeInfo = reinterpret_cast<STimerInfo*>(dwUser);
if (!gPcTimerPaused)
{
gTimerMsInterval = (double)pTimeInfo->field_4 * 60.0 / 1000.0;
pTimeInfo->field_C += pTimeInfo->field_4;
gTimerVblankRelated += gTimerMsInterval;
u32 newTime = gTimerVblankRelated;
while ( newTime > Vblanks )
MyVSync();
}
}
void validate_STimerInfo(void)
{
VALIDATE_SIZE(STimerInfo, 0x10);
VALIDATE(STimerInfo, uTimerID, 0x0);
VALIDATE(STimerInfo, field_4, 0x4);
VALIDATE(STimerInfo, uPeriod, 0x8);
VALIDATE(STimerInfo, field_C, 0xC);
}