シューティングゲーム(9)WIC + Direct2D でSprite表示

公開:2009-12-24 10:30
更新:2020-02-15 04:36
カテゴリ:windows,シューティングゲーム,ゲーム,ゲーム製作

WIC+Direct2Dでビットマップ描画してみた。
読み込みの部分はほとんどサンプルのままで、スマポにしたのとエラーを例外で飛ばすようにし、ビットマップを返り値にしたところが変更点。
20091223.png
以下コード。


ID2D1BitmapPtr load_bitmap_from_file(
ID2D1RenderTargetPtr render_target,
IWICImagingFactoryPtr wic_factory,
std::wstring uri,
boost::uint32_t destination_width,
boost::uint32_t destination_height
)
{
HRESULT hr = S_OK;
IWICBitmapDecoderPtr decoder;
IWICBitmapFrameDecodePtr decoder_source;
IWICStreamPtr stream;
IWICFormatConverterPtr converter;
IWICBitmapScalerPtr scaler;
ID2D1BitmapPtr bitmap;
EXCEPTION_ON_ERROR(wic_factory->CreateDecoderFromFilename(
uri.c_str(),
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&decoder
));
// Create the initial frame.
EXCEPTION_ON_ERROR(decoder->GetFrame(0, &decoder_source));
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
EXCEPTION_ON_ERROR(hr = wic_factory->CreateFormatConverter(&converter));
// If a new width or height was specified, create an
// IWICBitmapScaler and use it to resize the image.
if (destination_width != 0 || destination_height != 0)
{
UINT originalWidth, originalHeight;
EXCEPTION_ON_ERROR(decoder_source->GetSize(&originalWidth, &originalHeight));
if (destination_width == 0)
{
FLOAT scalar = static_cast<FLOAT>(destination_height) / static_cast<FLOAT>(originalHeight);
destination_width = static_cast<boost::uint32_t>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destination_height == 0)
{
FLOAT scalar = static_cast<FLOAT>(destination_width) / static_cast<FLOAT>(originalWidth);
destination_height = static_cast<boost::uint32_t>(scalar * static_cast<FLOAT>(originalHeight));
}
EXCEPTION_ON_ERROR(wic_factory->CreateBitmapScaler(&scaler));
EXCEPTION_ON_ERROR(scaler->Initialize(
decoder_source,
destination_width,
destination_height,
WICBitmapInterpolationModeCubic
));
EXCEPTION_ON_ERROR(converter->Initialize(
scaler.GetInterfacePtr(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
));
}
else // Don't scale the image.
{
EXCEPTION_ON_ERROR(converter->Initialize(
decoder_source.GetInterfacePtr(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
));
}
// Create a Direct2D bitmap from the WIC bitmap.
EXCEPTION_ON_ERROR(render_target->CreateBitmapFromWicBitmap(
converter.GetInterfacePtr(),
NULL,
&bitmap
));
return bitmap;
}