minor fixes

This commit is contained in:
Nomango 2019-08-14 08:52:01 +08:00
parent 47be1dc413
commit 90bfda923d
3 changed files with 340 additions and 216 deletions

View File

@ -65,16 +65,12 @@ namespace kiwano
if (in_scene_) if (in_scene_)
{ {
ThrowIfFailed( Renderer::GetInstance()->CreateLayer(in_layer_);
Renderer::GetInstance()->CreateLayer(in_layer_)
);
} }
if (out_scene_) if (out_scene_)
{ {
ThrowIfFailed( Renderer::GetInstance()->CreateLayer(out_layer_);
Renderer::GetInstance()->CreateLayer(out_layer_)
);
} }
window_size_ = Renderer::GetInstance()->GetOutputSize(); window_size_ = Renderer::GetInstance()->GetOutputSize();

View File

@ -211,25 +211,52 @@ namespace kiwano
return hr; return hr;
} }
HRESULT Renderer::CreateLayer(ComPtr<ID2D1Layer>& layer) void Renderer::IncreasePrimitivesCount()
{ {
if (!device_context_) if (collecting_status_)
return E_UNEXPECTED; {
++status_.primitives;
layer = nullptr; }
return device_context_->CreateLayer(&layer);
} }
HRESULT Renderer::DrawGeometry( void Renderer::CreateLayer(ComPtr<ID2D1Layer>& layer)
{
HRESULT hr = S_OK;
ComPtr<ID2D1Layer> new_layer;
if (!device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
hr = device_context_->CreateLayer(&new_layer);
}
if (SUCCEEDED(hr))
{
layer = new_layer;
}
ThrowIfFailed(hr);
}
void Renderer::DrawGeometry(
ComPtr<ID2D1Geometry> const& geometry, ComPtr<ID2D1Geometry> const& geometry,
Color const& stroke_color, Color const& stroke_color,
float stroke_width, float stroke_width,
StrokeStyle stroke StrokeStyle stroke
) )
{ {
HRESULT hr = S_OK;
if (!solid_color_brush_ || !device_context_) if (!solid_color_brush_ || !device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
solid_color_brush_->SetColor(DX::ConvertToColorF(stroke_color)); solid_color_brush_->SetColor(DX::ConvertToColorF(stroke_color));
device_context_->DrawGeometry( device_context_->DrawGeometry(
@ -239,30 +266,42 @@ namespace kiwano
d2d_res_->GetStrokeStyle(stroke) d2d_res_->GetStrokeStyle(stroke)
); );
if (collecting_status_) IncreasePrimitivesCount();
++status_.primitives;
return S_OK;
} }
HRESULT Renderer::FillGeometry(ComPtr<ID2D1Geometry> const & geometry, Color const& fill_color) ThrowIfFailed(hr);
{ }
if (!solid_color_brush_ || !device_context_)
return E_UNEXPECTED;
void Renderer::FillGeometry(ComPtr<ID2D1Geometry> const & geometry, Color const& fill_color)
{
HRESULT hr = S_OK;
if (!solid_color_brush_ || !device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
solid_color_brush_->SetColor(DX::ConvertToColorF(fill_color)); solid_color_brush_->SetColor(DX::ConvertToColorF(fill_color));
device_context_->FillGeometry( device_context_->FillGeometry(
geometry.get(), geometry.get(),
solid_color_brush_.get() solid_color_brush_.get()
); );
return S_OK;
} }
HRESULT Renderer::DrawRectangle(Rect const& rect, const Color& stroke_color, float stroke_width, StrokeStyle stroke) ThrowIfFailed(hr);
{ }
if (!solid_color_brush_ || !device_context_)
return E_UNEXPECTED;
void Renderer::DrawRectangle(Rect const& rect, const Color& stroke_color, float stroke_width, StrokeStyle stroke)
{
HRESULT hr = S_OK;
if (!solid_color_brush_ || !device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
solid_color_brush_->SetColor(DX::ConvertToColorF(stroke_color)); solid_color_brush_->SetColor(DX::ConvertToColorF(stroke_color));
device_context_->DrawRectangle( device_context_->DrawRectangle(
@ -272,33 +311,42 @@ namespace kiwano
d2d_res_->GetStrokeStyle(stroke) d2d_res_->GetStrokeStyle(stroke)
); );
if (collecting_status_) IncreasePrimitivesCount();
++status_.primitives;
return S_OK;
} }
HRESULT Renderer::FillRectangle(Rect const& rect, Color const& fill_color) ThrowIfFailed(hr);
{ }
if (!solid_color_brush_ || !device_context_)
return E_UNEXPECTED;
void Renderer::FillRectangle(Rect const& rect, Color const& fill_color)
{
HRESULT hr = S_OK;
if (!solid_color_brush_ || !device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
solid_color_brush_->SetColor(DX::ConvertToColorF(fill_color)); solid_color_brush_->SetColor(DX::ConvertToColorF(fill_color));
device_context_->FillRectangle( device_context_->FillRectangle(
DX::ConvertToRectF(rect), DX::ConvertToRectF(rect),
solid_color_brush_.get() solid_color_brush_.get()
); );
return S_OK;
} }
HRESULT Renderer::DrawBitmap(ComPtr<ID2D1Bitmap> const & bitmap, Rect const& src_rect, Rect const& dest_rect) ThrowIfFailed(hr);
}
void Renderer::DrawBitmap(ComPtr<ID2D1Bitmap> const & bitmap, Rect const& src_rect, Rect const& dest_rect)
{ {
HRESULT hr = S_OK;
if (!device_context_) if (!device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
if (!bitmap) }
return S_OK;
if (SUCCEEDED(hr) && bitmap)
{
device_context_->DrawBitmap( device_context_->DrawBitmap(
bitmap.get(), bitmap.get(),
DX::ConvertToRectF(dest_rect), DX::ConvertToRectF(dest_rect),
@ -307,19 +355,28 @@ namespace kiwano
DX::ConvertToRectF(src_rect) DX::ConvertToRectF(src_rect)
); );
if (collecting_status_) IncreasePrimitivesCount();
++status_.primitives;
return S_OK;
} }
HRESULT Renderer::DrawTextLayout(ComPtr<IDWriteTextLayout> const& text_layout) ThrowIfFailed(hr);
{ }
if (!text_renderer_)
return E_UNEXPECTED;
if (collecting_status_) void Renderer::DrawTextLayout(ComPtr<IDWriteTextLayout> const& text_layout)
++status_.primitives; {
return text_layout->Draw(nullptr, text_renderer_.get(), 0, 0); HRESULT hr = S_OK;
if (!text_renderer_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
hr = text_layout->Draw(nullptr, text_renderer_.get(), 0, 0);
IncreasePrimitivesCount();
}
ThrowIfFailed(hr);
} }
void Renderer::SetVSyncEnabled(bool enabled) void Renderer::SetVSyncEnabled(bool enabled)
@ -327,33 +384,52 @@ namespace kiwano
vsync_ = enabled; vsync_ = enabled;
} }
HRESULT Renderer::PushClip(const Matrix & clip_matrix, const Size & clip_size) void Renderer::PushClip(const Matrix & clip_matrix, const Size & clip_size)
{ {
HRESULT hr = S_OK;
if (!device_context_) if (!device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
device_context_->SetTransform(DX::ConvertToMatrix3x2F(clip_matrix)); device_context_->SetTransform(DX::ConvertToMatrix3x2F(clip_matrix));
device_context_->PushAxisAlignedClip( device_context_->PushAxisAlignedClip(
D2D1::RectF(0, 0, clip_size.x, clip_size.y), D2D1::RectF(0, 0, clip_size.x, clip_size.y),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
); );
return S_OK;
} }
HRESULT Renderer::PopClip() ThrowIfFailed(hr);
}
void Renderer::PopClip()
{ {
HRESULT hr = S_OK;
if (!device_context_) if (!device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
device_context_->PopAxisAlignedClip();
return S_OK;
} }
HRESULT Renderer::PushLayer(ComPtr<ID2D1Layer> const& layer, LayerProperties const& properties) if (SUCCEEDED(hr))
{ {
if (!device_context_ || !solid_color_brush_) device_context_->PopAxisAlignedClip();
return E_UNEXPECTED; }
ThrowIfFailed(hr);
}
void Renderer::PushLayer(ComPtr<ID2D1Layer> const& layer, LayerProperties const& properties)
{
HRESULT hr = S_OK;
if (!device_context_ || !solid_color_brush_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
device_context_->PushLayer( device_context_->PushLayer(
D2D1::LayerParameters( D2D1::LayerParameters(
DX::ConvertToRectF(properties.area), DX::ConvertToRectF(properties.area),
@ -366,27 +442,43 @@ namespace kiwano
), ),
layer.get() layer.get()
); );
return S_OK;
} }
HRESULT Renderer::PopLayer() ThrowIfFailed(hr);
}
void Renderer::PopLayer()
{ {
HRESULT hr = S_OK;
if (!device_context_) if (!device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
device_context_->PopLayer();
return S_OK;
} }
HRESULT Renderer::Resize(UINT width, UINT height) if (SUCCEEDED(hr))
{
device_context_->PopLayer();
}
ThrowIfFailed(hr);
}
void Renderer::Resize(UINT width, UINT height)
{
HRESULT hr = S_OK;
if (!d3d_res_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{ {
output_size_.x = static_cast<float>(width); output_size_.x = static_cast<float>(width);
output_size_.y = static_cast<float>(height); output_size_.y = static_cast<float>(height);
if (d3d_res_) hr = d3d_res_->SetLogicalSize(output_size_);
{
return d3d_res_->SetLogicalSize(output_size_);
} }
return S_OK;
ThrowIfFailed(hr);
} }
void Renderer::SetCollectingStatus(bool collecting) void Renderer::SetCollectingStatus(bool collecting)
@ -399,29 +491,43 @@ namespace kiwano
clear_color_ = color; clear_color_ = color;
} }
HRESULT Renderer::SetTransform(const Matrix & matrix) void Renderer::SetTransform(const Matrix & matrix)
{ {
HRESULT hr = S_OK;
if (!device_context_) if (!device_context_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
device_context_->SetTransform(DX::ConvertToMatrix3x2F(&matrix));
return S_OK;
} }
HRESULT Renderer::SetOpacity(float opacity) if (SUCCEEDED(hr))
{ {
if (!solid_color_brush_) device_context_->SetTransform(DX::ConvertToMatrix3x2F(&matrix));
return E_UNEXPECTED; }
ThrowIfFailed(hr);
}
void Renderer::SetOpacity(float opacity)
{
HRESULT hr = S_OK;
if (!solid_color_brush_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
if (opacity_ != opacity) if (opacity_ != opacity)
{ {
opacity_ = opacity; opacity_ = opacity;
solid_color_brush_->SetOpacity(opacity); solid_color_brush_->SetOpacity(opacity);
} }
return S_OK;
} }
HRESULT Renderer::SetTextStyle( ThrowIfFailed(hr);
}
void Renderer::SetTextStyle(
float opacity, float opacity,
Color const& color, Color const& color,
bool has_outline, bool has_outline,
@ -430,9 +536,14 @@ namespace kiwano
StrokeStyle outline_stroke StrokeStyle outline_stroke
) )
{ {
HRESULT hr = S_OK;
if (!text_renderer_ || !d3d_res_) if (!text_renderer_ || !d3d_res_)
return E_UNEXPECTED; {
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
text_renderer_->SetTextStyle( text_renderer_->SetTextStyle(
opacity, opacity,
DX::ConvertToColorF(color), DX::ConvertToColorF(color),
@ -441,26 +552,40 @@ namespace kiwano
outline_width, outline_width,
d2d_res_->GetStrokeStyle(outline_stroke) d2d_res_->GetStrokeStyle(outline_stroke)
); );
return S_OK;
} }
HRESULT Renderer::SetAntialiasMode(bool enabled) ThrowIfFailed(hr);
{ }
if (!device_context_)
return E_UNEXPECTED;
void Renderer::SetAntialiasMode(bool enabled)
{
HRESULT hr = S_OK;
if (!device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
device_context_->SetAntialiasMode( device_context_->SetAntialiasMode(
enabled ? D2D1_ANTIALIAS_MODE_PER_PRIMITIVE : D2D1_ANTIALIAS_MODE_ALIASED enabled ? D2D1_ANTIALIAS_MODE_PER_PRIMITIVE : D2D1_ANTIALIAS_MODE_ALIASED
); );
antialias_ = enabled; antialias_ = enabled;
return S_OK;
} }
HRESULT Renderer::SetTextAntialiasMode(TextAntialias mode) ThrowIfFailed(hr);
{ }
if (!device_context_)
return E_UNEXPECTED;
void Renderer::SetTextAntialiasMode(TextAntialias mode)
{
HRESULT hr = S_OK;
if (!device_context_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
text_antialias_ = mode; text_antialias_ = mode;
D2D1_TEXT_ANTIALIAS_MODE antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE; D2D1_TEXT_ANTIALIAS_MODE antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
switch (text_antialias_) switch (text_antialias_)
@ -481,7 +606,9 @@ namespace kiwano
break; break;
} }
device_context_->SetTextAntialiasMode(antialias_mode); device_context_->SetTextAntialiasMode(antialias_mode);
return S_OK; }
ThrowIfFailed(hr);
} }
bool Renderer::CheckVisibility(Size const& content_size, Matrix const& transform) bool Renderer::CheckVisibility(Size const& content_size, Matrix const& transform)

View File

@ -52,41 +52,41 @@ namespace kiwano
KGE_DECLARE_SINGLETON(Renderer); KGE_DECLARE_SINGLETON(Renderer);
public: public:
HRESULT CreateLayer( void CreateLayer(
ComPtr<ID2D1Layer>& layer ComPtr<ID2D1Layer>& layer
); );
HRESULT DrawGeometry( void DrawGeometry(
ComPtr<ID2D1Geometry> const& geometry, ComPtr<ID2D1Geometry> const& geometry,
const Color& stroke_color, const Color& stroke_color,
float stroke_width, float stroke_width,
StrokeStyle stroke = StrokeStyle::Miter StrokeStyle stroke = StrokeStyle::Miter
); );
HRESULT FillGeometry( void FillGeometry(
ComPtr<ID2D1Geometry> const& geometry, ComPtr<ID2D1Geometry> const& geometry,
Color const& fill_color Color const& fill_color
); );
HRESULT DrawRectangle( void DrawRectangle(
Rect const& rect, Rect const& rect,
const Color& stroke_color, const Color& stroke_color,
float stroke_width, float stroke_width,
StrokeStyle stroke = StrokeStyle::Miter StrokeStyle stroke = StrokeStyle::Miter
); );
HRESULT FillRectangle( void FillRectangle(
Rect const& rect, Rect const& rect,
Color const& fill_color Color const& fill_color
); );
HRESULT DrawBitmap( void DrawBitmap(
ComPtr<ID2D1Bitmap> const& bitmap, ComPtr<ID2D1Bitmap> const& bitmap,
Rect const& src_rect, Rect const& src_rect,
Rect const& dest_rect Rect const& dest_rect
); );
HRESULT DrawTextLayout( void DrawTextLayout(
ComPtr<IDWriteTextLayout> const& text_layout ComPtr<IDWriteTextLayout> const& text_layout
); );
@ -96,12 +96,12 @@ namespace kiwano
); );
// ÉèÖÿ¹¾â³Ýģʽ // ÉèÖÿ¹¾â³Ýģʽ
HRESULT SetAntialiasMode( void SetAntialiasMode(
bool enabled bool enabled
); );
// ÉèÖÃÎÄ×Ö¿¹¾â³Ýģʽ // ÉèÖÃÎÄ×Ö¿¹¾â³Ýģʽ
HRESULT SetTextAntialiasMode( void SetTextAntialiasMode(
TextAntialias mode TextAntialias mode
); );
@ -111,15 +111,15 @@ namespace kiwano
); );
// ÉèÖû­±Ê͸Ã÷¶È // ÉèÖû­±Ê͸Ã÷¶È
HRESULT SetOpacity( void SetOpacity(
float opacity float opacity
); );
HRESULT SetTransform( void SetTransform(
const Matrix& matrix const Matrix& matrix
); );
HRESULT SetTextStyle( void SetTextStyle(
float opacity, float opacity,
const Color& color, const Color& color,
bool has_outline, bool has_outline,
@ -128,21 +128,21 @@ namespace kiwano
StrokeStyle outline_stroke StrokeStyle outline_stroke
); );
HRESULT PushClip( void PushClip(
const Matrix& clip_matrix, const Matrix& clip_matrix,
const Size& clip_size const Size& clip_size
); );
HRESULT PopClip(); void PopClip();
HRESULT PushLayer( void PushLayer(
ComPtr<ID2D1Layer> const& layer, ComPtr<ID2D1Layer> const& layer,
LayerProperties const& properties LayerProperties const& properties
); );
HRESULT PopLayer(); void PopLayer();
HRESULT Resize( void Resize(
UINT width, UINT width,
UINT height UINT height
); );
@ -200,13 +200,14 @@ namespace kiwano
HRESULT EndDraw(); HRESULT EndDraw();
private: void IncreasePrimitivesCount();
HWND hwnd_;
float opacity_;
bool antialias_;
bool vsync_;
bool collecting_status_;
private:
bool vsync_;
bool antialias_;
bool collecting_status_;
float opacity_;
HWND hwnd_;
Size output_size_; Size output_size_;
Color clear_color_; Color clear_color_;
TextAntialias text_antialias_; TextAntialias text_antialias_;