创建D3D/DARECTd3d11 draw capsule设备失败

当前位置: >>>
D3DDraw 使用D3D的2D精灵绘制图形
为了偷懒使用二次线性插值缩放绘制图片,用GDI+,发现慢了不少.就抽时间写了个用D3D的2D精灵绘制图片的类,以为可以提高速度,结果更慢...
为了偷懒使用二次线性插值缩放绘制图片,用GDI+,发现慢了不少.就抽时间写了个用D3D的2D精灵绘制图片的类,以为可以提高速度,结果更慢... 郁闷到了... 代码贴这里给大家拍砖,帮我找找问题啊 T_T ================================= 分割线 ======================================== D3DDraw.h // 喜之狼の裤子
////////////////////////////////////////////////////////////////////////// #pragma once #include &Windows.h& #include &d3d9.h& #include &d3dx9.h& #pragma comment(lib, &d3d9.lib&) #pragma comment(lib, &d3dx9.lib&) class D3DTexture { public: &D3DTexture(IDirect3DDevice9* pd3dDevice) &{ & m_pd3dDevice = pd3dD & m_pd3dTexture = NULL; & m_pd3dSurface = NULL; & m_pd3dSurfaceSystemmem = NULL; & m_nWidth = 0; & m_nHeight = 0; &} &virtual ~D3DTexture() &{ & Release(); &} &virtual void Release() &{ & SAFE_RELEASE(m_pd3dTexture); & SAFE_RELEASE(m_pd3dSurface); & SAFE_RELEASE(m_pd3dSurfaceSystemmem); &} &virtual BOOL Create(DWORD nWidth, DWORD nHeight) &{ & if (m_pd3dDevice == NULL) && return FALSE; & if (m_pd3dTexture != NULL &&
&& m_pd3dSurface != NULL &&
&& m_nWidth == nWidth && && m_nHeight == nHeight) && return TRUE; & Release(); & if(FAILED(D3DXCreateTexture(m_pd3dDevice, nWidth, nHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pd3dTexture))) & { && if (FAILED(D3DXCreateTexture(m_pd3dDevice, nWidth, nHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pd3dTexture))) &&& return FALSE; & } & if (FAILED(m_pd3dTexture-&GetSurfaceLevel(0, &m_pd3dSurface))) && return FALSE; & m_nWidth = nW & m_nHeight = nH & return TRUE; &} &virtual BOOL ReCreate() &{ & if (m_nWidth == 0 || m_nHeight == 0) && return FALSE; & Release(); & return Create(m_nWidth, m_nHeight); &} &virtual BOOL LoadImage(BYTE* pImageBuffer, INT32 nFourcc, DWORD nPitch, RECT rImage, DWORD nColorKey = 0) &{ & if (m_pd3dSurface == NULL) && return FALSE;& & D3DFORMAT d3 & switch(nFourcc) & { & case MAKEFOURCC('U','Y','V','Y'): && d3dfmt = D3DFMT_UYVY; && & case MAKEFOURCC('Y','U','Y','2'): && d3dfmt = D3DFMT_YUY2; && & case MAKEFOURCC('R','G','B','A'): && d3dfmt = D3DFMT_A8R8G8B8; && & case MAKEFOURCC('R','V','2','4'): && d3dfmt = D3DFMT_R8G8B8; && & default: && return FALSE; & } & RECT rDec = {0, 0, m_nWidth, m_nHeight}; & DWORD nFilter = D3DX_FILTER_NONE; & if (rImage.right-rImage.left != m_nWidth || && rImage.bottom-rImage.top != m_nHeight) && nFilter = D3DX_FILTER_LINEAR; & if (FAILED(D3DXLoadSurfaceFromMemory(m_pd3dSurface,
&& NULL, &rDec, pImageBuffer, d3dfmt, nPitch, NULL, &rImage, nFilter, nColorKey))) && return FALSE; & return TRUE; &} &virtual UINT32 Width() const { return m_nW } &virtual UINT32 Height() const { return m_nH } protected: &friend class D3DD & &IDirect3DDevice9* m_pd3dD &IDirect3DTexture9* m_pd3dT &IDirect3DSurface9* m_pd3dS &IDirect3DSurface9* m_pd3dSurfaceS &UINT32&&& m_nW &UINT32&&& m_nH }; class D3DDraw { public: &D3DDraw(); &virtual ~D3DDraw(); & &virtual BOOL&& Init(HWND hWnd); &virtual D3DTexture*& CreateTexture(UINT nWidth, UINT nHeight); &virtual BOOL&& DrawImage(BYTE* pImageBuff, INT nImageForcc, DWORD nImageWidth, DWORD nImageHeight,
&&&&&&&&& DWORD nPitch, DWORD nColorKey = 0, RECT* pRectDec = NULL); &virtual BOOL&& DrawTexture(D3DTexture* pd3dTexture, DWORD nUseColorMask = 0xFFFFFFFF, &&&&&&&&& RECT* pRectSrc = NULL, RECT* pRectDec = NULL); &virtual BOOL&& Render(RECT* pRect = NULL); protected: &virtual BOOL&& CheckDeviceLost(); &virtual BOOL&& RestoreDevice(); &virtual BOOL&& CheckWindowSize(); &virtual BOOL&& LostDevice(); &virtual BOOL&& SetDevice(); &virtual BOOL&& SetPresent(HWND hWnd); &IDirect3D9*&&& m_pd3d;&&& //Direct3D 9 interface &IDirect3DDevice9*& m_pd3dD& //Graphics adapter interface &D3DCAPS9&&& m_d3dC&& //Capabilities of graphics adapter &D3DPRESENT_PARAMETERS m_d3dP& //Direct3D present parameters & &ID3DXSprite*&&& m_pd3dS &BOOL&&&& m_bDeviceL &HWND&&&& m_hW &RECT&&&& m_rW &D3DTexture*&&& m_pd3dI typedef map&D3DTexture*, D3DTexture*& TextureL &TextureList&&& m_vTextureL }; =============================================================================== ********************************** 漂亮的分割线 *********************************** =============================================================================== D3DDraw.cpp #include &stdafx.h& #include &D3DDraw.h& //Clear backbuffer static inline BOOL ClearBuffer(IDirect3DDevice9* pd3dDevice)
&if (FAILED(pd3dDevice-&Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0x00,0x00,0x00,0x00), 0.0f, 0))) & return FALSE; &return TRUE; } //Present static inline HRESULT Present(IDirect3DDevice9* pd3dDevice, const RECT* pRect/* = NULL*/)
&HRESULT &hr = pd3dDevice-&Present(pRect, pRect, NULL, NULL); & } //Begin drawing static inline BOOL BeginDrawing(IDirect3DDevice9* pd3dDevice)
&if (FAILED(pd3dDevice-&BeginScene())) & return FALSE; &return TRUE; } //End drawing static inline BOOL EndDrawing(IDirect3DDevice9* pd3dDevice)
&if (FAILED(pd3dDevice-&EndScene())) & return FALSE; &return TRUE; } ////////////////////////////////////////////////////////////////////////// D3DDraw::D3DDraw() { &m_pd3d = NULL; &m_pd3dDevice = NULL; &m_pd3dSprite = NULL; &m_bDeviceLost = FALSE; &m_pd3dImage = NULL; } D3DDraw::~D3DDraw() { &SAFE_RELEASE(m_pd3dSprite); &SAFE_RELEASE(m_pd3dDevice); &SAFE_RELEASE(m_pd3d); &for (TextureList::iterator p = m_vTextureList.begin(); p != m_vTextureList.end(); p++){ & delete p-& &} &m_vTextureList.clear(); } BOOL D3DDraw::Init(HWND hWnd) { &HRESULT &//Make Direct3D object &m_pd3d = Direct3DCreate9(D3D_SDK_VERSION); &//Make sure NULL pointer was not returned &if (!m_pd3d) & return FALSE; &//Get device capabilities &ZeroMemory (&m_d3dCaps, sizeof(m_d3dCaps)); &if (FAILED(m_pd3d-&GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_d3dCaps))) & return FALSE; &if (SetPresent(hWnd) == FALSE) & return FALSE; &//Check if hardware vertex processing is available &if (m_d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) &{&&&
& //Create device with hardware vertex processing & hr = m_pd3d-&CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd, && D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_d3dPresent, &m_pd3dDevice);&&&&&&&
&} &else &{ & //Create device with software vertex processing & hr = m_pd3d-&CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd, && D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_d3dPresent, &m_pd3dDevice); &} &//Make sure device was created &if (FAILED(hr)) & return FALSE; &if (SetDevice() == FALSE) & return FALSE; &//Crete sprite for draw 2d &if (m_pd3dSprite == NULL){ & if (FAILED(D3DXCreateSprite(m_pd3dDevice, &m_pd3dSprite))){ && return FALSE; & } &} &m_bDeviceLost = FALSE; &m_hWnd = hW &return TRUE; } BOOL D3DDraw::SetPresent(HWND hWnd) { &//Get display mode &D3DDISPLAYMODE d3 &if (FAILED(m_pd3d-&GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm))) & return FALSE; &if(FAILED(m_pd3d-&CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, d3ddm.Format, TRUE))) & return FALSE; &//Get window bounds &::GetClientRect (hWnd, &m_rWindow); &//Setup present parameters &ZeroMemory(&m_d3dPresent,sizeof(m_d3dPresent)); &m_d3dPresent.SwapEffect = D3DSWAPEFFECT_COPY; &m_d3dPresent.hDeviceWindow = hW &m_d3dPresent.Windowed = TRUE; &m_d3dPresent.BackBufferCount = 1; &m_d3dPresent.BackBufferWidth = m_rWindow. &m_d3dPresent.BackBufferHeight = m_rWindow. &return TRUE; } D3DTexture* D3DDraw::CreateTexture(UINT nWidth, UINT nHeight) { &if (m_hWnd == NULL) & return NULL; &D3DTexture* pTexture = new D3DTexture(m_pd3dDevice); &if (pTexture-&Create(nWidth, nHeight) == FALSE) &{ & delete pT & return NULL; &} &m_vTextureList.insert(make_pair(pTexture, pTexture)); &return pT } BOOL D3DDraw::DrawImage(BYTE* pImageBuff, INT nImageForcc, DWORD nImageWidth, DWORD nImageHeight,
&&&&& DWORD nPitch, DWORD nColorKey/* = 0*/, RECT* pRectDec/* = NULL*/) { &DWORD nWidth = nImageW &DWORD nHeight = nImageH &if (pRectDec != NULL) &{ & nWidth = pRectDec-&right-pRectDec-& & nHeight = pRectDec-&bottom-pRectDec-& &} &if (m_pd3dImage != NULL) &{ & if (m_pd3dImage-&Width() != nWidth || m_pd3dImage-&Height() != nHeight) & { && TextureList::iterator p = m_vTextureList.find(m_pd3dImage); && if (p != m_vTextureList.end()) && { &&& delete p-& &&& m_vTextureList.erase(p); && } && m_pd3dImage = NULL; & } &} &if (m_pd3dImage == NULL) &{ & m_pd3dImage = CreateTexture(nWidth, nHeight); & if (m_pd3dImage == NULL) && return FALSE; & m_vTextureList.insert(make_pair(m_pd3dImage, m_pd3dImage)); &} &RECT r = {0,0,nImageWidth,nImageHeight}; &if (m_pd3dImage-&LoadImage(pImageBuff, nImageForcc, nPitch, r, nColorKey) == FALSE) & return FALSE; &BOOL &ret = DrawTexture(m_pd3dImage, 0xFFFFFFFF, NULL, pRectDec); & } BOOL D3DDraw::DrawTexture(D3DTexture* pd3dTexture, DWORD nUseColorMask /*= 0xFFFFFFFF*/, &&&&&&& RECT* pRectSrc/* = NULL*/, RECT* pRectDec/* = NULL*/) { &if (pd3dTexture-&Width() == 0 || pd3dTexture-&Height() == 0) & return FALSE; &if (CheckWindowSize() == FALSE) & return FALSE; &if (CheckDeviceLost() == FALSE) & return FALSE; &if (BeginDrawing(m_pd3dDevice) == FALSE) & return FALSE; &do &{ & float nScalingW = 0.0f; & float nScalingH = 0.0f; & RECT & if (pRectDec != NULL) && r = *pRectD & else && ::GetClientRect(m_hWnd, &r); & nScalingW = (float)(r.right-r.left) / pd3dTexture-&Width(); & nScalingH = (float)(r.bottom-r.top) / pd3dTexture-&Height(); & D3DXMATRIX M & D3DXMatrixTransformation2D(&Mat,
&& &D3DXVECTOR2(0.0f, 0.0f),&&& // ScalingCenter && 0.0f,&&&&&&&& // ScalingRotation && &D3DXVECTOR2(nScalingW, nScalingH),& // Scaling && &D3DXVECTOR2(0.0f, 0.0f),&&& // RotationCenter && 0.0f,&&&&&&&& // Rotation && &D3DXVECTOR2(0.0f, 0.0f)&&& // Translation && ); & D3DXVECTOR3 pos(0.0f,0.0f,0.0f); & if (pRectDec != NULL) & { && pos.x = pRectDec-& && pos.y = pRectDec-& & } & m_pd3dSprite-&SetTransform(&Mat); & m_pd3dSprite-&Begin(0); & m_pd3dSprite-&Draw(pd3dTexture-&m_pd3dTexture, pRectSrc, NULL, &pos, nUseColorMask); & m_pd3dSprite-&End(); &} while (FALSE); &if (EndDrawing(m_pd3dDevice) == FALSE) & return FALSE; &return TRUE; } BOOL D3DDraw::Render(RECT* pRect/* = NULL*/) { &HRESULT &hr = Present(m_pd3dDevice, pRect); &if (hr == D3DERR_DEVICELOST) & m_bDeviceLost = TRUE; &if (FAILED(hr)) & return FALSE; &return TRUE; } BOOL D3DDraw::CheckWindowSize() { &RECT &GetClientRect(m_hWnd, &r); &if (r.right != m_rWindow.right || r.bottom != m_rWindow.bottom) &{ & m_rWindow = & m_d3dPresent.BackBufferWidth = r. & m_d3dPresent.BackBufferHeight = r. & LostDevice(); & RestoreDevice(); & return FALSE; &} &return TRUE; } BOOL D3DDraw::CheckDeviceLost() { &HRESULT &hr = m_pd3dDevice-&TestCooperativeLevel(); &if (SUCCEEDED(hr)) { & m_bDeviceLost = FALSE; & return TRUE; &} &m_bDeviceLost = TRUE; &if (hr == D3DERR_DEVICELOST){ & LostDevice(); & return FALSE; &} &else if (hr == D3DERR_DEVICENOTRESET){ & LostDevice(); & RestoreDevice(); & return FALSE; &} &return FALSE; } BOOL D3DDraw::LostDevice() { &for(TextureList::iterator p = m_vTextureList.begin(); p != m_vTextureList.end(); p++){ & p-&second-&Release(); &} &if (FAILED(m_pd3dSprite-&OnLostDevice())) & return FALSE; &return TRUE; } BOOL D3DDraw::RestoreDevice() { &if (SetPresent(m_hWnd) == FALSE) & return FALSE; &if (FAILED(m_pd3dDevice-&Reset(&m_d3dPresent))) & return FALSE; &if (SetDevice() == FALSE) & return FALSE; &if (FAILED(m_pd3dSprite-&OnResetDevice())) & return FALSE; &for(TextureList::iterator p = m_vTextureList.begin(); p != m_vTextureList.end(); p++){ & p-&second-&ReCreate(); &} &return TRUE; } BOOL D3DDraw::SetDevice() { &m_pd3dDevice-&SetRenderState(D3DRS_LIGHTING, FALSE); &m_pd3dDevice-&SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); &m_pd3dDevice-&SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); &m_pd3dDevice-&SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); &m_pd3dDevice-&SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); &m_pd3dDevice-&SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); &m_pd3dDevice-&SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); &return TRUE; }
(责任编辑:罗索客)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 []
本文出处: 作者:JackyHwei
------分隔线----------------------------
将本文分享到微信
1, 将 CreditStatic.h CreditStatic.cpp 文件添加到工程目录下,并加到...
windows下编译ffplay相关问题的解决方法。...
我原样贴出了我在我们工程里面写的代码, 里面有个CAPPLog::Lo...
这个图像公式大家在高中数学课都是会算滴。 然后我们要扩展一...
OPENGL ES 实现半透明纹理(画中画)的完整代码...
CImage封装了DIB(设备无关位图)的功能,因而可以让我们能够处理...

参考资料

 

随机推荐