第一个D3D窗口

//====================================================== //最简单的Direct3D程序 //====================================================== #include <d3d9.h> //------------------------------------------------------ //全局变量 //------------------------------------------------------ LPDIRECT3D9 g_pD3D = NULL;//Direct3D对象,g_pD3D是Direct3D接口指针 LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;//Direct3D设备对象 //------------------------------------------------------ //原型声明 //------------------------------------------------------ HRESULT InitD3D(HWND hWnd); VOID Cleanup(); VOID Render(); LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam); //------------------------------------------------------ //Desc:程序入口 //------------------------------------------------------ INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,INT) { WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,L"ClassName",NULL}; RegisterClassEx(&wc); //创建窗口 HWND hWnd = CreateWindow(L"ClassName",L"最简单的Direct3D程序",WS_OVERLAPPEDWINDOW,0,0,1024,768,NULL,NULL,wc.hInstance,NULL); // 初始化Direct3D if(SUCCEEDED(InitD3D(hWnd))) { //显示主窗口 ShowWindow(hWnd,SW_SHOWDEFAULT); UpdateWindow(hWnd); //进入消息循环 MSG msg; ZeroMemory(&msg,sizeof(msg)); while(msg.message!=WM_QUIT) { if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))//PeekMessage和GetMessage的区别:PeekMessage过程中用来处理Render而GetMessage过门不入,不会抢系统控制权 { TranslateMessage(&msg);//消息转换 DispatchMessage(&msg);//消息传递给窗口过程函数 } else { Render();//渲染图形,渲染函数一般都是在程序空闲时调用的 } } } UnregisterClass(L"ClassName",wc.hInstance);//参数说明:姚注销雷名字字符串指针;应用程序句柄 return 0; } //------------------------------------------------------ //Desc:初始化Direct3D //------------------------------------------------------ HRESULT InitD3D(HWND hWnd) { //创建Direct3D对象,该对象用来创建Direc3D设备对象 if( NULL == ( g_pD3D = Direct3DCreate9(D3D_SDK_VERSION) ) ) { return E_FAIL; } //设置D3DPRESENT_PARAMETERS结构,准备创建Direct3D对象 D3DPRESENT_PARAMETERS d3dpp;//创建Direct3D设备对象参数d3dpp ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = TRUE;//窗口还是全屏幕 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//后台缓存复制到前台后,清除后台缓存 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//后台缓存格式和前台缓存格式相同 //创建Direct3D设备对象 if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice))) { return E_FAIL; } return S_OK; } //------------------------------------------------------ //Desc:释放创建对象 //------------------------------------------------------ VOID Cleanup() { //释放Direct3D设备对象 if(g_pd3dDevice!=NULL) g_pd3dDevice->Release();//对应COM计数器会减一 //释放Direct3D对象 if(g_pD3D != NULL) g_pD3D->Release();//对应COM计数器会减一 } //------------------------------------------------------ //Desc:渲染图形 //------------------------------------------------------ VOID Render() { //清空后台缓存 g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,255,255),1.0f,0); //矩形数量;清空矩形区域,若为NULL,则第一个参数为0;清空内容:颜色缓冲区;清空颜色缓冲区后颜色缓冲区的值;颜色深度;模板缓冲区值 if(SUCCEEDED(g_pd3dDevice->BeginScene())) { //后台缓冲区绘制图形 //结束后台缓冲区绘制图形 g_pd3dDevice->EndScene(); } //将后台缓冲区绘制的图形提交到前台缓冲区显示 g_pd3dDevice->Present(NULL,NULL,NULL,NULL); //参数说明:复制源的矩形区域指针;复制目标的矩形区域指针;当前窗口指针;最小更新区域指针 } //------------------------------------------------------ //Desc:消息处理 //------------------------------------------------------ LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch(msg) { case WM_DESTROY: Cleanup(); PostQuitMessage(0); return 0; case WM_PAINT: Render(); ValidateRect(hWnd,NULL); return 0; } return DefWindowProc(hWnd,msg,wParam,lParam); }

你可能感兴趣的:(null,Parameters,图形,Direct3D,winapi)