| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 11:26:21 +08:00
										 |  |  | #include <kiwano/utils/Logger.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | #include <kiwano/platform/FileSystem.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:01:56 +08:00
										 |  |  | #include <kiwano/platform/Application.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | #include <kiwano/render/ShapeMaker.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | #include <kiwano/render/DirectX/RendererImpl.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | #include <kiwano/render/DirectX/NativePtr.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 18:51:02 +08:00
										 |  |  | #define KGE_SET_STATUS_IF_FAILED(ERRCODE, OBJ, MESSAGE)                                   \
 | 
					
						
							|  |  |  |     if (FAILED(ERRCODE))                                                                  \ | 
					
						
							|  |  |  |     {                                                                                     \ | 
					
						
							|  |  |  |         OBJ.Fail(strings::Format("%s failed (%#x): %s", __FUNCTION__, ERRCODE, MESSAGE)); \ | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-17 21:02:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 21:07:32 +08:00
										 |  |  | using namespace kiwano::graphics::directx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | Renderer& Renderer::GetInstance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return RendererImpl::GetInstance(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RendererImpl& RendererImpl::GetInstance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static RendererImpl instance; | 
					
						
							|  |  |  |     return instance; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RendererImpl::RendererImpl() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:01:56 +08:00
										 |  |  | void RendererImpl::MakeContextForWindow(WindowPtr window) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-17 12:23:18 +08:00
										 |  |  |     KGE_DEBUG_LOGF("Creating device resources"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     KGE_THROW_IF_FAILED(::CoInitialize(nullptr), "CoInitialize failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 01:56:02 +08:00
										 |  |  |     HWND       target_window = window->GetHandle(); | 
					
						
							|  |  |  |     Resolution resolution    = window->GetCurrentResolution(); | 
					
						
							|  |  |  |     HRESULT    hr            = target_window ? S_OK : E_FAIL; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 01:56:02 +08:00
										 |  |  |     output_size_ = Size{ float(resolution.width), float(resolution.height) }; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 12:02:04 +08:00
										 |  |  |     // Initialize Direct3D resources
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto d3d_res = graphics::directx::GetD3DDeviceResources(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 01:56:02 +08:00
										 |  |  |         hr = d3d_res->Initialize(target_window, output_size_); | 
					
						
							| 
									
										
										
										
											2020-05-29 12:02:04 +08:00
										 |  |  |         if (FAILED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             d3d_res->DiscardResources(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             d3d_res_ = d3d_res; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Initialize Direct2D resources
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto d2d_res = graphics::directx::GetD2DDeviceResources(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         hr = d2d_res->Initialize(d3d_res_->GetDXGIDevice(), d3d_res_->GetDXGISwapChain()); | 
					
						
							|  |  |  |         if (FAILED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             d2d_res->DiscardResources(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             d2d_res_ = d2d_res; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 00:12:36 +08:00
										 |  |  |     // Initialize other device resources
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-07-22 21:08:48 +08:00
										 |  |  |         RenderContextImplPtr ctx = MakePtr<RenderContextImpl>(); | 
					
						
							| 
									
										
										
										
											2020-05-22 21:07:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 00:12:36 +08:00
										 |  |  |         hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext()); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-05-26 00:12:36 +08:00
										 |  |  |             render_ctx_ = ctx; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     KGE_THROW_IF_FAILED(hr, "Create render resources failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:01:56 +08:00
										 |  |  | void RendererImpl::Destroy() | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-17 12:23:18 +08:00
										 |  |  |     KGE_DEBUG_LOGF("Destroying device resources"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 12:02:04 +08:00
										 |  |  |     if (d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         render_ctx_.Reset(); | 
					
						
							|  |  |  |         d2d_res_->DiscardResources(); | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |         d2d_res_.Reset(); | 
					
						
							| 
									
										
										
										
											2020-05-29 12:02:04 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 12:02:04 +08:00
										 |  |  |     if (d3d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         d3d_res_->DiscardResources(); | 
					
						
							|  |  |  |         d3d_res_.Reset(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ::CoUninitialize(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RendererImpl::Clear() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(d3d_res_); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 14:41:19 +08:00
										 |  |  |     d3d_res_->ClearRenderTarget(clear_color_); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RendererImpl::Present() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(d3d_res_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     HRESULT hr = d3d_res_->Present(vsync_); | 
					
						
							| 
									
										
										
										
											2020-05-22 21:07:32 +08:00
										 |  |  |     if (FAILED(hr) && hr != DXGI_ERROR_WAS_STILL_DRAWING) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         KGE_THROW_IF_FAILED(hr, "Unexpected DXGI exception"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateTexture(Texture& texture, const String& file_path) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!FileSystem::GetInstance().IsFileExists(file_path)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-13 22:35:04 +08:00
										 |  |  |         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); | 
					
						
							| 
									
										
										
										
											2020-07-26 11:51:27 +08:00
										 |  |  |         KGE_SET_STATUS_IF_FAILED(hr, texture, | 
					
						
							|  |  |  |                                  strings::Format("Texture file '%s' not found!", file_path.c_str()).c_str()); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  |         WideString full_path = strings::NarrowToWide(FileSystem::GetInstance().GetFullPathForFile(file_path)); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ComPtr<IWICBitmapDecoder> decoder; | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |         hr = d2d_res_->CreateBitmapDecoderFromFile(decoder, full_path.c_str()); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<IWICBitmapFrameDecode> source; | 
					
						
							|  |  |  |             hr = decoder->GetFrame(0, &source); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ComPtr<IWICFormatConverter> converter; | 
					
						
							|  |  |  |                 hr = d2d_res_->CreateBitmapConverter(converter, source, GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |                                                      WICBitmapDitherTypeNone, nullptr, 0.f, | 
					
						
							|  |  |  |                                                      WICBitmapPaletteTypeMedianCut); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     ComPtr<ID2D1Bitmap> bitmap; | 
					
						
							|  |  |  |                     hr = d2d_res_->CreateBitmapFromConverter(bitmap, nullptr, converter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                         NativePtr::Set(texture, bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); | 
					
						
							|  |  |  |                         texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateTexture(Texture& texture, const Resource& resource) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |         Resource::Data data = resource.GetData(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:39:44 +08:00
										 |  |  |         hr = data.IsValid() ? S_OK : E_FAIL; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |             ComPtr<IWICBitmapDecoder> decoder; | 
					
						
							|  |  |  |             hr = d2d_res_->CreateBitmapDecoderFromResource(decoder, data.buffer, (DWORD)data.size); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |                 ComPtr<IWICBitmapFrameDecode> source; | 
					
						
							|  |  |  |                 hr = decoder->GetFrame(0, &source); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |                     ComPtr<IWICFormatConverter> converter; | 
					
						
							|  |  |  |                     hr = d2d_res_->CreateBitmapConverter(converter, source, GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |                                                          WICBitmapDitherTypeNone, nullptr, 0.f, | 
					
						
							|  |  |  |                                                          WICBitmapPaletteTypeMedianCut); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |                         ComPtr<ID2D1Bitmap> bitmap; | 
					
						
							|  |  |  |                         hr = d2d_res_->CreateBitmapFromConverter(bitmap, nullptr, converter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                             NativePtr::Set(texture, bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); | 
					
						
							|  |  |  |                             texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateGifImage(GifImage& gif, const String& file_path) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!FileSystem::GetInstance().IsFileExists(file_path)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-13 22:35:04 +08:00
										 |  |  |         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); | 
					
						
							| 
									
										
										
										
											2020-07-26 11:51:27 +08:00
										 |  |  |         KGE_SET_STATUS_IF_FAILED(hr, gif, | 
					
						
							|  |  |  |                                  strings::Format("Gif texture file '%s' not found!", file_path.c_str()).c_str()); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  |         WideString full_path = strings::NarrowToWide(FileSystem::GetInstance().GetFullPathForFile(file_path)); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ComPtr<IWICBitmapDecoder> decoder; | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |         hr = d2d_res_->CreateBitmapDecoderFromFile(decoder, full_path.c_str()); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             NativePtr::Set(gif, decoder); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, gif, "Load GIF texture failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateGifImage(GifImage& gif, const Resource& resource) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |         Resource::Data data = resource.GetData(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:39:44 +08:00
										 |  |  |         hr = data.IsValid() ? S_OK : E_FAIL; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |             ComPtr<IWICBitmapDecoder> decoder; | 
					
						
							|  |  |  |             hr = d2d_res_->CreateBitmapDecoderFromResource(decoder, data.buffer, (DWORD)data.size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 NativePtr::Set(gif, decoder); | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, gif, "Load GIF texture failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateGifImageFrame(GifImage::Frame& frame, const GifImage& gif, size_t frame_index) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto decoder = NativePtr::Get<IWICBitmapDecoder>(gif); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!decoder) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         hr = E_INVALIDARG; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ComPtr<IWICBitmapFrameDecode> wic_frame; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         hr = decoder->GetFrame(UINT(frame_index), &wic_frame); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<IWICFormatConverter> converter; | 
					
						
							|  |  |  |             d2d_res_->CreateBitmapConverter(converter, wic_frame, GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |                                             WICBitmapDitherTypeNone, nullptr, 0.f, WICBitmapPaletteTypeCustom); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 ComPtr<ID2D1Bitmap> bitmap; | 
					
						
							|  |  |  |                 hr = d2d_res_->CreateBitmapFromConverter(bitmap, nullptr, converter); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2020-07-22 21:08:48 +08:00
										 |  |  |                     frame.texture = MakePtr<Texture>(); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                     NativePtr::Set(frame.texture, bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     frame.texture->SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); | 
					
						
							|  |  |  |                     frame.texture->SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             PROPVARIANT prop_val; | 
					
						
							|  |  |  |             PropVariantInit(&prop_val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Get Metadata Query Reader from the frame
 | 
					
						
							|  |  |  |             ComPtr<IWICMetadataQueryReader> metadata_reader; | 
					
						
							|  |  |  |             hr = wic_frame->GetMetadataQueryReader(&metadata_reader); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Get the Metadata for the current frame
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/imgdesc/Left", &prop_val); | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         frame.rect.left_top.x = static_cast<float>(prop_val.uiVal); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/imgdesc/Top", &prop_val); | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         frame.rect.left_top.y = static_cast<float>(prop_val.uiVal); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/imgdesc/Width", &prop_val); | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         frame.rect.right_bottom.x = frame.rect.left_top.x + static_cast<float>(prop_val.uiVal); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/imgdesc/Height", &prop_val); | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         frame.rect.right_bottom.y = frame.rect.left_top.y + static_cast<float>(prop_val.uiVal); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/grctlext/Delay", &prop_val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         uint32_t udelay = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         hr = UIntMult(prop_val.uiVal, 10, &udelay); | 
					
						
							|  |  |  |                         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             frame.delay.SetMilliseconds(static_cast<long>(udelay)); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     frame.delay = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/grctlext/Disposal", &prop_val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     hr = (prop_val.vt == VT_UI1) ? S_OK : E_FAIL; | 
					
						
							|  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         frame.disposal_type = GifImage::DisposalType(prop_val.bVal); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     frame.disposal_type = GifImage::DisposalType::Unknown; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, const_cast<GifImage&>(gif), "Load GIF frame failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateFontCollection(Font& font, const String& file_path) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!FileSystem::GetInstance().IsFileExists(file_path)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-13 22:35:04 +08:00
										 |  |  |             hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); | 
					
						
							| 
									
										
										
										
											2020-07-26 11:51:27 +08:00
										 |  |  |             KGE_SET_STATUS_IF_FAILED(hr, font, strings::Format("Font file '%s' not found!", file_path.c_str()).c_str()); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |         String full_path = FileSystem::GetInstance().GetFullPathForFile(file_path); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |         ComPtr<IDWriteFontCollection> font_collection; | 
					
						
							|  |  |  |         hr = d2d_res_->CreateFontCollectionFromFiles(font_collection, { full_path }); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:54:29 +08:00
										 |  |  |         Vector<String> family_names; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-08-03 19:54:29 +08:00
										 |  |  |             UINT32 count = font_collection->GetFontFamilyCount(); | 
					
						
							|  |  |  |             for (UINT32 i = 0; i < count; i++) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ComPtr<IDWriteFontFamily> family; | 
					
						
							|  |  |  |                 if (SUCCEEDED(font_collection->GetFontFamily(i, &family))) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     ComPtr<IDWriteLocalizedStrings> str; | 
					
						
							|  |  |  |                     if (SUCCEEDED(family->GetFamilyNames(&str))) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         UINT32 length = 0; | 
					
						
							|  |  |  |                         if (SUCCEEDED(str->GetStringLength(0, &length))) | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             WideString name; | 
					
						
							|  |  |  |                             name.resize(length + 1); | 
					
						
							|  |  |  |                             if (SUCCEEDED(str->GetString(0, &name[0], UINT32(name.size())))) | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 family_names.emplace_back(strings::WideToNarrow(name)); | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             font.SetFamilyNames(family_names); | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |             NativePtr::Set(font, font_collection); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, font, "Create font collection failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateFontCollection(Font& font, const Resource& res) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |         ComPtr<IDWriteFontCollection> font_collection; | 
					
						
							|  |  |  |         hr = d2d_res_->CreateFontCollectionFromResources(font_collection, Vector<Resource>{ res }); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:54:29 +08:00
										 |  |  |         Vector<String> family_names; | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             UINT32 count = font_collection->GetFontFamilyCount(); | 
					
						
							|  |  |  |             for (UINT32 i = 0; i < count; i++) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ComPtr<IDWriteFontFamily> family; | 
					
						
							|  |  |  |                 if (SUCCEEDED(font_collection->GetFontFamily(i, &family))) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     ComPtr<IDWriteLocalizedStrings> str; | 
					
						
							|  |  |  |                     if (SUCCEEDED(family->GetFamilyNames(&str))) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         UINT32 length = 0; | 
					
						
							|  |  |  |                         if (SUCCEEDED(str->GetStringLength(0, &length))) | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             WideString name; | 
					
						
							|  |  |  |                             name.resize(length + 1); | 
					
						
							|  |  |  |                             if (SUCCEEDED(str->GetString(0, &name[0], UINT32(name.size())))) | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 family_names.emplace_back(strings::WideToNarrow(name)); | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-08-03 19:54:29 +08:00
										 |  |  |             font.SetFamilyNames(family_names); | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |             NativePtr::Set(font, font_collection); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, font, "Create font collection failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void RendererImpl::CreateTextLayout(TextLayout& layout, const String& content, const TextStyle& style) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (content.empty()) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         layout.Clear(); | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  |         layout.SetDirtyFlag(TextLayout::DirtyFlag::Dirty); | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (SUCCEEDED(hr)) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  |         WideString         font_family = style.font_family.empty() ? L"" : strings::NarrowToWide(style.font_family); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         DWRITE_FONT_WEIGHT font_weight = DWRITE_FONT_WEIGHT(style.font_weight); | 
					
						
							|  |  |  |         DWRITE_FONT_STYLE  font_style  = style.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL; | 
					
						
							|  |  |  |         auto               collection  = NativePtr::Get<IDWriteFontCollection>(style.font); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         ComPtr<IDWriteTextFormat> format; | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         hr = d2d_res_->CreateTextFormat(format, font_family.c_str(), collection, font_weight, font_style, | 
					
						
							|  |  |  |                                         DWRITE_FONT_STRETCH_NORMAL, style.font_size); | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  |             WideString wide = strings::NarrowToWide(content); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             ComPtr<IDWriteTextLayout> output; | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |             hr = d2d_res_->CreateTextLayout(output, wide.c_str(), wide.length(), format); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 NativePtr::Set(layout, output); | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  |                 layout.SetDirtyFlag(TextLayout::DirtyFlag::Dirty); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, layout, "Create text layout failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateLineShape(Shape& shape, const Point& begin_pos, const Point& end_pos) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         ComPtr<ID2D1PathGeometry> path_geo; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         hr = d2d_res_->GetFactory()->CreatePathGeometry(&path_geo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<ID2D1GeometrySink> path_sink; | 
					
						
							|  |  |  |             hr = path_geo->Open(&path_sink); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 path_sink->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED); | 
					
						
							|  |  |  |                 path_sink->AddLine(DX::ConvertToPoint2F(end_pos)); | 
					
						
							|  |  |  |                 path_sink->EndFigure(D2D1_FIGURE_END_OPEN); | 
					
						
							|  |  |  |                 hr = path_sink->Close(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 NativePtr::Set(shape, path_geo); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1PathGeometry failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateRectShape(Shape& shape, const Rect& rect) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ComPtr<ID2D1RectangleGeometry> output; | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = d2d_res_->GetFactory()->CreateRectangleGeometry(DX::ConvertToRectF(rect), &output); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         NativePtr::Set(shape, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RectangleGeometry failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateRoundedRectShape(Shape& shape, const Rect& rect, const Vec2& radius) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ComPtr<ID2D1RoundedRectangleGeometry> output; | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = d2d_res_->GetFactory()->CreateRoundedRectangleGeometry( | 
					
						
							|  |  |  |             D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), &output); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         NativePtr::Set(shape, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RoundedRectangleGeometry failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateEllipseShape(Shape& shape, const Point& center, const Vec2& radius) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ComPtr<ID2D1EllipseGeometry> output; | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = d2d_res_->GetFactory()->CreateEllipseGeometry( | 
					
						
							|  |  |  |             D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), &output); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         NativePtr::Set(shape, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1EllipseGeometry failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | void RendererImpl::CreateShapeSink(ShapeMaker& maker) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         ComPtr<ID2D1PathGeometry> geometry; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         hr = d2d_res_->GetFactory()->CreatePathGeometry(&geometry); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-07-22 21:08:48 +08:00
										 |  |  |             ShapePtr shape = MakePtr<Shape>(); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             NativePtr::Set(shape, geometry); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             maker.SetShape(shape); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, maker, "Create ID2D1PathGeometry failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateBrush(Brush& brush, const Color& color) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ComPtr<ID2D1SolidColorBrush> solid_brush; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         if (brush.GetType() == Brush::Type::SolidColor && brush.IsValid()) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             hr = NativePtr::Get<ID2D1Brush>(brush)->QueryInterface(&solid_brush); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 solid_brush->SetColor(DX::ConvertToColorF(color)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateSolidColorBrush(DX::ConvertToColorF(color), &solid_brush); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 NativePtr::Set(brush, solid_brush); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, brush, "Create ID2D1SolidBrush failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateBrush(Brush& brush, const LinearGradientStyle& style) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ComPtr<ID2D1GradientStopCollection> collection; | 
					
						
							|  |  |  |         hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( | 
					
						
							|  |  |  |             reinterpret_cast<const D2D1_GRADIENT_STOP*>(&style.stops[0]), UINT32(style.stops.size()), D2D1_GAMMA_2_2, | 
					
						
							|  |  |  |             D2D1_EXTEND_MODE(style.extend_mode), &collection); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<ID2D1LinearGradientBrush> output; | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateLinearGradientBrush( | 
					
						
							|  |  |  |                 D2D1::LinearGradientBrushProperties(DX::ConvertToPoint2F(style.begin), DX::ConvertToPoint2F(style.end)), | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |                 collection.Get(), &output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 NativePtr::Set(brush, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, brush, "Create ID2D1LinearGradientBrush failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  | void RendererImpl::CreateBrush(Brush& brush, const RadialGradientStyle& style) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ComPtr<ID2D1GradientStopCollection> collection; | 
					
						
							|  |  |  |         hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( | 
					
						
							|  |  |  |             reinterpret_cast<const D2D1_GRADIENT_STOP*>(&style.stops[0]), UINT32(style.stops.size()), D2D1_GAMMA_2_2, | 
					
						
							|  |  |  |             D2D1_EXTEND_MODE(style.extend_mode), &collection); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<ID2D1RadialGradientBrush> output; | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateRadialGradientBrush( | 
					
						
							|  |  |  |                 D2D1::RadialGradientBrushProperties(DX::ConvertToPoint2F(style.center), | 
					
						
							|  |  |  |                                                     DX::ConvertToPoint2F(style.offset), style.radius.x, style.radius.y), | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |                 collection.Get(), &output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 NativePtr::Set(brush, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, brush, "Create ID2D1RadialGradientBrush failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:32:08 +08:00
										 |  |  | void RendererImpl::CreateBrush(Brush& brush, TexturePtr texture) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto bitmap = NativePtr::Get<ID2D1Bitmap>(texture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ComPtr<ID2D1BitmapBrush> output; | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateBitmapBrush(bitmap.Get(), &output); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 NativePtr::Set(brush, output); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, brush, "Create ID2D1RadialGradientBrush failed"); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:32:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | void RendererImpl::CreateStrokeStyle(StrokeStyle& stroke_style) | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         D2D1_CAP_STYLE  cap         = D2D1_CAP_STYLE(stroke_style.GetCapStyle()); | 
					
						
							|  |  |  |         D2D1_LINE_JOIN  line_join   = D2D1_LINE_JOIN(stroke_style.GetLineJoinStyle()); | 
					
						
							|  |  |  |         D2D1_DASH_STYLE dash_style  = D2D1_DASH_STYLE_SOLID; | 
					
						
							|  |  |  |         const float*    dash_array  = nullptr; | 
					
						
							|  |  |  |         uint32_t        dash_count  = 0; | 
					
						
							|  |  |  |         float           dash_offset = stroke_style.GetDashOffset(); | 
					
						
							|  |  |  |         const auto&     dashes      = stroke_style.GetDashArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!dashes.empty()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             dash_array = &dashes[0]; | 
					
						
							|  |  |  |             dash_count = uint32_t(dashes.size()); | 
					
						
							|  |  |  |             dash_style = D2D1_DASH_STYLE_CUSTOM; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto params = D2D1::StrokeStyleProperties(cap, cap, cap, line_join, 10.0f, dash_style, dash_offset); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ComPtr<ID2D1StrokeStyle> output; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         hr = d2d_res_->GetFactory()->CreateStrokeStyle(params, dash_array, dash_count, &output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             NativePtr::Set(stroke_style, output); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 00:22:32 +08:00
										 |  |  |     KGE_SET_STATUS_IF_FAILED(hr, stroke_style, "Create ID2D1StrokeStyle failed"); | 
					
						
							| 
									
										
										
										
											2020-02-10 14:41:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  | RenderContextPtr RendererImpl::CreateTextureRenderContext(Texture& texture, const Size* desired_size) | 
					
						
							| 
									
										
										
										
											2020-02-10 14:41:19 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-22 21:08:48 +08:00
										 |  |  |     RenderContextImplPtr ptr = MakePtr<RenderContextImpl>(); | 
					
						
							| 
									
										
										
										
											2020-02-10 14:41:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  |     if (!d2d_res_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ComPtr<ID2D1BitmapRenderTarget> bitmap_rt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (desired_size) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateCompatibleRenderTarget(DX::ConvertToSizeF(*desired_size), | 
					
						
							|  |  |  |                                                                             &bitmap_rt); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             hr = d2d_res_->GetDeviceContext()->CreateCompatibleRenderTarget(&bitmap_rt); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             hr = ptr->CreateDeviceResources(d2d_res_->GetFactory(), bitmap_rt); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |             ComPtr<ID2D1Bitmap> output; | 
					
						
							|  |  |  |             hr = bitmap_rt->GetBitmap(&output); | 
					
						
							|  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 NativePtr::Set(texture, output); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-02-10 14:41:19 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         return ptr; | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RendererImpl::Resize(uint32_t width, uint32_t height) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     HRESULT hr = S_OK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!d3d_res_) | 
					
						
							|  |  |  |         hr = E_UNEXPECTED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 17:08:15 +08:00
										 |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Clear resources
 | 
					
						
							|  |  |  |         d2d_res_->SetTargetBitmap(nullptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         output_size_.x = static_cast<float>(width); | 
					
						
							|  |  |  |         output_size_.y = static_cast<float>(height); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         hr = d3d_res_->SetLogicalSize(output_size_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-10 13:47:00 +08:00
										 |  |  |         hr = d2d_res_->SetLogicalSize(output_size_.x, output_size_.y); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         render_ctx_->Resize(output_size_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     KGE_THROW_IF_FAILED(hr, "Resize render target failed"); | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace kiwano
 |