DirectX 9 编程 -- DirectX窗口
1 #include <windows.h>
2 #include <d3d9.h>
3 #include <d3dx9.h>
4
5 LRESULT CALLBACK MsgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
6 {
7 switch(message)
8 {
9 case WM_DESTROY:
10 PostQuitMessage(0);
11 return 0;
12 }
13 return DefWindowProc(hwnd,message,wParam,lParam);
14 }
15
16 int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
17 {
18 IDirect3D9* g_pD3D=NULL;
19 if(!(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION))) return E_FAIL;
20
21 WNDCLASSEX wc=
22 {
23 sizeof(WNDCLASSEX),
24 CS_CLASSDC,
25 MsgProc,
26 0,
27 0,
28 GetModuleHandle(NULL),
29 NULL,
30 NULL,
31 NULL,
32 NULL,
33 TEXT("WindowClass"),
34 NULL
35 };
36
37 RegisterClassEx(&wc);
38
39 HWND hWnd=CreateWindow(
40 TEXT("WindowClass"),
41 TEXT("TEST"),
42 WS_OVERLAPPEDWINDOW,
43 100,
44 100,
45 512,
46 512,
47 GetDesktopWindow(),
48 NULL,
49 wc.hInstance,
50 NULL);
51
52 D3DPRESENT_PARAMETERS d3dpp;
53 ZeroMemory(&d3dpp,sizeof(d3dpp));
54 d3dpp.Windowed=true;
55 d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
56 d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
57 d3dpp.EnableAutoDepthStencil=true;
58 d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
59
60 IDirect3DDevice9* g_pd3dDevice;
61 if(FAILED(
62 g_pD3D->CreateDevice(
63 D3DADAPTER_DEFAULT,
64 D3DDEVTYPE_HAL,
65 hWnd,
66 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
67 &d3dpp,
68 &g_pd3dDevice)
69 )
70 ) return E_FAIL;
71
72 ShowWindow(hWnd,nShowCmd);
73
74 MSG msg;
75
76 PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);
77
78 while(msg.message!=WM_QUIT)
79 {
80 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
81 {
82 TranslateMessage(&msg);
83 DispatchMessage(&msg);
84 }
85 else
86 {
87 //here render your game
88 g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0xff,0xff,0x00),1.0,0);
89 if(SUCCEEDED(g_pd3dDevice->BeginScene()))
90 {
91 g_pd3dDevice->EndScene();
92 }
93 g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
94 }
95 }
96
97 g_pd3dDevice->Release();
98
99 g_pD3D->Release();
100
101 return 0;
102 }
2 #include <d3d9.h>
3 #include <d3dx9.h>
4
5 LRESULT CALLBACK MsgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
6 {
7 switch(message)
8 {
9 case WM_DESTROY:
10 PostQuitMessage(0);
11 return 0;
12 }
13 return DefWindowProc(hwnd,message,wParam,lParam);
14 }
15
16 int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
17 {
18 IDirect3D9* g_pD3D=NULL;
19 if(!(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION))) return E_FAIL;
20
21 WNDCLASSEX wc=
22 {
23 sizeof(WNDCLASSEX),
24 CS_CLASSDC,
25 MsgProc,
26 0,
27 0,
28 GetModuleHandle(NULL),
29 NULL,
30 NULL,
31 NULL,
32 NULL,
33 TEXT("WindowClass"),
34 NULL
35 };
36
37 RegisterClassEx(&wc);
38
39 HWND hWnd=CreateWindow(
40 TEXT("WindowClass"),
41 TEXT("TEST"),
42 WS_OVERLAPPEDWINDOW,
43 100,
44 100,
45 512,
46 512,
47 GetDesktopWindow(),
48 NULL,
49 wc.hInstance,
50 NULL);
51
52 D3DPRESENT_PARAMETERS d3dpp;
53 ZeroMemory(&d3dpp,sizeof(d3dpp));
54 d3dpp.Windowed=true;
55 d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
56 d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
57 d3dpp.EnableAutoDepthStencil=true;
58 d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
59
60 IDirect3DDevice9* g_pd3dDevice;
61 if(FAILED(
62 g_pD3D->CreateDevice(
63 D3DADAPTER_DEFAULT,
64 D3DDEVTYPE_HAL,
65 hWnd,
66 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
67 &d3dpp,
68 &g_pd3dDevice)
69 )
70 ) return E_FAIL;
71
72 ShowWindow(hWnd,nShowCmd);
73
74 MSG msg;
75
76 PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);
77
78 while(msg.message!=WM_QUIT)
79 {
80 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
81 {
82 TranslateMessage(&msg);
83 DispatchMessage(&msg);
84 }
85 else
86 {
87 //here render your game
88 g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0xff,0xff,0x00),1.0,0);
89 if(SUCCEEDED(g_pd3dDevice->BeginScene()))
90 {
91 g_pd3dDevice->EndScene();
92 }
93 g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
94 }
95 }
96
97 g_pd3dDevice->Release();
98
99 g_pD3D->Release();
100
101 return 0;
102 }