近来开始学习Direct3D编程,希望像当时学习DirectShow一样,在这里记下我学习的过程。下面是我写的一个基本的Direct3D的框架程序,适合入门。
// // D3DBasicStruct.cpp // // Written by Leezhm, 4th July, 2008 // Contact : Leezhm@126.com #pragma once #include <windows.h> #include <tchar.h> #include <strsafe.h> #include <d3d9.h> #include <d3dx9.h> // global variable IDirect3D9 * g_pD3D = NULL;; IDirect3DDevice9 * g_pD3Device = NULL; // // Show message // void ShowMsg(TCHAR * szFormat, ...) { TCHAR szBuffer[1024] ={0}; const unsigned int numChar = sizeof(szBuffer) / sizeof(szBuffer[0]); va_list ptrArg; va_start(ptrArg, szFormat); // Formatted string StringCchPrintf(szBuffer, numChar - 1, szFormat, ptrArg); va_end(ptrArg); MessageBox(NULL, szBuffer, _T("D3D Information"), MB_OK | MB_ICONERROR); } // // Initialize Direct3D // HRESULT InitDirect3D(HWND hwnd) { HRESULT hr = NOERROR; try { // Create IDirect3D9 g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (NULL == g_pD3D) { ShowMsg(TEXT("Could not create IDirect3D9 interface! hr = 0x%d"), hr); return hr; } D3DDISPLAYMODE d3dMode; hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dMode); if (FAILED(hr)) { ShowMsg(TEXT("The GetAdapterDisplayMode() function failed! hr = 0x%d"), hr); return hr; } D3DPRESENT_PARAMETERS d3dpParm; ZeroMemory(&d3dpParm, sizeof(d3dpParm)); d3dpParm.Windowed = TRUE; d3dpParm.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpParm.BackBufferFormat = d3dMode.Format; hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpParm, &g_pD3Device); if (FAILED(hr)) { ShowMsg(TEXT("Could not create the D3D Device! hr = 0x%d"), hr); return hr; } } catch(...) { ShowMsg(TEXT("Initialize Direct3D failed!")); hr = E_FAIL; } return hr; } // // Release // void CleanUp() { // Release D3D interface if (g_pD3Device) { g_pD3Device->Release(); g_pD3Device = NULL; } if (g_pD3D) { g_pD3D->Release(); g_pD3D = NULL; } } // // Render geometry // HRESULT Render() { HRESULT hr = NOERROR; try { // clear background buffer if (g_pD3Device) { hr = g_pD3Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); if (FAILED(hr)) { ShowMsg(TEXT("Could not clear the background buffer! hr = 0x%d"), hr); return hr; } if (SUCCEEDED(hr = g_pD3Device->BeginScene())) { // render // // ... // g_pD3Device->EndScene(); } else { ShowMsg(TEXT("BeginScene failed! hr = 0x%d"), hr); hr = E_FAIL; } g_pD3Device->Present(NULL, NULL, NULL, NULL); } } catch (...) { ShowMsg(TEXT("Application encountered an unexpected error /r/n") TEXT("when trying to render the geometry./r/n")); hr = E_UNEXPECTED; } return hr; } // // processes application messages // LRESULT WINAPI WndMsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; BeginPaint(hwnd, &ps); Render(); EndPaint(hwnd, &ps); break; } case WM_DESTROY: { CleanUp(); PostQuitMessage(0); break; } default: return DefWindowProc(hwnd, msg, wParam, lParam); } return FALSE; } // // WinMain // int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { // Initialize COM CoInitialize(NULL); WNDCLASS wndcls; wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(NULL, IDI_WINLOGO); wndcls.hInstance = hInstance; wndcls.lpfnWndProc = WndMsgProc; wndcls.lpszClassName = _T("D3DBasicStruct"); wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); HWND hwnd; hwnd = CreateWindow(_T("D3DBasicStruct"), _T("Direct 3D Window"), WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, wndcls.hInstance, NULL); if (SUCCEEDED(InitDirect3D(hwnd))) { ShowWindow(hwnd, SW_SHOWDEFAULT); UpdateWindow(hwnd); MSG uMsg; ZeroMemory(&uMsg, sizeof(uMsg)); while (WM_QUIT != uMsg.message) { if (PeekMessage(&uMsg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&uMsg); DispatchMessage(&uMsg); } else { Render(); } } } UnregisterClass(_T("D3DBasicStruct"), wndcls.hInstance); CoUninitialize(); return 0; }