dx9init代码
d3dUtility.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ////////////////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // File: d3dUtility.h 4 // 5 // Author: Frank Luna (C) All Rights Reserved 6 // 7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 8 // 9 // Desc: Provides utility functions for simplifying common tasks. 10 // 11 ////////////////////////////////////////////////////////////////////////////////////////////////// 12 13 #ifndef __d3dUtilityH__ 14 #define __d3dUtilityH__ 15 16 #include <d3dx9.h> 17 #include <string> 18 19 namespace d3d 20 { 21 bool InitD3D( 22 HINSTANCE hInstance, // [in] Application instance. 23 int width, int height, // [in] Backbuffer dimensions. 24 bool windowed, // [in] Windowed (true)or full screen (false). 25 D3DDEVTYPE deviceType, // [in] HAL or REF 26 IDirect3DDevice9** device);// [out]The created device. 27 28 int EnterMsgLoop( 29 bool (*ptr_display)(float timeDelta)); 30 31 LRESULT CALLBACK WndProc( 32 HWND hwnd, 33 UINT msg, 34 WPARAM wParam, 35 LPARAM lParam); 36 37 template<class T> void Release(T t) 38 { 39 if( t ) 40 { 41 t->Release(); 42 t = 0; 43 } 44 } 45 46 template<class T> void Delete(T t) 47 { 48 if( t ) 49 { 50 delete t; 51 t = 0; 52 } 53 } 54 } 55 56 #endif // __d3dUtilityH__
d3dUtility.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ////////////////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // File: d3dUtility.cpp 4 // 5 // Author: Frank Luna (C) All Rights Reserved 6 // 7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 8 // 9 // Desc: Provides utility functions for simplifying common tasks. 10 // 11 ////////////////////////////////////////////////////////////////////////////////////////////////// 12 13 #include "d3dUtility.h" 14 15 bool d3d::InitD3D( 16 HINSTANCE hInstance, 17 int width, int height, 18 bool windowed, 19 D3DDEVTYPE deviceType, 20 IDirect3DDevice9** device) 21 { 22 // 23 // Create the main application window. 24 // 25 26 WNDCLASS wc; 27 28 wc.style = CS_HREDRAW | CS_VREDRAW; 29 wc.lpfnWndProc = (WNDPROC)d3d::WndProc; 30 wc.cbClsExtra = 0; 31 wc.cbWndExtra = 0; 32 wc.hInstance = hInstance; 33 wc.hIcon = LoadIcon(0, IDI_APPLICATION); 34 wc.hCursor = LoadCursor(0, IDC_ARROW); 35 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 36 wc.lpszMenuName = 0; 37 wc.lpszClassName = L"Direct3D9App"; 38 39 if( !RegisterClass(&wc) ) 40 { 41 ::MessageBox(0, L"RegisterClass() - FAILED", 0, 0); 42 return false; 43 } 44 45 HWND hwnd = 0; 46 hwnd = ::CreateWindow(L"Direct3D9App", L"Direct3D9App", 47 WS_EX_TOPMOST, 48 0, 0, width, height, 49 0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); 50 51 if( !hwnd ) 52 { 53 ::MessageBox(0, L"CreateWindow() - FAILED", 0, 0); 54 return false; 55 } 56 57 ::MoveWindow(hwnd,2250,80,640, 480,true); 58 ::ShowWindow(hwnd, SW_SHOW); 59 ::UpdateWindow(hwnd); 60 61 // 62 // Init D3D: 63 // 64 65 HRESULT hr = 0; 66 67 // Step 1: Create the IDirect3D9 object. 68 69 IDirect3D9* d3d9 = 0; 70 d3d9 = Direct3DCreate9(D3D_SDK_VERSION); 71 72 if( !d3d9 ) 73 { 74 ::MessageBox(0, L"Direct3DCreate9() - FAILED", 0, 0); 75 return false; 76 } 77 78 // Step 2: Check for hardware vp. 79 80 D3DCAPS9 caps; 81 d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); 82 83 int vp = 0; 84 if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) 85 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; 86 else 87 vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 88 89 // Step 3: Fill out the D3DPRESENT_PARAMETERS structure. 90 91 D3DPRESENT_PARAMETERS d3dpp; 92 d3dpp.BackBufferWidth = width; 93 d3dpp.BackBufferHeight = height; 94 d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 95 d3dpp.BackBufferCount = 1; 96 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; 97 d3dpp.MultiSampleQuality = 0; 98 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 99 d3dpp.hDeviceWindow = hwnd; 100 d3dpp.Windowed = windowed; 101 d3dpp.EnableAutoDepthStencil = true; 102 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 103 d3dpp.Flags = 0; 104 d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; 105 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 106 107 // Step 4: Create the device. 108 109 hr = d3d9->CreateDevice( 110 D3DADAPTER_DEFAULT, // primary adapter 111 deviceType, // device type 112 hwnd, // window associated with device 113 vp, // vertex processing 114 &d3dpp, // present parameters 115 device); // return created device 116 117 if( FAILED(hr) ) 118 { 119 // try again using a 16-bit depth buffer 120 d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 121 122 hr = d3d9->CreateDevice( 123 D3DADAPTER_DEFAULT, 124 deviceType, 125 hwnd, 126 vp, 127 &d3dpp, 128 device); 129 130 if( FAILED(hr) ) 131 { 132 d3d9->Release(); // done with d3d9 object 133 ::MessageBox(0, L"CreateDevice() - FAILED", 0, 0); 134 return false; 135 } 136 } 137 138 d3d9->Release(); // done with d3d9 object 139 140 return true; 141 } 142 143 int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) ) 144 { 145 MSG msg; 146 ::ZeroMemory(&msg, sizeof(MSG)); 147 148 static float lastTime = (float)timeGetTime(); 149 150 while(msg.message != WM_QUIT) 151 { 152 if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 153 { 154 ::TranslateMessage(&msg); 155 ::DispatchMessage(&msg); 156 } 157 else 158 { 159 float currTime = (float)timeGetTime(); 160 float timeDelta = (currTime - lastTime)*0.001f; 161 162 ptr_display(timeDelta); 163 164 lastTime = currTime; 165 } 166 } 167 return msg.wParam; 168 }
d3dInit.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ////////////////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // File: d3dinit.cpp 4 // 5 // Author: Frank Luna (C) All Rights Reserved 6 // 7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 8 // 9 // Desc: Demonstrates how to initialize Direct3D, how to use the book's framework 10 // functions, and how to clear the screen to black. Note that the Direct3D 11 // initialization code is in the d3dUtility.h/.cpp files. 12 // 13 ////////////////////////////////////////////////////////////////////////////////////////////////// 14 15 #include "d3dUtility.h" 16 #pragma comment(lib, "d3d9.lib") 17 #pragma comment(lib, "d3dx9.lib") 18 #pragma comment(lib, "winmm.lib") 19 // 20 // Globals 21 // 22 23 IDirect3DDevice9* Device = 0; 24 25 // 26 // Framework Functions 27 // 28 29 bool Setup() 30 { 31 // Nothing to setup in this sample. 32 33 return true; 34 } 35 36 void Cleanup() 37 { 38 // Nothing to cleanup in this sample. 39 } 40 41 bool Display(float timeDelta) 42 { 43 if( Device ) // Only use Device methods if we have a valid device. 44 { 45 // Instruct the device to set each pixel on the back buffer black - 46 // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on 47 // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f. 48 Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0); 49 50 // Swap the back and front buffers. 51 Device->Present(0, 0, 0, 0); 52 } 53 return true; 54 } 55 56 // 57 // WndProc 58 // 59 LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 60 { 61 switch( msg ) 62 { 63 case WM_DESTROY: 64 ::PostQuitMessage(0); 65 break; 66 67 case WM_KEYDOWN: 68 if( wParam == VK_ESCAPE ) 69 ::DestroyWindow(hwnd); 70 break; 71 } 72 return ::DefWindowProc(hwnd, msg, wParam, lParam); 73 } 74 75 // 76 // WinMain 77 // 78 int WINAPI WinMain(HINSTANCE hinstance, 79 HINSTANCE prevInstance, 80 PSTR cmdLine, 81 int showCmd) 82 { 83 if(!d3d::InitD3D(hinstance, 84 640, 480, true, D3DDEVTYPE_HAL, &Device)) 85 { 86 ::MessageBox(0, L"InitD3D() - FAILED", 0, 0); 87 return 0; 88 } 89 90 if(!Setup()) 91 { 92 ::MessageBox(0, L"Setup() - FAILED", 0, 0); 93 return 0; 94 } 95 96 d3d::EnterMsgLoop( Display ); 97 98 Cleanup(); 99 100 Device->Release(); 101 102 return 0; 103 }