无法使用Direct2D png中的DirectX9绘制图像精灵

时间:2013-10-23 02:28:25

标签: c++ directx direct3d directx-9 direct2d

我正在尝试将一些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, &center, &position, D3DCOLOR_XRGB(255,255,255));

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

此行发生错误d3dspt-&gt; 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正在被破坏,因为它的地址现在是0xFFFFFFFFFFFFFFFF。我不知道为什么会这样。继续调试并尝试找到我的问题的解决方案,但来自SO的任何输入都会很棒! :)

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

1 个答案:

答案 0 :(得分:1)

阿。看起来好像文件在创建后仍然处于锁定状态。必须'->Release()'流和缓冲区等。我想我会尝试将文件写入内存,所以我不必每帧重新分配这些文件。