我正在尝试将一些Direct2D绘图合并到Ogre项目中。目前,Ogre仅真正支持DirectX9,这会导致使用Direct2D带来一些麻烦。我的解决方案是这样。使用Direct2D将我的覆盖图绘制为png,然后使用DirectX将图像绘制为精灵。我尝试执行此方法,并且在draw方法完成后,精灵已损坏。首先是Direct2D绘图代码:
void SceneManager::_initalize( void ){
// Initalize the bitmiap
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void **>(&pWICFactory)
);
// Initalize Direct2D
if(SUCCEEDED(hr)){
hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory );
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateBitmap(
rc.right - rc.left,
rc.bottom - rc.top,
GUID_WICPixelFormat8bppAlpha,
WICBitmapCacheOnLoad,
&pBitMap
);
}
if(SUCCEEDED(hr)){
hr = pD2DFactory->CreateWicBitmapRenderTarget(
pBitMap,
D2D1::RenderTargetProperties(),
&pRenderTarget
);
}
if(SUCCEEDED(hr)){
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1,1,1,1), &pWhiteBrush);
}
// Draw to the bitmap here :)
if(SUCCEEDED(hr)){
pRenderTarget->BeginDraw();
pRenderTarget->Clear(D2D1::ColorF(0,0,0,0));
pRenderTarget->FillRectangle( D2D1::RectF( 0, 0, 500, 500), pWhiteBrush);
pRenderTarget->EndDraw();
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateStream(&pStream);
}
WICPixelFormatGUID format = GUID_WICPixelFormat8bppAlpha;
if(SUCCEEDED(hr)){
hr = pStream->InitializeFromFilename(L"c:\\tmp\\dsplyr.xmg", GENERIC_WRITE);
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
}
if(SUCCEEDED(hr)){
hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
}
if(SUCCEEDED(hr)){
hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->Initialize(NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->SetSize(rc.right - rc.left, rc.bottom - rc.top);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->SetPixelFormat(&format);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->WriteSource(pBitMap, NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->Commit();
}
if(SUCCEEDED(hr)){
hr = pEncoder->Commit();
}
if(!SUCCEEDED(hr)){
MessageBox(NULL, L"ERROR!", L"ERROR!", MB_OKCANCEL);
}
}
这是绘制代码:
void SceneManager::Render( void ){
LPDIRECT3DDEVICE9 pd3dDevice = NULL; // Our rendering device
LPD3DXSPRITE d3dspt;
LPDIRECT3DTEXTURE9 sprite;
mWindow->getCustomAttribute("D3DDEVICE", &pd3dDevice);
D3DXCreateSprite(pd3dDevice, &d3dspt);
//D3DXCreateTextureFromFileInMemory(pd3dDevice, pBitMap, sizeof(pBitMap) , &sprite);
D3DXCreateTextureFromFile(pd3dDevice, L"c:\\tmp\\testxmg" , &sprite);
d3dspt->Begin( D3DXSPRITE_ALPHABLEND );
D3DXVECTOR3 center(0,0,0);
D3DXVECTOR3 position(0,0,0);
d3dspt->Draw(sprite, &rc, ¢er, &position, D3DCOLOR_XRGB(255,255,255));
d3dspt->End();
sprite->Release();
d3dspt->Release();
}
错误发生在此行d3dspt-> End();。
这是错误:
First-chance exception at 0x000007FA34B51F9C (D3DX9_43.dll) in GUI.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Unhandled exception at 0x000007FA34B51F9C (D3DX9_43.dll) in GUI.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
显然,d3dspt已损坏,因为其地址现在为0xFFFFFFFFFFFFFFFFFF。我不知道为什么会这样。将继续调试并尝试找到解决我的问题的方法,但是SO的任何输入都很棒! :)
环境信息:
Microsfot Visual Studio 2012
C++
食人魔1.9
DirectX9
Direct2D
最佳答案
啊。在文件创建后看起来仍然处于锁定状态。必须“->Release()
”流和缓冲区等。认为我将尝试将文件写入内存,因此不必在每个帧中重新分配这些文件。
关于c++ - 无法使用Direct2D png中的DirectX9绘制图像 Sprite ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19531702/