1
+ #define _CRT_SECURE_NO_WARNINGS
2
+
3
+ #include < Windows.h>
4
+ #include < stdio.h>
5
+ #include < iostream>
6
+
7
+ #define DIRECTINPUT_VERSION 0x0800
8
+ #include < d3d9.h>
9
+ #pragma comment(lib,"d3d9.lib")
10
+ #include < dinput.h>
11
+ #include < tchar.h>
12
+
13
+ #include " ../include/renderer.h"
14
+
15
+ static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL ;
16
+ static D3DPRESENT_PARAMETERS g_d3dpp;
17
+
18
+ short GetUsageOfCPU ()
19
+ {
20
+ const static HANDLE hCurrentProcess = GetCurrentProcess ();
21
+
22
+ static DWORD dwNumberOfProcessors = 0 ;
23
+
24
+ if (!dwNumberOfProcessors)
25
+ {
26
+ SYSTEM_INFO info;
27
+ GetSystemInfo (&info);
28
+ dwNumberOfProcessors = info.dwNumberOfProcessors ;
29
+ }
30
+
31
+ FILETIME now, creation_time, exit_time, kernel_time, user_time;
32
+
33
+ GetSystemTimeAsFileTime (&now);
34
+
35
+ if (!GetProcessTimes (hCurrentProcess, &creation_time, &exit_time, &kernel_time, &user_time))
36
+ return -1 ;
37
+
38
+ static auto QuadPartFt = [](const FILETIME* ft) -> uint64_t
39
+ {
40
+ LARGE_INTEGER li;
41
+ li.LowPart = ft->dwLowDateTime ;
42
+ li.HighPart = ft->dwHighDateTime ;
43
+
44
+ return li.QuadPart ;
45
+ };
46
+
47
+ const int64_t system_time = (QuadPartFt (&kernel_time) + QuadPartFt (&user_time)) / dwNumberOfProcessors;
48
+ const int64_t time = QuadPartFt (&now);
49
+
50
+ static int64_t last_system_time = 0 , last_time = 0 ;
51
+
52
+ if (!last_system_time || !last_time)
53
+ {
54
+ last_system_time = system_time;
55
+ last_time = time;
56
+ return -1 ;
57
+ }
58
+
59
+ const int64_t time_delta = time - last_time;
60
+
61
+ if (!time_delta)
62
+ return -1 ;
63
+
64
+ const auto ret = short (((system_time - last_system_time) * 100 + time_delta / 2 ) / time_delta);
65
+
66
+ last_system_time = system_time;
67
+ last_time = time;
68
+
69
+ return ret;
70
+ }
71
+
72
+ LRESULT WINAPI WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
73
+ {
74
+ switch (msg)
75
+ {
76
+ case WM_DESTROY:
77
+ PostQuitMessage (0 );
78
+ return 0 ;
79
+ }
80
+
81
+ return DefWindowProc (hWnd, msg, wParam, lParam);
82
+ }
83
+
84
+ int CALLBACK WinMain (
85
+ _In_ HINSTANCE, _In_ HINSTANCE,
86
+ _In_ LPSTR, _In_ int )
87
+ {
88
+ #define WINDOW_NAME " DirectX9 Example"
89
+
90
+ WNDCLASSEX wc = { sizeof (WNDCLASSEX), CS_CLASSDC, WndProc, 0L , 0L , GetModuleHandle (NULL ), NULL , NULL , NULL , NULL , WINDOW_NAME, NULL };
91
+ RegisterClassEx (&wc);
92
+ HWND hwnd = CreateWindow (WINDOW_NAME, " DirectX 9 Test" , WS_OVERLAPPEDWINDOW, 100 , 100 , 1280 , 800 , NULL , NULL , wc.hInstance , NULL );
93
+
94
+ LPDIRECT3D9 pD3D;
95
+ if ((pD3D = Direct3DCreate9 (D3D_SDK_VERSION)) == NULL )
96
+ {
97
+ UnregisterClass (WINDOW_NAME, wc.hInstance );
98
+ return 0 ;
99
+ }
100
+
101
+ ZeroMemory (&g_d3dpp, sizeof (g_d3dpp));
102
+ g_d3dpp.Windowed = TRUE ;
103
+ g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
104
+ g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
105
+ g_d3dpp.EnableAutoDepthStencil = TRUE ;
106
+ g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
107
+ g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;// D3DPRESENT_INTERVAL_ONE;
108
+
109
+ if (pD3D->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0 )
110
+ {
111
+ pD3D->Release ();
112
+ UnregisterClass (WINDOW_NAME, wc.hInstance );
113
+ return 0 ;
114
+ }
115
+
116
+ D3DADAPTER_IDENTIFIER9 AdapterIdentifier;
117
+ pD3D->GetAdapterIdentifier (D3DADAPTER_DEFAULT, 0 , &AdapterIdentifier);
118
+
119
+ SYSTEM_INFO info;
120
+ GetSystemInfo (&info);
121
+
122
+
123
+ MSG msg;
124
+ ZeroMemory (&msg, sizeof (msg));
125
+ ShowWindow (hwnd, SW_SHOWDEFAULT);
126
+ UpdateWindow (hwnd);
127
+
128
+ cRender* pRender = new cRender (g_pd3dDevice);
129
+
130
+ ID3DXFont* font1 = nullptr ;
131
+ pRender->AddFont (&font1, " Consolas" , 48 , false );
132
+
133
+ pRender->SetFramerateUpdateRate (400U );
134
+
135
+ while (msg.message != WM_QUIT)
136
+ {
137
+ if (PeekMessage (&msg, NULL , 0U , 0U , PM_REMOVE))
138
+ {
139
+ TranslateMessage (&msg);
140
+ DispatchMessage (&msg);
141
+ continue ;
142
+ }
143
+
144
+ g_pd3dDevice->SetRenderState (D3DRS_ZENABLE, false );
145
+ g_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, false );
146
+ g_pd3dDevice->SetRenderState (D3DRS_SCISSORTESTENABLE, false );
147
+
148
+ g_pd3dDevice->Clear (0 , 0 , D3DCLEAR_TARGET, D3DCOLOR_XRGB (100 , 150 , 240 ), 1 .0f , 0 );
149
+
150
+ if (g_pd3dDevice->BeginScene () >= 0 )
151
+ {
152
+ pRender->BeginDraw ();
153
+
154
+ pRender->DrawGradientBox (20 , 20 , 200 , 50 , Color::Blue, 0xFFCCCC00 , true );
155
+
156
+ pRender->DrawFilledBox (240 , 20 , 200 , 50 , Color::SkyBlue);
157
+ pRender->DrawBox (460 , 20 , 200 , 50 , 4 , Color::Black);
158
+ pRender->DrawBox (680 , 20 , 200 , 50 , Color::Black);
159
+ pRender->DrawGradientBox (900 , 20 , 200 , 50 , Color::Blue, Color::Green, Color::Red, Color::Yellow);
160
+
161
+ pRender->DrawCircle (120 , 190 , 100 , 32 , Color::Red, true );
162
+ pRender->DrawCircle (340 , 190 , 100 , 32 , Color::Yellow);
163
+ pRender->DrawGradientCircle (560 , 190 , 100 , 32 , Color::Green, 0 );
164
+
165
+ pRender->DrawTriangle (120 , 310 , 20 , 480 , 220 , 480 , Color::Green);
166
+ pRender->DrawTriangle (340 , 310 , 240 , 480 , 440 , 480 , Color::SkyBlue, true );
167
+ pRender->DrawGradientTriangle (560 , 310 , 460 , 480 , 660 , 480 , Color::Yellow, Color::Green, Color::Red);
168
+
169
+ // text panel
170
+ {
171
+ static int cpu_usage = 0 ;
172
+ static uint16_t last_fps = 0 ;
173
+
174
+ const uint16_t current_fps = pRender->GetFramerate ();
175
+
176
+ if (last_fps != current_fps)
177
+ cpu_usage = GetUsageOfCPU ();
178
+
179
+ last_fps = current_fps;
180
+
181
+ pRender->DrawString (
182
+ 680 , 90 ,
183
+ Color::White,
184
+ font1, true , false ,
185
+ " CPU: %i%%\n FPS: %d\n CPU Cores: %i\n %s" ,
186
+ cpu_usage, current_fps,
187
+ info.dwNumberOfProcessors ,
188
+ AdapterIdentifier.Description );
189
+ }
190
+
191
+ pRender->EndDraw ();
192
+
193
+ g_pd3dDevice->EndScene ();
194
+ }
195
+
196
+ if (g_pd3dDevice->Present (NULL , NULL , NULL , NULL ) == D3DERR_DEVICELOST &&
197
+ g_pd3dDevice->TestCooperativeLevel () == D3DERR_DEVICENOTRESET)
198
+ {
199
+ pRender->OnLostDevice ();
200
+
201
+ if (g_pd3dDevice->Reset (&g_d3dpp) >= 0 )
202
+ pRender->OnResetDevice ();
203
+ }
204
+ }
205
+
206
+ if (g_pd3dDevice)
207
+ g_pd3dDevice->Release ();
208
+
209
+ if (pD3D)
210
+ pD3D->Release ();
211
+
212
+ DestroyWindow (hwnd);
213
+ UnregisterClass (WINDOW_NAME, wc.hInstance );
214
+
215
+ return 0 ;
216
+ }
0 commit comments