Here is a class that load D2DBitmap and draw it.
class YHMSprite
{
public:
YHMSprite(const std::shared_ptr<DX::DeviceResources>& deviceResources);
//YHMSprite(const std::shared_ptr<DX::DeviceResources>& deviceResources, LPCWSTR fileName);
//YHMSprite(const std::shared_ptr<DX::DeviceResources>& deviceResources, Platform::String^ fileName);
~YHMSprite();
void LoadResource(LPCWSTR fileName);
void Draw(float scale, float x, float y);
void Draw(float scaleX, float scaleY, float x, float y);
void Draw(float scaleX, float scaleY, float x, float y, float opacity);
D2D1_SIZE_F GetBitmapSize();
private:
std::shared_ptr<DX::DeviceResources> mDeviceResources;
bool mIsResourceLoaded;
ID2D1DrawingStateBlock *mStateBlock;
ID2D1Bitmap *mBitmap;
D2D1_SIZE_F mBitmapSize;
};
And I create these resource
YHMSprite::YHMSprite(const std::shared_ptr<DX::DeviceResources>& deviceResources)
{
mIsResourceLoaded = false;
mDeviceResources = deviceResources;
mDeviceResources->GetD2DFactory()->CreateDrawingStateBlock(&mStateBlock);
}
void YHMSprite::LoadResource(LPCWSTR fileName)
{
ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
mDeviceResources->GetWicImagingFactory()->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &wicBitmapDecoder);
ComPtr<IWICBitmapFrameDecode> wicBitmapFrame;
wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
ComPtr<IWICFormatConverter> wicFormatConverter;
mDeviceResources->GetWicImagingFactory()->CreateFormatConverter(&wicFormatConverter);
wicFormatConverter->Initialize(wicBitmapFrame.Get(), GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.0, WICBitmapPaletteTypeCustom);
double DpiX = 72.0f, DpiY = 72.0f;
wicFormatConverter->GetResolution(&DpiX, &DpiY);
mDeviceResources->GetD2DDeviceContext()->CreateBitmapFromWicBitmap(wicFormatConverter.Get(), BitmapProperties(PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), static_cast<float>(DpiX), static_cast<float>(DpiY)), &mBitmap);
mBitmapSize = mBitmap->GetSize();
mIsResourceLoaded = true;
}
In my code, I release these resource using this destruction function
YHMSprite::~YHMSprite()
{
mBitmap->Release();
mStateBlock->Release();
}
I release my resource like this:
YHMSprite* test = new YHMSprite(....);
delete test;
But it doesn't release test completed!
Thanks.