diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index 12c22127..ff36b905 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -52,6 +52,21 @@ inline DXGI_FORMAT ConvertPixelFormat(PixelFormat format, UINT32& pitch) } } +inline const GUID& ConvertPixelFormat2WIC(PixelFormat format, UINT& stride) +{ + switch (format) + { + case PixelFormat::Bpp32RGBA: + stride = 4; + return GUID_WICPixelFormat32bppRGBA; + case PixelFormat::Bpp32BGRA: + stride = 4; + return GUID_WICPixelFormat32bppBGRA; + default: + return GUID_WICPixelFormatDontCare; + } +} + Renderer& Renderer::GetInstance() { return RendererImpl::GetInstance(); @@ -316,6 +331,56 @@ void RendererImpl::CreateTexture(Texture& texture, const BinaryData& data) KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture failed"); } +void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const BinaryData& data, PixelFormat format) +{ + HRESULT hr = S_OK; + if (!d2d_res_) + { + hr = E_UNEXPECTED; + } + + if (SUCCEEDED(hr)) + { + hr = data.IsValid() ? S_OK : E_FAIL; + + if (SUCCEEDED(hr)) + { + UINT stride = 0; + const auto& wicFormat = ConvertPixelFormat2WIC(format, stride); + + ComPtr source; + hr = d2d_res_->CreateBitmapSourceFromMemory(source, UINT(size.x), UINT(size.y), UINT(size.x) * stride, + UINT(data.size), reinterpret_cast(data.buffer), + wicFormat); + + if (SUCCEEDED(hr)) + { + ComPtr converter; + hr = d2d_res_->CreateBitmapConverter(converter, source, GUID_WICPixelFormat32bppPBGRA, + WICBitmapDitherTypeNone, nullptr, 0.f, + WICBitmapPaletteTypeMedianCut); + + if (SUCCEEDED(hr)) + { + ComPtr bitmap; + hr = d2d_res_->CreateBitmapFromConverter(bitmap, nullptr, converter); + + if (SUCCEEDED(hr)) + { + ComPolicy::Set(texture, bitmap); + + texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); + texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); + } + } + } + } + } + + KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture from memory failed"); +} + +/* void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const BinaryData& data, PixelFormat format) { HRESULT hr = S_OK; @@ -351,6 +416,7 @@ void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture from memory failed"); } +*/ void RendererImpl::CreateGifImage(GifImage& gif, const String& file_path) {