简单的Direct3D初始化步骤:
1 /* 2 2015.4 3 Direct3D初始化 4 */ 5 6 #include <d3d9.h> 7 8 #pragma comment(lib,"d3d9.lib") 9 #pragma comment(lib,"d3dx9.lib") 10 11 #define WINDOW_WIDTH 800 12 #define WINDOW_HEIGHT 600 13 #define WINDOW_TITLE L"Direct3D初始化" 14 #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } } //安全释放宏,便于后面COM接口指针的释放 15 16 LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; //Direct3D设备对象 17 18 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 19 20 //Direct3D的初始化 21 HRESULT Direct3D_Init(HWND hwnd); 22 23 //要绘制的物体的资源初始化 24 HRESULT Objects_Init(HWND hwnd); 25 26 //Direct3D渲染 27 VOID Direct3D_Render(HWND hwnd); 28 29 VOID Direct3D_CleanUp(); 30 31 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 32 { 33 WNDCLASSEX wndClass = { 0 }; 34 wndClass.cbSize = sizeof(WNDCLASSEX); 35 wndClass.style = CS_HREDRAW | CS_VREDRAW; 36 wndClass.lpfnWndProc = WndProc; 37 wndClass.cbClsExtra = 0; 38 wndClass.cbWndExtra = 0; 39 wndClass.hInstance = hInstance; 40 wndClass.hIcon = 0; 41 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 42 wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); 43 wndClass.lpszMenuName = NULL; 44 wndClass.lpszClassName = L"Game Develop"; 45 46 if (!RegisterClassEx(&wndClass)) 47 return -1; 48 49 HWND hwnd = CreateWindow(L"Game Develop", WINDOW_TITLE, 50 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, 51 WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); 52 53 //Direct3D资源的初始化,成功或者失败都用messagebox予以显示 54 if (S_OK == Direct3D_Init(hwnd)) 55 { 56 MessageBox(hwnd, L"Direct3D初始化完成~!", L"消息窗口", 0); 57 } 58 else 59 { 60 MessageBox(hwnd, L"Direct3D初始化失败~!", L"消息窗口", 0); 61 } 62 63 MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true); 64 ShowWindow(hwnd, nShowCmd); 65 UpdateWindow(hwnd); 66 67 MSG msg = { 0 }; 68 while (msg.message != WM_QUIT) 69 { 70 if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 71 { 72 TranslateMessage(&msg); //将虚拟键消息转换为字符消息 73 DispatchMessage(&msg); //该函数分发一个消息给窗口程序 74 } 75 else 76 { 77 //进行渲染 78 Direct3D_Render(hwnd); 79 } 80 } 81 //注销窗口类 82 UnregisterClass(L"Game Develop", wndClass.hInstance); 83 return 0; 84 } 85 86 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 87 { 88 switch (message) 89 { 90 case WM_PAINT: 91 //调用Direct3D渲染函数 92 Direct3D_Render(hwnd); 93 // 更新客户区的显示 94 ValidateRect(hwnd, NULL); 95 break; 96 97 case WM_KEYDOWN: 98 if (wParam == VK_ESCAPE) 99 DestroyWindow(hwnd); 100 break; 101 102 case WM_DESTROY: 103 Direct3D_CleanUp(); 104 PostQuitMessage(0); 105 break; 106 107 default: 108 return DefWindowProc(hwnd, message, wParam, lParam); 109 } 110 111 return 0; 112 } 113 114 HRESULT Direct3D_Init(HWND hwnd) 115 { 116 //Direct3D接口对象的创建 117 LPDIRECT3D9 pD3D = NULL; 118 //初始化Direct3D接口对象,并进行DirectX版本协商 119 if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) 120 return E_FAIL; 121 122 //获取硬件设备信息 123 D3DCAPS9 caps; 124 int vp = 0; 125 if (FAILED(pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps))) 126 { 127 return E_FAIL; 128 } 129 //支持硬件顶点运算,采用硬件顶点运算 130 if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) 131 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; 132 133 //不支持硬件顶点运算,采用软件顶点运算 134 else 135 vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 136 137 //填充D3DPRESENT_PARAMETERS结构体 138 D3DPRESENT_PARAMETERS d3dpp; 139 ZeroMemory(&d3dpp, sizeof(d3dpp)); 140 d3dpp.BackBufferWidth = WINDOW_WIDTH; 141 d3dpp.BackBufferHeight = WINDOW_HEIGHT; 142 d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 143 d3dpp.BackBufferCount = 1; 144 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; 145 d3dpp.MultiSampleQuality = 0; 146 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 147 d3dpp.hDeviceWindow = hwnd; 148 d3dpp.Windowed = true; 149 d3dpp.EnableAutoDepthStencil = true; 150 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 151 d3dpp.Flags = 0; 152 d3dpp.FullScreen_RefreshRateInHz = 0; 153 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 154 155 //创建Direct3D设备接口 156 /* 157 HRESULT CreateDevice( 158 UINT Adapter, 159 D3DDEVTYPE DeviceType, 160 HWND hFocusWindow, 161 DWORD BehaviorFlags, 162 D3DPRESENT_PARAMETERS *pPresentationParameters, 163 IDirect3DDevice9** ppReturnedDeviceInterface); 164 */ 165 if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 166 hwnd, vp, &d3dpp, &g_pd3dDevice))) 167 return E_FAIL; 168 169 //释放LPDIRECT3D9接口 170 SAFE_RELEASE(pD3D); 171 172 //渲染资源的初始化 173 if (!(S_OK == Objects_Init(hwnd))) 174 return E_FAIL; 175 176 return S_OK; 177 } 178 179 HRESULT Objects_Init(HWND hwnd) 180 { 181 return S_OK; 182 } 183 184 void Direct3D_Render(HWND hwnd) 185 { 186 187 } 188 189 void Direct3D_CleanUp() 190 { 191 192 }
时间: 2024-10-08 16:45:51