From ea87825d58ab5b58dd360c3f080518c07a865db0 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 19 Apr 2019 23:20:09 +0800 Subject: ... --- src/win/graph/CMakeLists.txt | 2 +- src/win/graph/d2d_painter.cpp | 93 ++++++++++++++++++++++++++++++++++++++ src/win/graph/graph_manager.cpp | 6 ++- src/win/graph/win_font.cpp | 2 +- src/win/graph/win_painter.cpp | 99 ----------------------------------------- 5 files changed, 99 insertions(+), 103 deletions(-) create mode 100644 src/win/graph/d2d_painter.cpp delete mode 100644 src/win/graph/win_painter.cpp (limited to 'src/win/graph') diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt index 7781e09a..61749a6a 100644 --- a/src/win/graph/CMakeLists.txt +++ b/src/win/graph/CMakeLists.txt @@ -1,9 +1,9 @@ add_library(cru_win_graph STATIC + d2d_painter.cpp graph_manager.cpp win_brush.cpp win_font.cpp win_geometry.cpp win_graph_factory.cpp - win_painter.cpp win_text_layout.cpp) target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/win/graph/d2d_painter.cpp b/src/win/graph/d2d_painter.cpp new file mode 100644 index 00000000..26f6bef0 --- /dev/null +++ b/src/win/graph/d2d_painter.cpp @@ -0,0 +1,93 @@ +#include "cru/win/graph/d2d_painter.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include + +namespace cru::win::graph { +D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { + assert(render_target); + render_target_ = render_target; +} + +platform::Matrix D2DPainter::GetTransform() { + assert(!IsDisposed()); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return util::Convert(m); +} + +void D2DPainter::SetTransform(const platform::Matrix& matrix) { + assert(!IsDisposed()); + render_target_->SetTransform(util::Convert(matrix)); +} + +void D2DPainter::Clear(const ui::Color& color) { + assert(!IsDisposed()); + render_target_->Clear(util::Convert(color)); +} + +void D2DPainter::StrokeRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), + width); +} + +void D2DPainter::FillRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); +} + +void D2DPainter::StrokeGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); +} + +void D2DPainter::FillGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); +} + +void D2DPainter::DrawText(const ui::Point& offset, + platform::graph::TextLayout* text_layout, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto t = dynamic_cast(text_layout); + assert(t); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawTextLayout(util::Convert(offset), + t->GetDWriteTextLayout(), b->GetD2DBrush()); +} + +void D2DPainter::EndDraw() { + if (!is_disposed_) { + is_disposed_ = true; + DoEndDraw(); + } +} +} // namespace cru::win::graph diff --git a/src/win/graph/graph_manager.cpp b/src/win/graph/graph_manager.cpp index a4306b1b..30e1a14e 100644 --- a/src/win/graph/graph_manager.cpp +++ b/src/win/graph/graph_manager.cpp @@ -2,10 +2,12 @@ #include "cru/win/exception.hpp" +#include + namespace cru::win::graph { GraphManager* GraphManager::GetInstance() { - static GraphManager instance; - return &instance; + static GraphManager* instance = new GraphManager(); + return instance; } GraphManager::GraphManager() { diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp index 0eb5e6b2..96983d3e 100644 --- a/src/win/graph/win_font.cpp +++ b/src/win/graph/win_font.cpp @@ -13,7 +13,7 @@ WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, float font_size) { assert(graph_manager); std::array buffer; - if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size())) + if (!::GetUserDefaultLocaleName(buffer.data(), static_cast(buffer.size()))) throw Win32Error(::GetLastError(), "Failed to get locale."); ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat( diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp deleted file mode 100644 index 69ce1141..00000000 --- a/src/win/graph/win_painter.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include "cru/win/graph/win_painter.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/d2d_util.hpp" -#include "cru/win/graph/graph_manager.hpp" -#include "cru/win/graph/win_brush.hpp" -#include "cru/win/graph/win_geometry.hpp" -#include "cru/win/graph/win_text_layout.hpp" - -#include - -namespace cru::win::graph { -WinPainter::WinPainter(ID2D1RenderTarget* render_target) { - assert(render_target); - render_target_ = render_target; -} - -WinPainter::~WinPainter() { EndDraw(); } - -platform::Matrix WinPainter::GetTransform() { - assert(!IsDisposed()); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return util::Convert(m); -} - -void WinPainter::SetTransform(const platform::Matrix& matrix) { - assert(!IsDisposed()); - render_target_->SetTransform(util::Convert(matrix)); -} - -void WinPainter::Clear(const ui::Color& color) { - assert(!IsDisposed()); - render_target_->Clear(util::Convert(color)); -} - -void WinPainter::StrokeRectangle(const ui::Rect& rectangle, - platform::graph::Brush* brush, float width) { - assert(!IsDisposed()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), - width); -} - -void WinPainter::FillRectangle(const ui::Rect& rectangle, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); -} - -void WinPainter::StrokeGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush, float width) { - assert(!IsDisposed()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); -} - -void WinPainter::FillGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); -} - -void WinPainter::DrawText(const ui::Point& offset, - platform::graph::TextLayout* text_layout, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto t = dynamic_cast(text_layout); - assert(t); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawTextLayout(util::Convert(offset), - t->GetDWriteTextLayout(), b->GetD2DBrush()); -} - -void WinPainter::EndDraw() { - if (!IsDisposed()) { - DoEndDraw(); - } -} - -void WinPainter::DoEndDraw() { - ThrowIfFailed(render_target_->EndDraw()); - is_disposed_ = true; -} -} // namespace cru::win::graph -- cgit v1.2.3