DX编程(1) — 创建D3D对象、D3D设备,渲染

#include <d3d9.h> #pragma comment(lib,"d3d9.lib") #pragma warning(disable : 4996) #include <strsafe.h> #pragma warning(default : 4996) LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; HRESULT InitD3D(HWND hWnd) { //创建D3D对象 if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return E_FAIL; //创建D3D设备 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice))) return E_FAIL; return S_OK; } void Cleanup() { if(NULL != g_pd3dDevice) g_pd3dDevice->Release(); if(NULL != g_pD3D) g_pD3D->Release(); } //渲染 void Render() { if(NULL == g_pd3dDevice) return; g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,0,0),1.0,0); if(SUCCEEDED(g_pd3dDevice->BeginScene())) { g_pd3dDevice->EndScene(); } g_pd3dDevice->Present(NULL,NULL,NULL,NULL); } LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_PAINT: Render(); ValidateRect(hWnd,NULL); break; case WM_DESTROY: Cleanup(); PostQuitMessage(0); break; default: break; } return DefWindowProc(hWnd,uMsg,wParam,lParam); } int WINAPI wWinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPWSTR lpCmdLine, __in int nShowCmd ) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hbrBackground = NULL; wcex.hCursor = NULL; wcex.hIcon = NULL; wcex.hIconSm = NULL; wcex.hInstance = GetModuleHandle(NULL); wcex.lpfnWndProc = WndProc; wcex.lpszClassName = L"First D3D Window"; wcex.lpszMenuName = NULL; wcex.style = CS_CLASSDC; RegisterClassEx(&wcex); HWND hWnd = CreateWindow(L"First D3D Window",L"第一个D3D窗口",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL, wcex.hInstance,NULL); if(SUCCEEDED(InitD3D(hWnd))) { ShowWindow(hWnd,SW_SHOWNORMAL); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } UnregisterClass(L"First D3D Window",wcex.hInstance); return 0; }

你可能感兴趣的:(编程,null,Parameters,callback,include,winapi)