-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDirectX 9.cpp
349 lines (294 loc) · 10.2 KB
/
DirectX 9.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//#define USETHREADS //spawn a second renderer thread and use busy message reading for input
#define MODEZERO //comment out for human benchmark mode, uncomment for black and white
//#define FPSLIMIT 4000 //comment out for uncapped FPS
//set these for different viewport resolution, for example
//#define HEIGHT 480
//#define WIDTH 640
#define HEIGHT GetSystemMetrics(SM_CYSCREEN)
#define WIDTH GetSystemMetrics(SM_CXSCREEN)
//<----------- no need to change below this point ----------->
#include <d3d9.h>
//if linker can't find DirectX SDK, you can use these
/*#ifdef _M_IX86 //32 bit compile
#pragma comment (lib, "C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Lib/x86/d3d9.lib")
#else
#pragma comment (lib, "C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Lib/x64/d3d9.lib")
#endif*/
D3DCOLOR black = D3DCOLOR(0x00000000);
D3DCOLOR white = D3DCOLOR(0x00FFFFFF);
#ifdef MODEZERO
D3DCOLOR currentColor = white;
#else
#include <iostream>
#include <iomanip> // std::setprecision
#include <chrono> //used for time
D3DCOLOR currentColor = black;
D3DCOLOR red = D3DCOLOR(0x00FF0000);
D3DCOLOR green = D3DCOLOR(0x0000FF00);
auto redToGreenTime = std::chrono::high_resolution_clock::now();
auto greenStartTime = redToGreenTime;
auto greenClickTime = redToGreenTime;
auto redStartTime = redToGreenTime;
int state = 0; //0 stats screen, 1 red waiting screen, 2 green
int errors = 0;
int click = 0;
long long lastClick, minClick = 999999999999, maxClick = 0, averageClick = 0, clickSum = 0, clickAmount = 0;
#endif
#ifdef USETHREADS
#include <thread>
#endif
#ifdef FPSLIMIT
#include <chrono> //used for time
float frameTimeMicro = FPSLIMIT == 0 ? 0 : 1000000 / FPSLIMIT;
auto start = std::chrono::high_resolution_clock::now();
#else
float frameTimeMicro = 0;
#endif
bool stop = false;
long long microseconds = 1;
UINT sizeofRAWINPUTHEADER = sizeof(RAWINPUTHEADER);
RAWINPUT* raw_buf = (PRAWINPUT)malloc(800); //this dirty hack probably saves nanoseconds. worth it.
UINT cb_size = 0;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INPUT: {
//get the size of the RAWINPUT structure returned
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &cb_size, sizeofRAWINPUTHEADER);
//allocate memory RAWINPUT structure
//raw_buf = (PRAWINPUT)malloc(cb_size);
//if (!raw_buf) { stop = TRUE; return false; }
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, raw_buf, &cb_size, sizeofRAWINPUTHEADER);
if (raw_buf->header.dwType == RIM_TYPEMOUSE &&
(raw_buf->data.mouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_DOWN ||
raw_buf->data.mouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_DOWN)) { //only triggers once per button press
#ifdef MODEZERO
if (currentColor == white)
currentColor = black;
else
currentColor = white;
#endif
#ifndef MODEZERO
if (state == 0) {
system("CLS"); //clear console
state = 1;
currentColor = red;
//pick a time to transition screen from red to green
int rnd = rand() % 1000 + 3000; //(between 3 and 4 seconds)
auto now = std::chrono::high_resolution_clock::now();
redToGreenTime = now + std::chrono::milliseconds(rnd);
redStartTime = now;
}
else if (state == 1) { //too early click
auto now = std::chrono::high_resolution_clock::now();
auto timeDiff = now - redStartTime;
if (std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count() >= 500000) { //grace period for double clicks
state = 0;
currentColor = black;
errors++;
}
}
else if (state == 2) {
state = 0;
currentColor = black;
greenClickTime = std::chrono::high_resolution_clock::now();
auto timeDiff = greenClickTime - greenStartTime;
lastClick = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count();
if (lastClick < minClick) minClick = lastClick;
if (lastClick > maxClick) maxClick = lastClick;
clickSum += lastClick;
clickAmount++;
averageClick = clickSum / clickAmount;
std::cout << "\n";
std::cout << " last click: " << std::fixed << std::setprecision(1) << (double)lastClick / 1000 << " ms\n";
std::cout << " min click: " << (double)minClick / 1000 << " ms\n";
std::cout << " max click: " << (double)maxClick / 1000 << " ms\n";
std::cout << " average click: " << (double)averageClick / 1000 << " ms\n";
std::cout << " successful clicks: " << clickAmount << "\n";
std::cout << " early clicks: " << errors << "\n";
}
#endif
}
#ifndef MODEZERO
if (raw_buf->header.dwType == RIM_TYPEKEYBOARD) {
#endif
if (raw_buf->data.keyboard.Message == WM_KEYDOWN || raw_buf->data.keyboard.Message == WM_SYSKEYDOWN)
#ifdef MODEZERO
if (raw_buf->data.keyboard.VKey == 0x57) // W key
currentColor = white;
else if (raw_buf->data.keyboard.VKey == 0x42) // B key
currentColor = black;
else
#endif
if (raw_buf->data.keyboard.VKey == 0x1B) { // ESC key
stop = TRUE;
break;
}
#ifndef MODEZERO
}
#endif
if (GET_RAWINPUT_CODE_WPARAM(wParam) == RIM_INPUT)
DefWindowProc(hWnd, msg, wParam, lParam); //The application must call DefWindowProc so the system can perform cleanup.
break;
}
case WM_CLOSE:
stop = TRUE;
DestroyWindow(hWnd);
return DefWindowProc(hWnd, msg, wParam, lParam);
case WM_DESTROY:
stop = TRUE;
PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
case WM_QUIT:
stop = TRUE;
return DefWindowProc(hWnd, msg, wParam, lParam);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int main() //for console purposes.
{
int ret = WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOWNORMAL);
return ret;
}
inline void renderFunc(LPDIRECT3DDEVICE9 d3ddev) {
#ifdef USETHREADS
do {
#endif
#ifdef FPSLIMIT
auto elapsed = std::chrono::high_resolution_clock::now() - start;
microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
#endif
#ifndef MODEZERO
if (state == 1 && std::chrono::high_resolution_clock::now() >= redToGreenTime) {
state = 2;
currentColor = green;
greenStartTime = std::chrono::high_resolution_clock::now(); //keep actual green start timer
}
#endif
#ifdef FPSLIMIT
if (microseconds >= frameTimeMicro) {
start = std::chrono::high_resolution_clock::now();
#endif
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, currentColor, 0.0f, 0);
d3ddev->Present(NULL, NULL, NULL, NULL);
#ifdef FPSLIMIT
}
#endif
#ifdef USETHREADS
} while (!stop);
#endif
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
MSG msg;
HRESULT res;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"somewindowclass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(0, L"somewindowclass", L"test",
WS_EX_TOPMOST | WS_POPUP,
0, 0, (UINT)GetSystemMetrics(SM_CXSCREEN), (UINT)GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
d3d = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = (UINT)GetSystemMetrics(SM_CXSCREEN);
d3dpp.BackBufferHeight = (UINT)GetSystemMetrics(SM_CYSCREEN);
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //disable vsync
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
D3DVIEWPORT9 pViewport = { 0, 0, (DWORD)WIDTH, (DWORD)HEIGHT,0.0,1.0 };
res = d3ddev->SetViewport(&pViewport);
if (res != S_OK) return -1;
if (d3ddev == NULL) return -1;
RAWINPUTDEVICE Keyboard;
Keyboard.usUsagePage = 0x01;
Keyboard.usUsage = 0x06; //keyboard
Keyboard.dwFlags = RIDEV_NOLEGACY;
Keyboard.hwndTarget = hWnd;
if (!RegisterRawInputDevices(&Keyboard, 1, sizeof(RAWINPUTDEVICE))) return -1;
//#ifndef MODEZERO
RAWINPUTDEVICE Mouse;
Mouse.usUsagePage = 0x01;
Mouse.usUsage = 0x02; //mouse
Mouse.dwFlags = RIDEV_NOLEGACY;
Mouse.hwndTarget = hWnd;
if (!RegisterRawInputDevices(&Mouse, 1, sizeof(RAWINPUTDEVICE))) return -1;
//#endif
ShowCursor(FALSE);
SetCursor(NULL);
d3ddev->ShowCursor(FALSE);
//using System;
//using System.Diagnostics;
//using System.Threading;
//#include <sched.h> // sched_setaffinity
//using System.Windows.Forms;
HANDLE process = GetCurrentProcess();
DWORD_PTR processAffinityMask;
DWORD_PTR systemAffinityMask;
if (!GetProcessAffinityMask(process, &processAffinityMask, &systemAffinityMask))
return -1;
int core = 4; /* set this to the core you want your process to run on */
DWORD_PTR mask = 0x1;
for (int bit = 0, currentCore = 1; bit < 64; bit++)
{
if (mask & processAffinityMask)
{
if (currentCore != core)
{
processAffinityMask &= ~mask;
}
currentCore++;
}
mask = mask << 1;
}
//BOOL success = SetProcessAffinityMask(process, processAffinityMask);
SetPriorityClass(process, REALTIME_PRIORITY_CLASS);
//SetPriorityClass(process, HIGH_PRIORITY_CLASS);
//Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2); // Use only the second core
//Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; // Set highest process priority
//Thread.CurrentThread.Priority = ThreadPriority.Highest; // Set highest thread priority
#ifdef USETHREADS
std::thread renderThread; //declare a thread without launching it
renderThread = std::thread(renderFunc, d3ddev); //this launches the thread
#endif
while (!stop) {
#ifdef USETHREADS
GetMessage(&msg, NULL, 0, 0);
DispatchMessage(&msg);
#else
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) //unthreaded - one time check per loop
DispatchMessage(&msg);
renderFunc(d3ddev); //draw frame
#endif
}
#ifdef USETHREADS
renderThread.join(); //var 'stop' ends the thread
#endif
d3ddev->Release(); //release d3d
d3d->Release();
free(raw_buf);
DestroyWindow(hWnd);
PostQuitMessage(0);
ShowCursor(true);
#ifndef MODEZERO
std::cout << "\nPress Enter to Continue\n";
getchar();
#endif
return 0; //all done
}