-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectX.cpp
357 lines (296 loc) · 9.22 KB
/
DirectX.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
350
351
352
353
354
355
356
357
//-------------------------------------------------------------------
//
// Template DirectX 8 & 9:
//
// FILE:
// DirectX.cpp
//
// COMPILE:
//
// g++ DirectX.cpp -o DirectX -ld3dx8d -ld3d8 -lwinmm -DDX8
// or
// g++ DirectX.cpp -o DirectX -ld3dx9d -ld3d9 -lwinmm -DDX9
//
// COMPILE DIRECTX 8:
// compile.bat
//
// PROJECT:
// https://github.com/gokernel2017/directx_8_9
//
// BY: Francisco - [email protected]
//
//-------------------------------------------------------------------
//
#include <windows.h>
#include <stdio.h>
#include <time.h>
#ifdef DX8
#include "DX8/d3dx8.h"
typedef LPDIRECT3D8 LPDIRECT3D;
typedef LPDIRECT3DDEVICE8 LPDIRECT3DDEVICE;
#define Direct3DCreate Direct3DCreate8
#endif
#ifdef DX9
#include "DX9/d3dx9.h"
typedef LPDIRECT3D9 LPDIRECT3D;
typedef LPDIRECT3DDEVICE9 LPDIRECT3DDEVICE;
#define Direct3DCreate Direct3DCreate9
#endif
struct Vertex {
float x, y, z, rhw;
DWORD color;
};
static char ClassName[] = "Application_Class_Name";
LPDIRECT3D D3D = NULL;
LPDIRECT3DDEVICE device = NULL;
int color = D3DCOLOR_XRGB(255,130,30);
HFONT hFont;// = (HFONT)GetStockObject (DEFAULT_GUI_FONT);//SYSTEM_FONT);
LPD3DXFONT pFont = NULL;
HRESULT r = 0;
//-------------------------------------------------------------------
//
// Frame Rate API:
//
// Copyright (C) 2001-2012 Andreas Schiffler
//
//-------------------------------------------------------------------
#define FPS_MIN 30
#define FPS_MAX 200
#define UINT32 unsigned int
struct FRAME_RATE {
UINT32 framecount;
float rateticks;
UINT32 baseticks;
UINT32 lastticks;
UINT32 rate;
}FPS;
UINT32 FPS_GetTicks (void) {
UINT32 ticks = timeGetTime();
/*
* Since baseticks!=0 is used to track initialization
* we need to ensure that the tick count is always >0
* since SDL_GetTicks may not have incremented yet and
* return 0 depending on the timing of the calls.
*/
if (ticks == 0)
return 1;
else
return ticks;
}
void FPS_Init (void) {
FPS.framecount = 0;
FPS.rate = FPS_MIN;
FPS.rateticks = (1000.0f / (float) FPS_MIN);
FPS.baseticks = FPS_GetTicks();
FPS.lastticks = FPS.baseticks;
printf ("\nUsing Frame Rate API:\n");
printf ("Copyright (C) 2001-2012 Andreas Schiffler\n\n");
}
void FPS_SetFps (UINT32 rate) {
if ((rate >= FPS_MIN) && (rate <= FPS_MAX)) {
FPS.framecount = 0;
FPS.rate = rate;
FPS.rateticks = (1000.0f / (float) rate);
}
}
void FPS_Delay (void) {
UINT32 current_ticks;
UINT32 target_ticks;
FPS.framecount++;
// Get/calc ticks
//
current_ticks = FPS_GetTicks();
FPS.lastticks = current_ticks;
target_ticks = FPS.baseticks + (UINT32) ((float) FPS.framecount * FPS.rateticks);
if (current_ticks <= target_ticks) {
Sleep (target_ticks - current_ticks);
} else {
FPS.framecount = 0;
FPS.baseticks = FPS_GetTicks();
}
}
static LRESULT WINAPI WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
return 0;
}
LPDIRECT3DDEVICE gxCreateDevice (HWND hwnd, int FullScreen) {
LPDIRECT3DDEVICE dev = NULL;
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;
if ((D3D = Direct3DCreate (D3D_SDK_VERSION)) == NULL)
return NULL;
if (FAILED(D3D->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm)))
return NULL;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.Windowed = TRUE;
// Create the D3DDevice
if (FAILED(D3D->CreateDevice (
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING
&d3dpp,
&dev
)))
return NULL;
return dev;
}
HWND gxInit (int x, int y, int w, int h) {
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle (NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;//COLOR_WINDOW+1;
wc.lpszMenuName = NULL;
wc.lpszClassName = ClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
return NULL;
x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
y = ((GetSystemMetrics(SM_CYSCREEN) - h) / 2) - (GetSystemMetrics(SM_CYCAPTION)/2);
HWND win = CreateWindowEx (
0, // Extended possibilites for variation
ClassName, // Classname
#ifdef DX8
"GX API ( OpenGL/DirectX ) : DirectX 8",
#endif
#ifdef DX9
"GX API ( OpenGL/DirectX ) : DirectX 9",
#endif
#ifdef GL
"GX API ( OpenGL/DirectX ) : OpenGL",
#endif
// WS_OVERLAPPEDWINDOW, // default window
WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
x, y, w, h,
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
GetModuleHandle (NULL), // Program Instance handler
NULL // No Window Creation data
);
return win;
}
void gxRun (void(*idle)(void)) {
MSG msg;
if (idle) {
for (;;) {
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
if(msg.message == WM_QUIT)
break;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
idle ();
}
} else {
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
}// gxRun ()
void gxBeginScene (void) {
// device->Clear (0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
device->Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
device->BeginScene ();
}
void gxEndScene (void) {
device->EndScene ();
device->Present (NULL, NULL, NULL, NULL);
}
void gxDrawRect (int x, int y, int w, int h) {
Vertex p[] = {
{ x, y, 0, 1, color }, { x+w, y, 0, 1, color }, // --
{ x+w, y, 0, 1, color }, { x+w, y+h, 0, 1, color }, // |
{ x+w, y+h, 0, 1, color }, { x, y+h, 0, 1, color }, // --
{ x, y+h, 0, 1, color }, { x, y, 0, 1, color } // |
};
#define SVertexType D3DFVF_XYZRHW | D3DFVF_DIFFUSE
#ifdef DX8
device->SetVertexShader (SVertexType);
#endif
#ifdef DX9
device->SetFVF (SVertexType);
#endif
device->DrawPrimitiveUP (D3DPT_LINELIST, 4, p, sizeof(Vertex));
}
int x = 100, count;
char buf[100] = { 'F', 'P', 'S', ':', ' ', '6', '0', 0, 0 };
void idle (void) {
static int fps=0, t1=0, t2=0;
gxBeginScene();
gxDrawRect (x, 150, 320, 240);
x += 2;
if (x > 750) x = 10;
fps++;
t1 = time(NULL);
if (t1 != t2) {
t2 = t1;
sprintf (buf, "FPS(Rate 60): %d | %d", fps, count);
count++;
fps=0;
}
if (r==S_OK) {
// Rectangle where the text will be located
RECT TextRect = {10,10,0,0};
// Inform font it is about to be used
pFont->Begin();
// Calculate the rectangle the text will occupy
// pFont->DrawText(buf, -1, &TextRect, DT_CALCRECT, 0 );
// Output the text, left aligned
//pFont->DrawText("Hello World 2", -1, &TextRect, DT_LEFT, color );
pFont->DrawText (buf, -1, &TextRect, DT_LEFT, color );
// Finish up drawing
pFont->End();
}
gxEndScene();
FPS_Delay ();
}
void CreateText (void) {
hFont = (HFONT)GetStockObject (SYSTEM_FIXED_FONT);
// Create the D3DX Font
r = D3DXCreateFont(device, hFont, &pFont);
}
int main (int argc, char **argv) {
HWND win = NULL;
if ((win = gxInit (100,100,800,600))) {
if ((device = gxCreateDevice(win,1))) {
#ifdef DX8
printf ("DirextX 8 D3D_SDK_VERSION: %d\n", D3D_SDK_VERSION);
#endif
#ifdef DX9
printf ("DirextX 9 D3D_SDK_VERSION: %d\n", D3D_SDK_VERSION);
#endif
ShowWindow (win,1);
timeBeginPeriod (1);
FPS_Init ();
FPS_SetFps (60);
CreateText ();
gxRun (idle);
// Free objects:
if (pFont)
pFont->Release();
device->Release();
D3D->Release();
}
else {
printf ("DirectX Failed\n");
}
}
printf ("Exiting With Sucess:\n");
}