1
#include
<
d3dx9.h
>
2
#include
<
d3d9.h
>
3
4
5
6
#pragma comment(lib,
"
d3dx9.lib
"
)
7
#pragma comment(lib,
"
d3d9.lib
"
)
8
9
10
#define
MAX_LOADSTRING 100
11
12
13
HINSTANCE hInst;
14
TCHAR szTitle[MAX_LOADSTRING]
=
TEXT(
"
D3D
"
);
15
TCHAR szWindowClass[MAX_LOADSTRING]
=
TEXT(
"
HelloD3D
"
);
16
17
18
19
ATOM MyRegisterClass(HINSTANCE hInstance);
20
BOOL InitInstance(HINSTANCE,
int
);
21
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
22
23
24
25
HWND g_hWnd
=
NULL;
26
27
IDirect3D9
*
pD3D
=
NULL;
28
IDirect3DDevice9
*
pDevice
=
NULL;
29
30
31
bool
InitD3D();
32
void
Release();
33
void
Render();
34
35
36
37
int
APIENTRY WinMain(HINSTANCE hInstance,
38
HINSTANCE hPrevInstance,
39
LPSTR lpCmdLine,
40
int
nCmdShow)
41
{
42
MSG msg;
43
44
MyRegisterClass(hInstance);
45
46
if
(
!
InitInstance (hInstance, nCmdShow))
47
{
48
return
FALSE;
49
}
50
51
if
(
!
InitD3D())
52
{
53
return
-
1
;
54
}
55
56
ZeroMemory(
&
msg,
sizeof
(msg));
57
58
while
(msg.message
!=
WM_QUIT)
59
{
60
if
(PeekMessage(
&
msg,NULL,
0
,
0
,PM_REMOVE))
61
{
62
TranslateMessage(
&
msg);
63
DispatchMessage(
&
msg);
64
65
}
66
else
67
{
68
Render();
69
}
70
}
71
72
return
(
int
)msg.wParam;
73
}
74
75
76
ATOM MyRegisterClass(HINSTANCE hInstance)
77
{
78
WNDCLASSEX wcex;
79
80
wcex.cbSize
=
sizeof
(WNDCLASSEX);
81
82
wcex.style
=
CS_HREDRAW
|
CS_VREDRAW;
83
wcex.lpfnWndProc
=
(WNDPROC)WndProc;
84
wcex.cbClsExtra
=
0
;
85
wcex.cbWndExtra
=
0
;
86
wcex.hInstance
=
hInstance;
87
wcex.hIcon
=
NULL;
88
wcex.hCursor
=
LoadCursor(NULL, IDC_ARROW);
89
wcex.hbrBackground
=
(HBRUSH)(COLOR_WINDOW
+
1
);
90
wcex.lpszMenuName
=
NULL;
91
wcex.lpszClassName
=
szWindowClass;
92
wcex.hIconSm
=
NULL;
93
return
RegisterClassEx(
&
wcex);
94
}
95
96
97
BOOL InitInstance(HINSTANCE hInstance,
int
nCmdShow)
98
{
99
HWND hWnd;
100
101
hInst
=
hInstance;
102
103
hWnd
=
CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
104
CW_USEDEFAULT,
0
, CW_USEDEFAULT,
0
, NULL, NULL, hInstance, NULL);
105
106
if
(
!
hWnd)
107
{
108
return
FALSE;
109
}
110
111
g_hWnd
=
hWnd;
112
113
ShowWindow(hWnd, nCmdShow);
114
UpdateWindow(hWnd);
115
116
return
TRUE;
117
}
118
119
120
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
121
{
122
switch
(message)
123
{
124
case
WM_DESTROY:
125
Release();
126
PostQuitMessage(
0
);
127
break
;
128
default
:
129
return
DefWindowProc(hWnd, message, wParam, lParam);
130
}
131
return
0
;
132
}
133
134
135
void
Release()
136
{
137
if
(pDevice
!=
NULL)
138
pDevice
->
Release();
139
if
(pD3D
!=
NULL)
140
pD3D
->
Release();
141
}
142
143
144
bool
InitD3D()
145
{
146
pD3D
=
Direct3DCreate9(D3D_SDK_VERSION);
147
148
if
(pD3D
==
NULL)
149
{
150
MessageBox(NULL,TEXT(
"
pD3D init failed
"
),TEXT(
"
error
"
),MB_OK);
151
return
false
;
152
}
153
154
D3DPRESENT_PARAMETERS d3dpp;
155
156
ZeroMemory(
&
d3dpp,
sizeof
(d3dpp) );
157
d3dpp.Windowed
=
TRUE;
158
d3dpp.SwapEffect
=
D3DSWAPEFFECT_COPY;
159
160
161
162
HRESULT hr;
163
hr
=
pD3D
->
CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
164
g_hWnd,
165
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
166
&
d3dpp,
167
&
pDevice);
168
169
if
(FAILED(hr))
170
{
171
MessageBox(NULL,TEXT(
"
create device failed
"
),TEXT(
"
error
"
),MB_OK);
172
return
false
;
173
}
174
175
return
true
;
176
}
177
178
179
180
void
Render()
181
{
182
if
(pDevice
==
NULL)
183
{
184
return
;
185
}
186
187
pDevice
->
Clear(
1
,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(
0
,
0
,
0
),
1.0f
,
0
);
188
189
pDevice
->
Present(NULL,NULL,NULL,NULL);
190
191
}
192
193
#include
<
d3dx9.h
>
2
#include
<
d3d9.h
>
3
4
5
6
#pragma comment(lib,
"
d3dx9.lib
"
)7
#pragma comment(lib,
"
d3d9.lib
"
)8
9
10
#define
MAX_LOADSTRING 100
11
12
13
HINSTANCE hInst; 14
TCHAR szTitle[MAX_LOADSTRING]
=
TEXT(
"
D3D
"
);15
TCHAR szWindowClass[MAX_LOADSTRING]
=
TEXT(
"
HelloD3D
"
); 16
17
18
19
ATOM MyRegisterClass(HINSTANCE hInstance);20
BOOL InitInstance(HINSTANCE,
int
);21
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);22
23
24
25
HWND g_hWnd
=
NULL;26
27
IDirect3D9
*
pD3D
=
NULL;28
IDirect3DDevice9
*
pDevice
=
NULL;29
30
31
bool
InitD3D();32
void
Release();33
void
Render();34
35
36
37
int
APIENTRY WinMain(HINSTANCE hInstance,38
HINSTANCE hPrevInstance,39
LPSTR lpCmdLine,40
int
nCmdShow)41
{42
MSG msg;43
44
MyRegisterClass(hInstance);45
46
if
(
!
InitInstance (hInstance, nCmdShow))47
{48
return
FALSE;49
}
50
51
if
(
!
InitD3D())52
{53
return
-
1
;54
}
55
56
ZeroMemory(
&
msg,
sizeof
(msg));57
58
while
(msg.message
!=
WM_QUIT)59
{60
if
(PeekMessage(
&
msg,NULL,
0
,
0
,PM_REMOVE))61
{62
TranslateMessage(
&
msg);63
DispatchMessage(
&
msg);64
65
}
66
else
67
{68
Render();69
}
70
}
71
72
return
(
int
)msg.wParam;73
}
74
75
76
ATOM MyRegisterClass(HINSTANCE hInstance)77
{78
WNDCLASSEX wcex;79
80
wcex.cbSize
=
sizeof
(WNDCLASSEX);81
82
wcex.style
=
CS_HREDRAW
|
CS_VREDRAW;83
wcex.lpfnWndProc
=
(WNDPROC)WndProc;84
wcex.cbClsExtra
=
0
;85
wcex.cbWndExtra
=
0
;86
wcex.hInstance
=
hInstance;87
wcex.hIcon
=
NULL;88
wcex.hCursor
=
LoadCursor(NULL, IDC_ARROW);89
wcex.hbrBackground
=
(HBRUSH)(COLOR_WINDOW
+
1
);90
wcex.lpszMenuName
=
NULL;91
wcex.lpszClassName
=
szWindowClass;92
wcex.hIconSm
=
NULL;93
return
RegisterClassEx(
&
wcex);94
}
95
96
97
BOOL InitInstance(HINSTANCE hInstance,
int
nCmdShow)98
{99
HWND hWnd;100
101
hInst
=
hInstance;102
103
hWnd
=
CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,104
CW_USEDEFAULT,
0
, CW_USEDEFAULT,
0
, NULL, NULL, hInstance, NULL);105
106
if
(
!
hWnd)107
{108
return
FALSE;109
}
110
111
g_hWnd
=
hWnd;112
113
ShowWindow(hWnd, nCmdShow);114
UpdateWindow(hWnd);115
116
return
TRUE;117
}
118
119
120
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)121
{122
switch
(message)123
{124
case
WM_DESTROY:125
Release();126
PostQuitMessage(
0
);127
break
;128
default
:129
return
DefWindowProc(hWnd, message, wParam, lParam);130
}
131
return
0
;132
}
133
134
135
void
Release()136
{137
if
(pDevice
!=
NULL)138
pDevice
->
Release();139
if
(pD3D
!=
NULL)140
pD3D
->
Release();141
}
142
143
144
bool
InitD3D()145
{146
pD3D
=
Direct3DCreate9(D3D_SDK_VERSION);147
148
if
(pD3D
==
NULL)149
{150
MessageBox(NULL,TEXT(
"
pD3D init failed
"
),TEXT(
"
error
"
),MB_OK);151
return
false
;152
}
153
154
D3DPRESENT_PARAMETERS d3dpp;155
156
ZeroMemory(
&
d3dpp,
sizeof
(d3dpp) );157
d3dpp.Windowed
=
TRUE;158
d3dpp.SwapEffect
=
D3DSWAPEFFECT_COPY;159
160
161
162
HRESULT hr;163
hr
=
pD3D
->
CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,164
g_hWnd,165
D3DCREATE_SOFTWARE_VERTEXPROCESSING,166
&
d3dpp,167
&
pDevice);168
169
if
(FAILED(hr))170
{171
MessageBox(NULL,TEXT(
"
create device failed
"
),TEXT(
"
error
"
),MB_OK);172
return
false
;173
}
174
175
return
true
;176
}
177
178
179
180
void
Render()181
{182
if
(pDevice
==
NULL)183
{184
return
;185
}
186
187
pDevice
->
Clear(
1
,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(
0
,
0
,
0
),
1.0f
,
0
);188
189
pDevice
->
Present(NULL,NULL,NULL,NULL);190
191
}
192
193
本文介绍了一个使用 Direct3D9 进行窗口创建、设备初始化及渲染循环的基础示例。从注册窗口类到创建 Direct3D 设备,再到渲染循环中的消息处理,详尽地展示了 Direct3D9 应用程序的基本结构。

被折叠的 条评论
为什么被折叠?



