-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cpp
More file actions
243 lines (201 loc) * 6.49 KB
/
basic.cpp
File metadata and controls
243 lines (201 loc) * 6.49 KB
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
//-------------------------------------------------------------------
//
// Template DirectX 8 & 9:
//
// FILE:
// basic.cpp
//
// COMPILE:
//
// g++ basic.cpp -o basic -ld3d8 -lwinmm -DDX8
// or
// g++ basic.cpp -o basic -ld3d9 -lwinmm -DDX9
//
// COMPILE DIRECTX 8:
// compile_basic.bat
//
// PROJECT:
// https://github.com/gokernel2017/directx_8_9
//
//
// BY: Francisco - gokernel@hotmail.com
//
//-------------------------------------------------------------------
//
//#include
#include <stdio.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
static char ClassName[] = "Application_Class_Name";
LPDIRECT3D D3D = NULL;
LPDIRECT3DDEVICE device = NULL;
int color = D3DCOLOR_XRGB(255,130,30);
HFONT hFont;
LPD3DXFONT pFont = NULL;
HRESULT r = 0;
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
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) {
MSG msg;
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) {
struct Vertex {
float x, y, z, rhw;
DWORD color;
} 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));
}
void DrawText (void) {
hFont = (HFONT)GetStockObject (SYSTEM_FIXED_FONT);
// Create the D3DX Font
r = D3DXCreateFont(device, hFont, &pFont);
if (r==S_OK) {
// Rectangle where the text will be located
RECT TextRect={100,100,0,0};
// Inform font it is about to be used
pFont->Begin();
// Calculate the rectangle the text will occupy
pFont->DrawText("Hello World", -1, &TextRect, DT_CALCRECT, 0 );
// Output the text, left aligned
pFont->DrawText("Hello World 2", -1, &TextRect, DT_LEFT, color);
// Finish up drawing
pFont->End();
// Release the font
pFont->Release();
pFont = NULL;
}
printf ("Function: DrawText();\n");
}
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);
gxBeginScene();
gxDrawRect (100, 150, 320, 240);
DrawText ();
gxEndScene();
gxRun ();
// Free objects:
if (pFont)
pFont->Release();
device->Release();
D3D->Release();
}
else {
printf ("DirectX Failed\n");
}
}
printf ("Exiting With Sucess:\n");
}