aboutsummaryrefslogtreecommitdiff
path: root/src/win/graphics/direct/Geometry.cpp
blob: 2a54485976222eebbc71fd765afd41a5a23aafdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "cru/win/graphics/direct/Geometry.hpp"

#include "cru/win/graphics/direct/ConvertUtil.hpp"
#include "cru/win/graphics/direct/Exception.hpp"
#include "cru/win/graphics/direct/Factory.hpp"

namespace cru::platform::graphics::win::direct {
D2DGeometryBuilder::D2DGeometryBuilder(DirectGraphicsFactory* factory)
    : DirectGraphicsResource(factory) {
  ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_));
  ThrowIfFailed(geometry_->Open(&geometry_sink_));
}

void D2DGeometryBuilder::CheckValidation() {
  if (!IsValid())
    throw ReuseException(L"The geometry builder is already disposed.");
}

void D2DGeometryBuilder::BeginFigure(const Point& point) {
  CheckValidation();
  geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED);
}

void D2DGeometryBuilder::LineTo(const Point& point) {
  CheckValidation();
  geometry_sink_->AddLine(Convert(point));
}

void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point,
                                           const Point& end_point) {
  CheckValidation();
  geometry_sink_->AddQuadraticBezier(
      D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point)));
}

void D2DGeometryBuilder::CloseFigure(bool close) {
  CheckValidation();
  geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED
                                  : D2D1_FIGURE_END_OPEN);
}

std::unique_ptr<IGeometry> D2DGeometryBuilder::Build() {
  CheckValidation();
  ThrowIfFailed(geometry_sink_->Close());
  geometry_sink_ = nullptr;
  auto geometry =
      std::make_unique<D2DGeometry>(GetDirectFactory(), std::move(geometry_));
  geometry_ = nullptr;
  return geometry;
}

D2DGeometry::D2DGeometry(DirectGraphicsFactory* factory,
                         Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry)
    : DirectGraphicsResource(factory), geometry_(std::move(geometry)) {}

bool D2DGeometry::FillContains(const Point& point) {
  BOOL result;
  ThrowIfFailed(geometry_->FillContainsPoint(
      Convert(point), D2D1::Matrix3x2F::Identity(), &result));
  return result != 0;
}
}  // namespace cru::platform::graphics::win::direct