2013-10-23 73 views
0

我試圖將一些Direct2D繪圖合併到Ogre項目中。目前Ogre只支持DirectX9,這會導致使用Direct2D時出現一些問題。我的解決辦法就是這樣使用Direct2D將我的覆蓋圖繪製到png,然後使用DirectX將圖像繪製到精靈。我試着做這個方法,一旦繪製方法完成,精靈就會被破壞。首先的Direct2D繪圖代碼:無法從Direct2D中使用DirectX9繪製圖像精靈png

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, &center, &position, D3DCOLOR_XRGB(255,255,255)); 

    d3dspt->End(); 
    sprite->Release(); 
    d3dspt->Release(); 
} 

在這條線d3dspt-發生該錯誤>結束();

這裏是錯誤:

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是成爲損壞,buecause它現在地址是0xFFFFFFFFFFFFFFFF。我不知道爲什麼會這樣。要繼續調試並嘗試找到解決我的問題的方法,但是來自SO的任何輸入都會非常棒! :)

環境信息: Microsfot的Visual Studio 2012 C++ 食人魔1.9 DirectX9的 的Direct2D

回答

1

啊。看起來好像該文件在創建後仍處於鎖定狀態。必須'->Release()'的流和緩衝區等。想想我會嘗試將文件寫入內存,所以我不必重新分配這些每一幀。