【原创】《DirectX 9 3D 图形程序设计》 Tutorial 01 容易犯错的地方
手写Tutorial 01例子是创建Windows窗口,并创建D3D设备。
97行,PeekMessage(&msg , 0 , 0 , 0 , PM_REMOVE)第二个参数如果改成PeekMessage(&msg , hWnd , 0 , 0 , PM_REMOVE)就会出现无法退出程序的问题!请注意!
44行,g_pd3dDevice->Present(0 , 0 , 0 , 0);这句话千万不要漏掉哦!否则就会显示花瓶!
97行,PeekMessage(&msg , 0 , 0 , 0 , PM_REMOVE)第二个参数如果改成PeekMessage(&msg , hWnd , 0 , 0 , PM_REMOVE)就会出现无法退出程序的问题!请注意!
44行,g_pd3dDevice->Present(0 , 0 , 0 , 0);这句话千万不要漏掉哦!否则就会显示花瓶!
1
#include
<
d3dx9.h
>
2
3 #define SAFE_RELEASE(o) {if(o){o->Release();o=0;}}
4
5 LPDIRECT3D9 g_pD3D = 0 ;
6 LPDIRECT3DDEVICE9 g_pd3dDevice = 0 ;
7
8 // 创建D3D设备
9 HRESULT InitD3D(HWND hWnd)
10 {
11 // 创建D3D SDK
12 if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
13 return E_FAIL;
14
15
16 D3DPRESENT_PARAMETERS d3dpp;
17 memset(&d3dpp , 0 , sizeof(d3dpp));
18 d3dpp.Windowed = true;
19 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
20 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
21
22 RECT rect;
23 GetClientRect(hWnd , &rect);
24 d3dpp.BackBufferWidth = rect.right - rect.left;
25 d3dpp.BackBufferHeight = rect.bottom - rect.top;
26
27 if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT
28 , D3DDEVTYPE_HAL
29 , hWnd
30 , D3DCREATE_HARDWARE_VERTEXPROCESSING
31 , &d3dpp , &g_pd3dDevice
32 )))
33 return E_FAIL;
34
35 return S_OK;
36}
37
38 // 渲染
39 void Render()
40 {
41 // 清空成蓝色
42 g_pd3dDevice->Clear(0 , 0 , D3DCLEAR_TARGET , D3DCOLOR_XRGB(0 , 0 , 255) , 1.0f , 0);
43 // 显示
44 g_pd3dDevice->Present(0 , 0 , 0 , 0);
45}
46
47 // 清除占有的内存
48 void CleanUp()
49 {
50 SAFE_RELEASE(g_pD3D);
51}
52
53 // 消息处理
54 LRESULT WINAPI MsgProc(HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam)
55 {
56 switch(message)
57 {
58 case WM_DESTROY:
59 CleanUp();
60 PostQuitMessage(0);
61 return 0;
62 break;
63 default:
64 break;
65 }
66 return DefWindowProc(hWnd, message , wParam , lParam);
67}
68
69 // Windows入口
70 int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd)
71 {
72 WNDCLASS wndClass;
73 memset(&wndClass , 0 , sizeof(wndClass));
74 wndClass.hInstance = hInstance;
75 wndClass.lpszClassName = "Tutorial01";
76 wndClass.style = 0;
77 wndClass.lpfnWndProc = MsgProc;
78 // 注册窗口类
79 RegisterClass(&wndClass);
80
81 // 创建窗口
82 HWND hWnd = CreateWindow("Tutorial01" , "Tutorial01 : CreateDevice" , WS_OVERLAPPEDWINDOW
83 , 0 , 0 , 512 , 512 , GetDesktopWindow()
84 , 0 , wndClass.hInstance , 0);
85
86 // 显示窗口
87 ShowWindow(hWnd , SW_SHOWDEFAULT);
88 UpdateWindow(hWnd);
89
90 if(SUCCEEDED(InitD3D(hWnd)))
91 {
92 // 消息循环
93 MSG msg;
94 memset(&msg , 0 , sizeof(msg));
95 while(msg.message != WM_QUIT)
96 {
97 if(PeekMessage(&msg , 0 , 0 , 0 , PM_REMOVE))
98 {
99 DispatchMessage(&msg);
100 TranslateMessage(&msg);
101 }
102 else
103 {
104 Render();
105 }
106 }
107 }
108
109 CleanUp();
110 UnregisterClass("Tutorial01" , wndClass.hInstance);
111 return 0;
112}
113
114
115
2
3 #define SAFE_RELEASE(o) {if(o){o->Release();o=0;}}
4
5 LPDIRECT3D9 g_pD3D = 0 ;
6 LPDIRECT3DDEVICE9 g_pd3dDevice = 0 ;
7
8 // 创建D3D设备
9 HRESULT InitD3D(HWND hWnd)
10 {
11 // 创建D3D SDK
12 if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
13 return E_FAIL;
14
15
16 D3DPRESENT_PARAMETERS d3dpp;
17 memset(&d3dpp , 0 , sizeof(d3dpp));
18 d3dpp.Windowed = true;
19 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
20 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
21
22 RECT rect;
23 GetClientRect(hWnd , &rect);
24 d3dpp.BackBufferWidth = rect.right - rect.left;
25 d3dpp.BackBufferHeight = rect.bottom - rect.top;
26
27 if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT
28 , D3DDEVTYPE_HAL
29 , hWnd
30 , D3DCREATE_HARDWARE_VERTEXPROCESSING
31 , &d3dpp , &g_pd3dDevice
32 )))
33 return E_FAIL;
34
35 return S_OK;
36}
37
38 // 渲染
39 void Render()
40 {
41 // 清空成蓝色
42 g_pd3dDevice->Clear(0 , 0 , D3DCLEAR_TARGET , D3DCOLOR_XRGB(0 , 0 , 255) , 1.0f , 0);
43 // 显示
44 g_pd3dDevice->Present(0 , 0 , 0 , 0);
45}
46
47 // 清除占有的内存
48 void CleanUp()
49 {
50 SAFE_RELEASE(g_pD3D);
51}
52
53 // 消息处理
54 LRESULT WINAPI MsgProc(HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam)
55 {
56 switch(message)
57 {
58 case WM_DESTROY:
59 CleanUp();
60 PostQuitMessage(0);
61 return 0;
62 break;
63 default:
64 break;
65 }
66 return DefWindowProc(hWnd, message , wParam , lParam);
67}
68
69 // Windows入口
70 int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd)
71 {
72 WNDCLASS wndClass;
73 memset(&wndClass , 0 , sizeof(wndClass));
74 wndClass.hInstance = hInstance;
75 wndClass.lpszClassName = "Tutorial01";
76 wndClass.style = 0;
77 wndClass.lpfnWndProc = MsgProc;
78 // 注册窗口类
79 RegisterClass(&wndClass);
80
81 // 创建窗口
82 HWND hWnd = CreateWindow("Tutorial01" , "Tutorial01 : CreateDevice" , WS_OVERLAPPEDWINDOW
83 , 0 , 0 , 512 , 512 , GetDesktopWindow()
84 , 0 , wndClass.hInstance , 0);
85
86 // 显示窗口
87 ShowWindow(hWnd , SW_SHOWDEFAULT);
88 UpdateWindow(hWnd);
89
90 if(SUCCEEDED(InitD3D(hWnd)))
91 {
92 // 消息循环
93 MSG msg;
94 memset(&msg , 0 , sizeof(msg));
95 while(msg.message != WM_QUIT)
96 {
97 if(PeekMessage(&msg , 0 , 0 , 0 , PM_REMOVE))
98 {
99 DispatchMessage(&msg);
100 TranslateMessage(&msg);
101 }
102 else
103 {
104 Render();
105 }
106 }
107 }
108
109 CleanUp();
110 UnregisterClass("Tutorial01" , wndClass.hInstance);
111 return 0;
112}
113
114
115