pref: refactoring native object

This commit is contained in:
Nomango 2023-09-24 21:43:06 +08:00
parent 52cbf0d1a6
commit 03c24091c7
26 changed files with 295 additions and 305 deletions

View File

@ -82,6 +82,7 @@
<ClInclude Include="..\..\src\kiwano\platform\FileSystem.h" /> <ClInclude Include="..\..\src\kiwano\platform\FileSystem.h" />
<ClInclude Include="..\..\src\kiwano\platform\Input.h" /> <ClInclude Include="..\..\src\kiwano\platform\Input.h" />
<ClInclude Include="..\..\src\kiwano\platform\Keys.h" /> <ClInclude Include="..\..\src\kiwano\platform\Keys.h" />
<ClInclude Include="..\..\src\kiwano\platform\NativeObject.hpp" />
<ClInclude Include="..\..\src\kiwano\platform\Runner.h" /> <ClInclude Include="..\..\src\kiwano\platform\Runner.h" />
<ClInclude Include="..\..\src\kiwano\platform\win32\ComPtr.hpp" /> <ClInclude Include="..\..\src\kiwano\platform\win32\ComPtr.hpp" />
<ClInclude Include="..\..\src\kiwano\platform\win32\libraries.h" /> <ClInclude Include="..\..\src\kiwano\platform\win32\libraries.h" />
@ -100,7 +101,6 @@
<ClInclude Include="..\..\src\kiwano\render\DirectX\RendererImpl.h" /> <ClInclude Include="..\..\src\kiwano\render\DirectX\RendererImpl.h" />
<ClInclude Include="..\..\src\kiwano\render\DirectX\TextRenderer.h" /> <ClInclude Include="..\..\src\kiwano\render\DirectX\TextRenderer.h" />
<ClInclude Include="..\..\src\kiwano\render\Font.h" /> <ClInclude Include="..\..\src\kiwano\render\Font.h" />
<ClInclude Include="..\..\src\kiwano\render\NativeObject.h" />
<ClInclude Include="..\..\src\kiwano\render\Shape.h" /> <ClInclude Include="..\..\src\kiwano\render\Shape.h" />
<ClInclude Include="..\..\src\kiwano\render\ShapeMaker.h" /> <ClInclude Include="..\..\src\kiwano\render\ShapeMaker.h" />
<ClInclude Include="..\..\src\kiwano\render\GifImage.h" /> <ClInclude Include="..\..\src\kiwano\render\GifImage.h" />
@ -192,7 +192,6 @@
<ClCompile Include="..\..\src\kiwano\render\DirectX\RendererImpl.cpp" /> <ClCompile Include="..\..\src\kiwano\render\DirectX\RendererImpl.cpp" />
<ClCompile Include="..\..\src\kiwano\render\DirectX\TextRenderer.cpp" /> <ClCompile Include="..\..\src\kiwano\render\DirectX\TextRenderer.cpp" />
<ClCompile Include="..\..\src\kiwano\render\Font.cpp" /> <ClCompile Include="..\..\src\kiwano\render\Font.cpp" />
<ClCompile Include="..\..\src\kiwano\render\NativeObject.cpp" />
<ClCompile Include="..\..\src\kiwano\render\Shape.cpp" /> <ClCompile Include="..\..\src\kiwano\render\Shape.cpp" />
<ClCompile Include="..\..\src\kiwano\render\ShapeMaker.cpp" /> <ClCompile Include="..\..\src\kiwano\render\ShapeMaker.cpp" />
<ClCompile Include="..\..\src\kiwano\render\GifImage.cpp" /> <ClCompile Include="..\..\src\kiwano\render\GifImage.cpp" />

View File

@ -405,8 +405,8 @@
<ClInclude Include="..\..\src\kiwano\render\DirectX\TextDrawingEffect.h"> <ClInclude Include="..\..\src\kiwano\render\DirectX\TextDrawingEffect.h">
<Filter>render\DirectX</Filter> <Filter>render\DirectX</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\kiwano\render\NativeObject.h"> <ClInclude Include="..\..\src\kiwano\platform\NativeObject.hpp">
<Filter>render</Filter> <Filter>platform</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -671,9 +671,6 @@
<ClCompile Include="..\..\src\kiwano\render\DirectX\TextDrawingEffect.cpp"> <ClCompile Include="..\..\src\kiwano\render\DirectX\TextDrawingEffect.cpp">
<Filter>render\DirectX</Filter> <Filter>render\DirectX</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\kiwano\render\NativeObject.cpp">
<Filter>render</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="suppress_warning.ruleset" /> <None Include="suppress_warning.ruleset" />

View File

@ -208,12 +208,12 @@ bool Sound::IsPlaying() const
if (!voice_) if (!voice_)
return false; return false;
if (!playing_)
return false;
XAUDIO2_VOICE_STATE state; XAUDIO2_VOICE_STATE state;
voice_->GetState(&state); voice_->GetState(&state);
uint32_t buffers_queued = state.BuffersQueued; return !!state.BuffersQueued;
if (buffers_queued && playing_)
return true;
} }
return false; return false;
} }
@ -231,7 +231,7 @@ void Sound::SetVolume(float volume)
{ {
KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL"); KGE_ASSERT(voice_ != nullptr && "IXAudio2SourceVoice* is NULL");
volume = std::min(std::max(volume, -224.f), 224.f); volume = std::min(std::max(volume, -XAUDIO2_MAX_VOLUME_LEVEL), XAUDIO2_MAX_VOLUME_LEVEL);
voice_->SetVolume(volume); voice_->SetVolume(volume);
} }
} // namespace audio } // namespace audio

View File

@ -22,7 +22,6 @@
#include <kiwano-audio/Transcoder.h> #include <kiwano-audio/Transcoder.h>
#include <kiwano/core/Resource.h> #include <kiwano/core/Resource.h>
#include <kiwano/base/ObjectBase.h> #include <kiwano/base/ObjectBase.h>
#include <kiwano/platform/win32/ComPtr.hpp>
#include <xaudio2.h> #include <xaudio2.h>
namespace kiwano namespace kiwano

View File

@ -0,0 +1,89 @@
// 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.
#pragma once
#include <kiwano/base/ObjectBase.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(NativeObject);
/**
* \~chinese
* @brief º¬Óб¾µØÖ¸ÕëµÄÔÏó
*/
class KGE_API NativeObject : public ObjectBase
{
public:
NativeObject() = default;
const Any& GetNative() const;
template <class _Ty>
_Ty GetNative() const;
template<class _Ty>
_Ty* GetNativePtr() const;
void SetNative(const Any& native);
void ResetNative();
protected:
Any native_;
};
inline const Any& NativeObject::GetNative() const
{
return native_;
}
template <class _Ty>
inline _Ty NativeObject::GetNative() const
{
if (native_.HasValue())
{
return native_.Cast<_Ty>();
}
return nullptr;
}
template <class _Ty>
inline _Ty* NativeObject::GetNativePtr() const
{
if (native_.HasValue())
{
return native_.Cast<_Ty*>();
}
return nullptr;
}
inline void NativeObject::SetNative(const Any& native)
{
native_ = native;
}
inline void NativeObject::ResetNative()
{
native_.Clear();
}
} // namespace kiwano

View File

@ -22,11 +22,12 @@
#include <type_traits> #include <type_traits>
#include <kiwano/core/Common.h> #include <kiwano/core/Common.h>
#include <kiwano/core/RefBasePtr.hpp> #include <kiwano/core/RefBasePtr.hpp>
#include <kiwano/platform/NativeObject.hpp>
#include <Unknwnbase.h> #include <Unknwnbase.h>
namespace kiwano namespace kiwano
{ {
struct ComPtrPolicy struct ComRefPolicy
{ {
inline void Retain(IUnknown* ptr) inline void Retain(IUnknown* ptr)
{ {
@ -43,6 +44,74 @@ struct ComPtrPolicy
// ComPtr<> is a smart pointer for COM // ComPtr<> is a smart pointer for COM
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type> template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
using ComPtr = RefBasePtr<_Ty, ComPtrPolicy>; using ComPtr = RefBasePtr<_Ty, ComRefPolicy>;
struct ComPolicy
{
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
static inline ComPtr<_Ty> Get(const NativeObject* object)
{
if (object)
{
const auto& native = object->GetNative();
if (native.HasValue())
{
auto ptr = native.Cast<ComPtr<IUnknown>>();
if (ptr)
{
ComPtr<_Ty> native;
if (SUCCEEDED(ptr->QueryInterface<_Ty>(&native)))
return native;
}
}
}
return nullptr;
}
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
static inline ComPtr<_Ty> Get(const NativeObject& object)
{
return ComPolicy::Get<_Ty>(&object);
}
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
static inline ComPtr<_Ty> Get(NativeObjectPtr object)
{
return ComPolicy::Get<_Ty>(object.Get());
}
static inline void Set(NativeObject* object, ComPtr<IUnknown> com_ptr)
{
if (object)
{
object->SetNative(Any{ com_ptr });
}
}
static inline void Set(NativeObject* object, IUnknown* com_ptr)
{
ComPolicy::Set(object, ComPtr<IUnknown>(com_ptr));
}
static inline void Set(NativeObject& object, IUnknown* com_ptr)
{
ComPolicy::Set(&object, com_ptr);
}
static inline void Set(NativeObjectPtr object, IUnknown* com_ptr)
{
ComPolicy::Set(object.Get(), com_ptr);
}
static inline void Set(NativeObject& object, ComPtr<IUnknown> com_ptr)
{
ComPolicy::Set(&object, com_ptr.Get());
}
static inline void Set(NativeObjectPtr object, ComPtr<IUnknown> com_ptr)
{
ComPolicy::Set(object.Get(), com_ptr.Get());
}
};
} // namespace kiwano } // namespace kiwano

View File

@ -22,6 +22,10 @@
#include <kiwano/render/Brush.h> #include <kiwano/render/Brush.h>
#include <kiwano/render/Renderer.h> #include <kiwano/render/Renderer.h>
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
#endif
namespace kiwano namespace kiwano
{ {
GradientStop::GradientStop() GradientStop::GradientStop()
@ -120,7 +124,7 @@ void Brush::SetTransform(const Transform& transform)
void Brush::SetTransform(const Matrix3x2& transform) void Brush::SetTransform(const Matrix3x2& transform)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1Brush>(this); auto native = ComPolicy::Get<ID2D1Brush>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)

View File

@ -19,7 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
#include <kiwano/render/Color.h> #include <kiwano/render/Color.h>
#include <kiwano/render/Texture.h> #include <kiwano/render/Texture.h>

View File

@ -21,7 +21,6 @@
#pragma once #pragma once
#include <kiwano/core/BinaryData.h> #include <kiwano/core/BinaryData.h>
#include <kiwano/render/DirectX/helper.h> #include <kiwano/render/DirectX/helper.h>
#include <dwrite.h>
namespace kiwano namespace kiwano
{ {

View File

@ -62,7 +62,7 @@ HRESULT RenderContextImpl::CreateDeviceResources(ComPtr<ID2D1Factory> factory, C
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(this, ctx); ComPolicy::Set(this, ctx);
} }
return hr; return hr;
} }
@ -73,7 +73,7 @@ void RenderContextImpl::DiscardDeviceResources()
render_ctx_.Reset(); render_ctx_.Reset();
current_brush_.Reset(); current_brush_.Reset();
ResetNativePointer(); ComPolicy::Set(this, nullptr);
} }
TexturePtr RenderContextImpl::GetTarget() const TexturePtr RenderContextImpl::GetTarget() const
@ -85,7 +85,7 @@ TexturePtr RenderContextImpl::GetTarget() const
if (target) if (target)
{ {
TexturePtr ptr = MakePtr<Texture>(); TexturePtr ptr = MakePtr<Texture>();
NativeObject::Set(*ptr, target.Get()); ComPolicy::Set(*ptr, target.Get());
} }
return nullptr; return nullptr;
} }
@ -123,7 +123,7 @@ void RenderContextImpl::CreateTexture(Texture& texture, const PixelSize& size)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(texture, saved_bitmap); ComPolicy::Set(texture, saved_bitmap);
} }
KGE_THROW_IF_FAILED(hr, "Create texture failed"); KGE_THROW_IF_FAILED(hr, "Create texture failed");
@ -145,7 +145,7 @@ void RenderContextImpl::DrawTexture(const Texture& texture, const Rect* src_rect
mode = D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR; mode = D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
} }
auto bitmap = NativeObject::Get<ID2D1Bitmap>(texture); auto bitmap = ComPolicy::Get<ID2D1Bitmap>(texture);
render_ctx_->DrawBitmap(bitmap.Get(), dest_rect ? &DX::ConvertToRectF(*dest_rect) : nullptr, brush_opacity_, render_ctx_->DrawBitmap(bitmap.Get(), dest_rect ? &DX::ConvertToRectF(*dest_rect) : nullptr, brush_opacity_,
mode, src_rect ? &DX::ConvertToRectF(*src_rect) : nullptr); mode, src_rect ? &DX::ConvertToRectF(*src_rect) : nullptr);
@ -159,10 +159,10 @@ void RenderContextImpl::DrawTextLayout(const TextLayout& layout, const Point& of
if (layout.IsValid()) if (layout.IsValid())
{ {
auto native = NativeObject::Get<IDWriteTextLayout>(layout); auto native = ComPolicy::Get<IDWriteTextLayout>(layout);
auto fill_brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto fill_brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto outline_brush = NativeObject::Get<ID2D1Brush>(current_outline_brush); auto outline_brush = ComPolicy::Get<ID2D1Brush>(current_outline_brush);
auto outline_stroke = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto outline_stroke = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float outline_width = 1.0f; float outline_width = 1.0f;
if (fill_brush) if (fill_brush)
@ -201,9 +201,9 @@ void RenderContextImpl::DrawShape(const Shape& shape)
if (shape.IsValid()) if (shape.IsValid())
{ {
auto geometry = NativeObject::Get<ID2D1Geometry>(shape); auto geometry = ComPolicy::Get<ID2D1Geometry>(shape);
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto stroke_style = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto stroke_style = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f;
render_ctx_->DrawGeometry(geometry.Get(), brush.Get(), stroke_width, stroke_style.Get()); render_ctx_->DrawGeometry(geometry.Get(), brush.Get(), stroke_width, stroke_style.Get());
@ -217,8 +217,8 @@ void RenderContextImpl::DrawLine(const Point& point1, const Point& point2)
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto stroke_style = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto stroke_style = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f;
render_ctx_->DrawLine(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), brush.Get(), stroke_width, render_ctx_->DrawLine(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), brush.Get(), stroke_width,
@ -232,8 +232,8 @@ void RenderContextImpl::DrawRectangle(const Rect& rect)
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto stroke_style = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto stroke_style = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f;
render_ctx_->DrawRectangle(DX::ConvertToRectF(rect), brush.Get(), stroke_width, stroke_style.Get()); render_ctx_->DrawRectangle(DX::ConvertToRectF(rect), brush.Get(), stroke_width, stroke_style.Get());
@ -246,8 +246,8 @@ void RenderContextImpl::DrawRoundedRectangle(const Rect& rect, const Vec2& radiu
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto stroke_style = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto stroke_style = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f;
render_ctx_->DrawRoundedRectangle(D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), brush.Get(), render_ctx_->DrawRoundedRectangle(D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), brush.Get(),
@ -261,8 +261,8 @@ void RenderContextImpl::DrawEllipse(const Point& center, const Vec2& radius)
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto stroke_style = NativeObject::Get<ID2D1StrokeStyle>(current_stroke_); auto stroke_style = ComPolicy::Get<ID2D1StrokeStyle>(current_stroke_);
float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f;
render_ctx_->DrawEllipse(D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), brush.Get(), render_ctx_->DrawEllipse(D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), brush.Get(),
@ -278,8 +278,8 @@ void RenderContextImpl::FillShape(const Shape& shape)
if (shape.IsValid()) if (shape.IsValid())
{ {
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
auto geometry = NativeObject::Get<ID2D1Geometry>(shape); auto geometry = ComPolicy::Get<ID2D1Geometry>(shape);
render_ctx_->FillGeometry(geometry.Get(), brush.Get()); render_ctx_->FillGeometry(geometry.Get(), brush.Get());
IncreasePrimitivesCount(); IncreasePrimitivesCount();
@ -291,7 +291,7 @@ void RenderContextImpl::FillRectangle(const Rect& rect)
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
render_ctx_->FillRectangle(DX::ConvertToRectF(rect), brush.Get()); render_ctx_->FillRectangle(DX::ConvertToRectF(rect), brush.Get());
IncreasePrimitivesCount(); IncreasePrimitivesCount();
@ -302,7 +302,7 @@ void RenderContextImpl::FillRoundedRectangle(const Rect& rect, const Vec2& radiu
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
render_ctx_->FillRoundedRectangle(D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), brush.Get()); render_ctx_->FillRoundedRectangle(D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), brush.Get());
IncreasePrimitivesCount(); IncreasePrimitivesCount();
@ -313,7 +313,7 @@ void RenderContextImpl::FillEllipse(const Point& center, const Vec2& radius)
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!"); KGE_ASSERT(current_brush_ && "The brush used for rendering has not been set!");
auto brush = NativeObject::Get<ID2D1Brush>(current_brush_); auto brush = ComPolicy::Get<ID2D1Brush>(current_brush_);
render_ctx_->FillEllipse(D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), brush.Get()); render_ctx_->FillEllipse(D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), brush.Get());
IncreasePrimitivesCount(); IncreasePrimitivesCount();
@ -345,8 +345,8 @@ void RenderContextImpl::PushLayer(Layer& layer)
{ {
KGE_ASSERT(render_ctx_ && "Render target has not been initialized!"); KGE_ASSERT(render_ctx_ && "Render target has not been initialized!");
auto native = NativeObject::Get<ID2D1Layer>(layer); auto native = ComPolicy::Get<ID2D1Layer>(layer);
auto mask = NativeObject::Get<ID2D1Geometry>(layer.GetMaskShape()); auto mask = ComPolicy::Get<ID2D1Geometry>(layer.GetMaskShape());
if (!native) if (!native)
{ {
@ -354,7 +354,7 @@ void RenderContextImpl::PushLayer(Layer& layer)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(layer, native); ComPolicy::Set(layer, native);
} }
KGE_THROW_IF_FAILED(hr, "Create ID2D1Layer failed"); KGE_THROW_IF_FAILED(hr, "Create ID2D1Layer failed");
} }
@ -400,7 +400,7 @@ void RenderContextImpl::SetCurrentBrush(BrushPtr brush)
if (current_brush_ && current_brush_->IsValid()) if (current_brush_ && current_brush_->IsValid())
{ {
NativeObject::Get<ID2D1Brush>(current_brush_)->SetOpacity(brush_opacity_); ComPolicy::Get<ID2D1Brush>(current_brush_)->SetOpacity(brush_opacity_);
} }
} }

View File

@ -20,7 +20,7 @@
#include <kiwano/utils/Logger.h> #include <kiwano/utils/Logger.h>
#include <kiwano/event/Events.h> #include <kiwano/event/Events.h>
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
#include <kiwano/platform/FileSystem.h> #include <kiwano/platform/FileSystem.h>
#include <kiwano/platform/Application.h> #include <kiwano/platform/Application.h>
#include <kiwano/render/ShapeMaker.h> #include <kiwano/render/ShapeMaker.h>
@ -258,7 +258,7 @@ void RendererImpl::CreateTexture(Texture& texture, const String& file_path)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(texture, bitmap); ComPolicy::Set(texture, bitmap);
texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height });
texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height });
@ -307,7 +307,7 @@ void RendererImpl::CreateTexture(Texture& texture, const BinaryData& data)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(texture, bitmap); ComPolicy::Set(texture, bitmap);
texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height });
texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height });
@ -344,7 +344,7 @@ void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const
D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET, D2D1::PixelFormat(dxgi_format)), &output); D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET, D2D1::PixelFormat(dxgi_format)), &output);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(texture, output); ComPolicy::Set(texture, output);
texture.SetSize({ output->GetSize().width, output->GetSize().height }); texture.SetSize({ output->GetSize().width, output->GetSize().height });
texture.SetSizeInPixels({ output->GetPixelSize().width, output->GetPixelSize().height }); texture.SetSizeInPixels({ output->GetPixelSize().width, output->GetPixelSize().height });
@ -380,7 +380,7 @@ void RendererImpl::CreateGifImage(GifImage& gif, const String& file_path)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(gif, decoder); ComPolicy::Set(gif, decoder);
} }
} }
@ -406,7 +406,7 @@ void RendererImpl::CreateGifImage(GifImage& gif, const BinaryData& data)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(gif, decoder); ComPolicy::Set(gif, decoder);
} }
} }
} }
@ -422,7 +422,7 @@ void RendererImpl::CreateGifImageFrame(GifImage::Frame& frame, const GifImage& g
hr = E_UNEXPECTED; hr = E_UNEXPECTED;
} }
auto decoder = NativeObject::Get<IWICBitmapDecoder>(gif); auto decoder = ComPolicy::Get<IWICBitmapDecoder>(gif);
if (!decoder) if (!decoder)
{ {
@ -449,7 +449,7 @@ void RendererImpl::CreateGifImageFrame(GifImage::Frame& frame, const GifImage& g
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
frame.texture = MakePtr<Texture>(); frame.texture = MakePtr<Texture>();
NativeObject::Set(frame.texture, bitmap); ComPolicy::Set(frame.texture, bitmap);
frame.texture->SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); frame.texture->SetSize({ bitmap->GetSize().width, bitmap->GetSize().height });
frame.texture->SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height }); frame.texture->SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height });
@ -603,7 +603,7 @@ void RendererImpl::CreateFontCollection(Font& font, Vector<String>& family_names
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
d2d_res_->GetFontFamilyNames(family_names, font_collection); // ignore the result d2d_res_->GetFontFamilyNames(family_names, font_collection); // ignore the result
NativeObject::Set(font, font_collection); ComPolicy::Set(font, font_collection);
} }
} }
@ -626,7 +626,7 @@ void RendererImpl::CreateFontCollection(Font& font, Vector<String>& family_names
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
d2d_res_->GetFontFamilyNames(family_names, font_collection); // ignore the result d2d_res_->GetFontFamilyNames(family_names, font_collection); // ignore the result
NativeObject::Set(font, font_collection); ComPolicy::Set(font, font_collection);
} }
} }
@ -660,7 +660,7 @@ void RendererImpl::CreateTextLayout(TextLayout& layout, const String& content, c
auto font_weight = DWRITE_FONT_WEIGHT(font->GetWeight()); auto font_weight = DWRITE_FONT_WEIGHT(font->GetWeight());
auto font_style = DWRITE_FONT_STYLE(font->GetPosture()); auto font_style = DWRITE_FONT_STYLE(font->GetPosture());
auto font_stretch = DWRITE_FONT_STRETCH(font->GetStretch()); auto font_stretch = DWRITE_FONT_STRETCH(font->GetStretch());
auto collection = NativeObject::Get<IDWriteFontCollection>(font); auto collection = ComPolicy::Get<IDWriteFontCollection>(font);
WideString font_family; WideString font_family;
@ -683,7 +683,7 @@ void RendererImpl::CreateTextLayout(TextLayout& layout, const String& content, c
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(layout, output); ComPolicy::Set(layout, output);
layout.SetDirtyFlag(TextLayout::DirtyFlag::Dirty); layout.SetDirtyFlag(TextLayout::DirtyFlag::Dirty);
} }
} }
@ -720,7 +720,7 @@ void RendererImpl::CreateLineShape(Shape& shape, const Point& begin_pos, const P
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(shape, path_geo); ComPolicy::Set(shape, path_geo);
} }
} }
} }
@ -744,7 +744,7 @@ void RendererImpl::CreateRectShape(Shape& shape, const Rect& rect)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(shape, output); ComPolicy::Set(shape, output);
} }
KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RectangleGeometry failed"); KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RectangleGeometry failed");
@ -767,7 +767,7 @@ void RendererImpl::CreateRoundedRectShape(Shape& shape, const Rect& rect, const
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(shape, output); ComPolicy::Set(shape, output);
} }
KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RoundedRectangleGeometry failed"); KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1RoundedRectangleGeometry failed");
@ -790,7 +790,7 @@ void RendererImpl::CreateEllipseShape(Shape& shape, const Point& center, const V
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(shape, output); ComPolicy::Set(shape, output);
} }
KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1EllipseGeometry failed"); KGE_SET_STATUS_IF_FAILED(hr, shape, "Create ID2D1EllipseGeometry failed");
@ -813,7 +813,7 @@ void RendererImpl::CreateShapeSink(ShapeMaker& maker)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
ShapePtr shape = MakePtr<Shape>(); ShapePtr shape = MakePtr<Shape>();
NativeObject::Set(shape, geometry); ComPolicy::Set(shape, geometry);
maker.SetShape(shape); maker.SetShape(shape);
} }
@ -835,7 +835,7 @@ void RendererImpl::CreateBrush(Brush& brush, const Color& color)
if (brush.GetType() == Brush::Type::SolidColor && brush.IsValid()) if (brush.GetType() == Brush::Type::SolidColor && brush.IsValid())
{ {
hr = NativeObject::Get<ID2D1Brush>(brush)->QueryInterface(&solid_brush); hr = ComPolicy::Get<ID2D1Brush>(brush)->QueryInterface(&solid_brush);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
solid_brush->SetColor(DX::ConvertToColorF(color)); solid_brush->SetColor(DX::ConvertToColorF(color));
@ -847,7 +847,7 @@ void RendererImpl::CreateBrush(Brush& brush, const Color& color)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(brush, solid_brush); ComPolicy::Set(brush, solid_brush);
} }
} }
} }
@ -879,7 +879,7 @@ void RendererImpl::CreateBrush(Brush& brush, const LinearGradientStyle& style)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(brush, output); ComPolicy::Set(brush, output);
} }
} }
} }
@ -912,7 +912,7 @@ void RendererImpl::CreateBrush(Brush& brush, const RadialGradientStyle& style)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(brush, output); ComPolicy::Set(brush, output);
} }
} }
} }
@ -930,7 +930,7 @@ void RendererImpl::CreateBrush(Brush& brush, TexturePtr texture)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
auto bitmap = NativeObject::Get<ID2D1Bitmap>(texture); auto bitmap = ComPolicy::Get<ID2D1Bitmap>(texture);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -939,7 +939,7 @@ void RendererImpl::CreateBrush(Brush& brush, TexturePtr texture)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(brush, output); ComPolicy::Set(brush, output);
} }
} }
} }
@ -979,7 +979,7 @@ void RendererImpl::CreateStrokeStyle(StrokeStyle& stroke_style)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(stroke_style, output); ComPolicy::Set(stroke_style, output);
} }
} }
@ -1023,7 +1023,7 @@ RenderContextPtr RendererImpl::CreateTextureRenderContext(TexturePtr texture, co
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
render_ctx->SetTarget(output.Get()); render_ctx->SetTarget(output.Get());
NativeObject::Set(texture, output); ComPolicy::Set(texture, output);
texture->SetSize({ output->GetSize().width, output->GetSize().height }); texture->SetSize({ output->GetSize().width, output->GetSize().height });
texture->SetSizeInPixels({ output->GetPixelSize().width, output->GetPixelSize().height }); texture->SetSizeInPixels({ output->GetPixelSize().width, output->GetPixelSize().height });

View File

@ -24,6 +24,7 @@
#include <kiwano/render/Color.h> #include <kiwano/render/Color.h>
#include <kiwano/platform/win32/ComPtr.hpp> #include <kiwano/platform/win32/ComPtr.hpp>
#include <d2d1_3.h> #include <d2d1_3.h>
#include <dwrite.h>
namespace kiwano namespace kiwano
{ {

View File

@ -105,7 +105,7 @@ Font::Font(const String& family_name, float size, uint32_t weight, FontPosture p
FontPtr found = FontCache::GetInstance().GetFontByFamily(family_name); FontPtr found = FontCache::GetInstance().GetFontByFamily(family_name);
if (found) if (found)
{ {
this->ResetNativePointer(found->GetNativePointer()); this->SetNative(found->GetNative());
} }
} }

View File

@ -20,7 +20,7 @@
#pragma once #pragma once
#include <kiwano/core/Resource.h> #include <kiwano/core/Resource.h>
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
namespace kiwano namespace kiwano
{ {

View File

@ -84,7 +84,7 @@ bool GifImage::Load(const String& file_path)
return true; return true;
// Clear data // Clear data
ResetNativePointer(); ResetNative();
Fail("GifImage::Load failed"); Fail("GifImage::Load failed");
} }
return false; return false;
@ -100,7 +100,7 @@ bool GifImage::Load(const Resource& res)
return true; return true;
// Clear data // Clear data
ResetNativePointer(); ResetNative();
Fail("GifImage::Load failed"); Fail("GifImage::Load failed");
} }
return false; return false;
@ -116,13 +116,14 @@ GifImage::Frame GifImage::GetFrame(uint32_t index)
} // namespace kiwano } // namespace kiwano
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
namespace kiwano namespace kiwano
{ {
bool GifImage::GetGlobalMetadata() bool GifImage::GetGlobalMetadata()
{ {
ComPtr<IWICBitmapDecoder> decoder = NativeObject::Get<IWICBitmapDecoder>(this); ComPtr<IWICBitmapDecoder> decoder = ComPolicy::Get<IWICBitmapDecoder>(this);
HRESULT hr = decoder ? S_OK : E_FAIL; HRESULT hr = decoder ? S_OK : E_FAIL;

View File

@ -1,44 +0,0 @@
#include "NativeObject.h"
namespace kiwano
{
NativeObjectBase::NativeObjectBase()
: native_pointer_(nullptr)
{
}
bool NativeObjectBase::IsValid() const
{
return native_pointer_ != nullptr;
}
void NativeObjectBase::ResetNativePointer(void* native_pointer)
{
native_pointer_ = native_pointer;
}
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
NativeObject::~NativeObject()
{
ResetNativePointer();
}
void NativeObject::ResetNativePointer(void* native_pointer)
{
if (native_pointer_)
{
static_cast<IUnknown*>(native_pointer_)->Release();
native_pointer_ = nullptr;
}
if (native_pointer)
{
native_pointer_ = native_pointer;
static_cast<IUnknown*>(native_pointer_)->AddRef();
}
}
#endif
}

View File

@ -1,139 +0,0 @@
// 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.
#pragma once
#include <kiwano/base/ObjectBase.h>
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/D2DDeviceResources.h>
#endif
namespace kiwano
{
KGE_DECLARE_SMART_PTR(NativeObject);
/**
* \addtogroup Render
* @{
*/
/**
* \~chinese
* @brief º¬Óб¾µØÖ¸ÕëµÄÔÏó
*/
class KGE_API NativeObjectBase : public ObjectBase
{
public:
NativeObjectBase();
bool IsValid() const override;
void* GetNativePointer() const;
template <typename _NativeTy>
_NativeTy* GetNativePointer() const;
virtual void ResetNativePointer(void* native_pointer = nullptr);
protected:
void* native_pointer_;
};
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
class KGE_API NativeObject : public NativeObjectBase
{
public:
virtual ~NativeObject();
void ResetNativePointer(void* native_pointer = nullptr) override;
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
static inline ComPtr<_Ty> Get(const NativeObject* object)
{
if (object)
{
ComPtr<IUnknown> ptr = object->GetNativePointer<IUnknown>();
if (ptr)
{
ComPtr<_Ty> native;
if (SUCCEEDED(ptr->QueryInterface<_Ty>(&native)))
return native;
}
}
return nullptr;
}
template <typename _Ty>
static inline ComPtr<_Ty> Get(const NativeObject& object)
{
return NativeObject::Get<_Ty>(&object);
}
template <typename _Ty>
static inline ComPtr<_Ty> Get(NativeObjectPtr object)
{
return NativeObject::Get<_Ty>(object.Get());
}
static inline void Set(NativeObject* object, ComPtr<IUnknown> com_ptr)
{
if (object)
{
object->ResetNativePointer(com_ptr.Get());
}
}
static inline void Set(NativeObject& object, ComPtr<IUnknown> com_ptr)
{
NativeObject::Set(&object, com_ptr);
}
static inline void Set(NativeObjectPtr object, ComPtr<IUnknown> com_ptr)
{
NativeObject::Set(object.Get(), com_ptr);
}
};
#else
typedef NativeObjectBase NativeObject;
#endif
/** @} */
inline void* NativeObjectBase::GetNativePointer() const
{
return native_pointer_;
}
template <typename _NativeTy>
inline _NativeTy* NativeObjectBase::GetNativePointer() const
{
if (native_pointer_ != nullptr)
{
return static_cast<_NativeTy*>(native_pointer_);
}
return nullptr;
}
} // namespace kiwano

View File

@ -22,6 +22,10 @@
#include <kiwano/render/ShapeMaker.h> #include <kiwano/render/ShapeMaker.h>
#include <kiwano/render/Renderer.h> #include <kiwano/render/Renderer.h>
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
#endif
namespace kiwano namespace kiwano
{ {
@ -29,14 +33,14 @@ Shape::Shape() {}
void Shape::Clear() void Shape::Clear()
{ {
ResetNativePointer(); ResetNative();
} }
Rect Shape::GetBoundingBox() const Rect Shape::GetBoundingBox() const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
Rect bounds; Rect bounds;
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (geometry) if (geometry)
{ {
// no matter it failed or not // no matter it failed or not
@ -52,7 +56,7 @@ Rect Shape::GetBoundingBox(const Matrix3x2& transform) const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
Rect bounds; Rect bounds;
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (geometry) if (geometry)
{ {
// no matter it failed or not // no matter it failed or not
@ -68,7 +72,7 @@ float Shape::GetLength() const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
float length = 0.f; float length = 0.f;
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (geometry) if (geometry)
{ {
// no matter it failed or not // no matter it failed or not
@ -83,7 +87,7 @@ float Shape::GetLength() const
bool Shape::ComputePointAtLength(float length, Point& point, Vec2& tangent) const bool Shape::ComputePointAtLength(float length, Point& point, Vec2& tangent) const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (geometry) if (geometry)
{ {
HRESULT hr = geometry->ComputePointAtLength(length, D2D1::Matrix3x2F::Identity(), DX::ConvertToPoint2F(&point), HRESULT hr = geometry->ComputePointAtLength(length, D2D1::Matrix3x2F::Identity(), DX::ConvertToPoint2F(&point),
@ -100,7 +104,7 @@ bool Shape::ComputePointAtLength(float length, Point& point, Vec2& tangent) cons
float Shape::ComputeArea() const float Shape::ComputeArea() const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (geometry) if (geometry)
{ {
float area = 0.f; float area = 0.f;
@ -116,7 +120,7 @@ float Shape::ComputeArea() const
bool Shape::ContainsPoint(const Point& point, const Matrix3x2* transform) const bool Shape::ContainsPoint(const Point& point, const Matrix3x2* transform) const
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto geometry = NativeObject::Get<ID2D1Geometry>(this); auto geometry = ComPolicy::Get<ID2D1Geometry>(this);
if (!geometry) if (!geometry)
return false; return false;

View File

@ -19,7 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
namespace kiwano namespace kiwano
{ {

View File

@ -21,6 +21,10 @@
#include <kiwano/render/ShapeMaker.h> #include <kiwano/render/ShapeMaker.h>
#include <kiwano/render/Renderer.h> #include <kiwano/render/Renderer.h>
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
#endif
namespace kiwano namespace kiwano
{ {
@ -34,7 +38,7 @@ ShapeMaker::~ShapeMaker()
void ShapeMaker::Clear() void ShapeMaker::Clear()
{ {
CloseStream(); CloseStream();
ResetNativePointer(); ResetNative();
} }
ShapePtr ShapeMaker::GetShape() ShapePtr ShapeMaker::GetShape()
@ -55,7 +59,7 @@ void ShapeMaker::BeginPath(const Point& begin_pos)
} }
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED); native->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
#else #else
// not supported // not supported
@ -67,7 +71,7 @@ void ShapeMaker::EndPath(bool closed)
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->EndFigure(closed ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN); native->EndFigure(closed ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN);
#else #else
// not supported // not supported
@ -81,7 +85,7 @@ void ShapeMaker::AddLine(const Point& point)
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->AddLine(DX::ConvertToPoint2F(point)); native->AddLine(DX::ConvertToPoint2F(point));
#else #else
// not supported // not supported
@ -93,7 +97,7 @@ void ShapeMaker::AddLines(const Vector<Point>& points)
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->AddLines(reinterpret_cast<const D2D_POINT_2F*>(&points[0]), static_cast<uint32_t>(points.size())); native->AddLines(reinterpret_cast<const D2D_POINT_2F*>(&points[0]), static_cast<uint32_t>(points.size()));
#else #else
// not supported // not supported
@ -105,7 +109,7 @@ void kiwano::ShapeMaker::AddLines(const Point* points, size_t count)
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->AddLines(reinterpret_cast<const D2D_POINT_2F*>(points), UINT32(count)); native->AddLines(reinterpret_cast<const D2D_POINT_2F*>(points), UINT32(count));
#else #else
// not supported // not supported
@ -117,7 +121,7 @@ void ShapeMaker::AddBezier(const Point& point1, const Point& point2, const Point
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->AddBezier( native->AddBezier(
D2D1::BezierSegment(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), DX::ConvertToPoint2F(point3))); D2D1::BezierSegment(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), DX::ConvertToPoint2F(point3)));
#else #else
@ -130,7 +134,7 @@ void ShapeMaker::AddArc(const Point& point, const Size& radius, float rotation,
KGE_ASSERT(IsStreamOpened()); KGE_ASSERT(IsStreamOpened());
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
native->AddArc(D2D1::ArcSegment(DX::ConvertToPoint2F(point), DX::ConvertToSizeF(radius), rotation, native->AddArc(D2D1::ArcSegment(DX::ConvertToPoint2F(point), DX::ConvertToSizeF(radius), rotation,
clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE, clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE)); is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE));
@ -147,9 +151,9 @@ ShapePtr ShapeMaker::Combine(ShapePtr shape_a, ShapePtr shape_b, CombineMode mod
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
if (shape_a && shape_b) if (shape_a && shape_b)
{ {
auto geo_a = NativeObject::Get<ID2D1Geometry>(shape_a); auto geo_a = ComPolicy::Get<ID2D1Geometry>(shape_a);
auto geo_b = NativeObject::Get<ID2D1Geometry>(shape_b); auto geo_b = ComPolicy::Get<ID2D1Geometry>(shape_b);
auto native = NativeObject::Get<ID2D1GeometrySink>(maker); auto native = ComPolicy::Get<ID2D1GeometrySink>(maker);
HRESULT hr = geo_a->CombineWithGeometry(geo_b.Get(), D2D1_COMBINE_MODE(mode), DX::ConvertToMatrix3x2F(matrix), HRESULT hr = geo_a->CombineWithGeometry(geo_b.Get(), D2D1_COMBINE_MODE(mode), DX::ConvertToMatrix3x2F(matrix),
native.Get()); native.Get());
@ -172,7 +176,7 @@ void ShapeMaker::OpenStream()
Renderer::GetInstance().CreateShapeSink(*this); Renderer::GetInstance().CreateShapeSink(*this);
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto geometry = NativeObject::Get<ID2D1PathGeometry>(shape_); auto geometry = ComPolicy::Get<ID2D1PathGeometry>(shape_);
if (geometry) if (geometry)
{ {
ComPtr<ID2D1GeometrySink> native; ComPtr<ID2D1GeometrySink> native;
@ -180,7 +184,7 @@ void ShapeMaker::OpenStream()
HRESULT hr = geometry->Open(&native); HRESULT hr = geometry->Open(&native);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
NativeObject::Set(this, native); ComPolicy::Set(this, native);
} }
KGE_THROW_IF_FAILED(hr, "ID2D1PathGeometry::Open failed"); KGE_THROW_IF_FAILED(hr, "ID2D1PathGeometry::Open failed");
} }
@ -195,12 +199,12 @@ void ShapeMaker::CloseStream()
return; return;
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<ID2D1GeometrySink>(this); auto native = ComPolicy::Get<ID2D1GeometrySink>(this);
HRESULT hr = native->Close(); HRESULT hr = native->Close();
KGE_THROW_IF_FAILED(hr, "ID2D1PathGeometry::Close failed"); KGE_THROW_IF_FAILED(hr, "ID2D1PathGeometry::Close failed");
ResetNativePointer(); ResetNative();
#else #else
return; // not supported return; // not supported
#endif #endif

View File

@ -18,7 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#include <kiwano/render/Renderer.h>
#include <kiwano/render/StrokeStyle.h> #include <kiwano/render/StrokeStyle.h>
namespace kiwano namespace kiwano

View File

@ -19,7 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
namespace kiwano namespace kiwano
{ {

View File

@ -21,6 +21,10 @@
#include <kiwano/render/Renderer.h> #include <kiwano/render/Renderer.h>
#include <kiwano/render/TextLayout.h> #include <kiwano/render/TextLayout.h>
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
#endif
namespace kiwano namespace kiwano
{ {
@ -37,8 +41,13 @@ TextLayout::TextLayout(const String& content, const TextStyle& style)
Reset(content, style); Reset(content, style);
} }
void TextLayout::Reset(const String& content, const TextStyle& style) void TextLayout::Clear()
{ {
ResetNative();
}
void TextLayout::Reset(const String& content, const TextStyle& style)
{
content_length_ = (uint32_t)content.length(); content_length_ = (uint32_t)content.length();
if (content_length_) if (content_length_)
{ {
@ -80,7 +89,7 @@ void TextLayout::SetFont(FontPtr font)
return; return;
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>((const NativeObject*)(this));
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -89,7 +98,7 @@ void TextLayout::SetFont(FontPtr font)
// reset font collection // reset font collection
{ {
auto collection = NativeObject::Get<IDWriteFontCollection>(font); auto collection = ComPolicy::Get<IDWriteFontCollection>(font);
hr = native->SetFontCollection(collection.Get(), { 0, content_length_ }); hr = native->SetFontCollection(collection.Get(), { 0, content_length_ });
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontCollection failed"); KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontCollection failed");
@ -144,7 +153,7 @@ void TextLayout::SetFont(FontPtr font)
void TextLayout::SetUnderline(bool enable) void TextLayout::SetUnderline(bool enable)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -162,7 +171,7 @@ void TextLayout::SetUnderline(bool enable)
void TextLayout::SetStrikethrough(bool enable) void TextLayout::SetStrikethrough(bool enable)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -180,7 +189,7 @@ void TextLayout::SetStrikethrough(bool enable)
void TextLayout::SetAlignment(TextAlign align) void TextLayout::SetAlignment(TextAlign align)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -215,7 +224,7 @@ void TextLayout::SetAlignment(TextAlign align)
void TextLayout::SetWrapWidth(float wrap_width) void TextLayout::SetWrapWidth(float wrap_width)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -245,7 +254,7 @@ void TextLayout::SetWrapWidth(float wrap_width)
void TextLayout::SetLineSpacing(float line_spacing) void TextLayout::SetLineSpacing(float line_spacing)
{ {
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native); KGE_ASSERT(native);
if (native) if (native)
@ -280,7 +289,7 @@ bool TextLayout::UpdateIfDirty()
line_count_ = 0; line_count_ = 0;
size_ = Size(); size_ = Size();
auto native = NativeObject::Get<IDWriteTextLayout>(this); auto native = ComPolicy::Get<IDWriteTextLayout>(this);
if (content_length_ == 0 || !native) if (content_length_ == 0 || !native)
return true; return true;

View File

@ -20,7 +20,7 @@
#pragma once #pragma once
#include <kiwano/math/Math.h> #include <kiwano/math/Math.h>
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
#include <kiwano/render/TextStyle.h> #include <kiwano/render/TextStyle.h>
namespace kiwano namespace kiwano
@ -135,11 +135,6 @@ inline bool TextLayout::IsDirty() const
return dirty_flag_ != DirtyFlag::Clean; return dirty_flag_ != DirtyFlag::Clean;
} }
inline void TextLayout::Clear()
{
ResetNativePointer();
}
inline uint32_t TextLayout::GetContentLength() const inline uint32_t TextLayout::GetContentLength() const
{ {
return content_length_; return content_length_;

View File

@ -23,6 +23,10 @@
#include <kiwano/render/TextureCache.h> #include <kiwano/render/TextureCache.h>
#include <functional> // std::hash #include <functional> // std::hash
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>
#endif
namespace kiwano namespace kiwano
{ {
@ -104,8 +108,8 @@ void Texture::CopyFrom(TexturePtr copy_from)
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
if (IsValid() && copy_from) if (IsValid() && copy_from)
{ {
auto native = NativeObject::Get<ID2D1Bitmap>(this); auto native = ComPolicy::Get<ID2D1Bitmap>(this);
auto native_to_copy = NativeObject::Get<ID2D1Bitmap>(copy_from); auto native_to_copy = ComPolicy::Get<ID2D1Bitmap>(copy_from);
HRESULT hr = native->CopyFromBitmap(nullptr, native_to_copy.Get(), nullptr); HRESULT hr = native->CopyFromBitmap(nullptr, native_to_copy.Get(), nullptr);
@ -121,8 +125,8 @@ void Texture::CopyFrom(TexturePtr copy_from, const Rect& src_rect, const Point&
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
if (IsValid() && copy_from) if (IsValid() && copy_from)
{ {
auto native = NativeObject::Get<ID2D1Bitmap>(this); auto native = ComPolicy::Get<ID2D1Bitmap>(this);
auto native_to_copy = NativeObject::Get<ID2D1Bitmap>(copy_from); auto native_to_copy = ComPolicy::Get<ID2D1Bitmap>(copy_from);
HRESULT hr = HRESULT hr =
native->CopyFromBitmap(&D2D1::Point2U(uint32_t(dest_point.x), uint32_t(dest_point.y)), native_to_copy.Get(), native->CopyFromBitmap(&D2D1::Point2U(uint32_t(dest_point.x), uint32_t(dest_point.y)), native_to_copy.Get(),

View File

@ -20,7 +20,7 @@
#pragma once #pragma once
#include <kiwano/core/Resource.h> #include <kiwano/core/Resource.h>
#include <kiwano/render/NativeObject.h> #include <kiwano/platform/NativeObject.hpp>
namespace kiwano namespace kiwano
{ {