本节的代码是进行D3D环境的初始化。
D3DPRESENT_PARAMETERS结构的设置要具体一点,免得表面不是纯色的。以上代码在win7 x64 下利用vs2008编译通过。注意添加对d3d9.lib的依赖。
/* *D3D窗口初始化 *whufly *2011年6月15日 * * */ #include #include IDirect3D9 *g_pD3D = NULL; IDirect3DDevice9 *g_pDevice = NULL; //初始化D3D环境 HRESULT InitD3D(HWND hwnd) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (g_pD3D == NULL) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); //d3dpp.Windowed = true; //d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.BackBufferWidth = 500; d3dpp.BackBufferHeight = 400; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //像素格式 d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hwnd; d3dpp.Windowed = true ; // fullscreen d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //创建D3D设备 if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pDevice))) { return E_FAIL; } return S_OK; } //资源清理 void Cleanup() { if (g_pDevice != NULL) g_pDevice->Release(); if (g_pD3D) g_pD3D->Release(); } //渲染 void Render() { if (g_pDevice != NULL) { g_pDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0,255,0,0),1.0f,0); if (SUCCEEDED(g_pDevice->BeginScene())) { g_pDevice->EndScene(); } g_pDevice->Present(NULL,NULL,NULL,NULL); } } //消息处理函数 LRESULT WINAPI WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; case WM_PAINT: //*、、*/Render(); break; default: break; } return DefWindowProc(hwnd,msg,wParam,lParam); } //主函数 INT WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ) { WNDCLASS wndclass; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor = 0; wndclass.hIcon = 0; wndclass.lpfnWndProc = WinProc; wndclass.hInstance = hInstance; wndclass.lpszClassName = "D3DClass"; wndclass.lpszMenuName = 0; wndclass.style. = CS_HREDRAW | CS_VREDRAW; if (!RegisterClass(&wndclass)) return FALSE; HWND hwnd = CreateWindow("D3DClass","D3DWindow",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hInstance,NULL); if (hwnd == NULL) return FALSE; if (SUCCEEDED(InitD3D(hwnd))) { ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); MSG msg; ZeroMemory(&msg,sizeof(msg)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { Render(); } } } UnregisterClass("D3DClass",hInstance); Cleanup(); return 0; } |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24818796/viewspace-698043/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24818796/viewspace-698043/