D3DEnvClass.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:2k
源码类别:
DirextX编程
开发平台:
Visual C++
- //--------------------------------------------------------------------------------------
- // 实现文件: D3DEnvClass.cpp, 作者:曾凡喜
- //
- // 版权归作者所有,不得用于商业用途
- //
- // 完成时间:2007-10-31
- //--------------------------------------------------------------------------------------
- #include "StdAfx.h"
- #include ".d3denvclass.h"
- CZFXD3DEnv::CZFXD3DEnv(void)
- {
- m_pD3D = NULL;
- m_pd3dDevice = NULL;
- }
- CZFXD3DEnv::~CZFXD3DEnv(void)
- {
- }
- HRESULT CZFXD3DEnv::InitD3D(BOOL bWindowed, HWND hWnd)
- {
- // 创建D3D对象
- if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION )))
- {
- ::MessageBox(0, "Create D3D Object FAILED", 0, 0);
- return E_FAIL;
- }
- //得到当前的显示模式
- D3DDISPLAYMODE d3ddm;
- if( FAILED( m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm )))
- {
- ::MessageBox(0, "Get Adapter Display Mode Failed", 0, 0);
- return E_FAIL;
- }
- ZeroMemory( &m_d3dpp, sizeof(m_d3dpp));
- if(!bWindowed)
- {
- // 全屏模式
- m_d3dpp.hDeviceWindow = hWnd;
- m_d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
- m_d3dpp.BackBufferCount = 1; //有一个后台缓存
- m_d3dpp.BackBufferWidth = d3ddm.Width;//屏幕宽度为800像素
- m_d3dpp.BackBufferHeight = d3ddm.Height;//屏幕长度为600像素
- m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- }
- else
- {
- // 窗口模式
- m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- }
- m_d3dpp.Windowed = bWindowed;
- m_d3dpp.BackBufferFormat = d3ddm.Format;
- m_d3dpp.EnableAutoDepthStencil = TRUE;
- m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- // 创建D3D设备
- if( FAILED( m_pD3D -> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
- D3DCREATE_MIXED_VERTEXPROCESSING, &m_d3dpp, &m_pd3dDevice)))
- {
- ::MessageBox(0, "Create D3D Device FAILED", 0, 0);
- return E_FAIL;
- }
- return D3D_OK;
- }
- void CZFXD3DEnv::MakeSureNotLost()
- {
- HRESULT result = m_pd3dDevice->TestCooperativeLevel();
- while(result != D3D_OK)
- {
- while(result != D3DERR_DEVICENOTRESET)
- {
- Sleep(1000);
- result = m_pd3dDevice->TestCooperativeLevel();
- }
- if(FAILED(m_pd3dDevice->Reset(&m_d3dpp)))
- result = D3DERR_DEVICELOST;
- }
- }
- void CZFXD3DEnv::Release()
- {
- if( m_pD3D != NULL)
- m_pD3D->Release();
- if( m_pd3dDevice != NULL)
- m_pd3dDevice->Release();
- }