/// <summary> /// Initializes a new instance of the <see cref="Polyline"/> class. /// </summary> /// <param name="">The .</param> RubyGM::Drawable::Polyline::Polyline(const PolylineStatus& ps) noexcept : Super(ps) { assert(m_pGiGeometry == nullptr); assert(ps.points && ps.count >= 2); this->filled_color.a = 0.f; // 初始化 ID2D1PathGeometry* path = nullptr; ID2D1GeometrySink* sink = nullptr; // 创建路径几何 auto hr = Bridge::CreatePathGeometry(path); // 打开路径记录 if (SUCCEEDED(hr)) { hr = path->Open(&sink); } // 填充路径 if (SUCCEEDED(hr)) { auto points = impl::d2d(ps.points); sink->BeginFigure(*points, D2D1_FIGURE_BEGIN_HOLLOW); sink->AddLines(points + 1, ps.count - 1); sink->EndFigure(D2D1_FIGURE_END_CLOSED); hr = sink->Close(); } // 错误代码 if (FAILED(hr)) Game::SetLastErrorCode(hr); RubyGM::SafeRelease(sink); // 数据转换 auto gi = static_cast<ID2D1Geometry*>(path); m_pGiGeometry = static_cast<IGMGeometry*>(gi); }
HRESULT Scene::CreateMouthGeometry() { HRESULT hr = S_OK; //mouth hr = m_pFactory->CreatePathGeometry(&m_pMouthGeometry); if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = m_pMouthGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(m_ellipse.point.x, m_ellipse.point.y), D2D1_FIGURE_BEGIN_FILLED ); D2D1_POINT_2F points[3] = { D2D1::Point2F(m_ellipse.point.x + m_ellipse.radiusX, m_ellipse.point.y - 10), D2D1::Point2F(m_ellipse.point.x + m_ellipse.radiusX, m_ellipse.point.y + 10), D2D1::Point2F(m_ellipse.point.x, m_ellipse.point.y), }; pSink->AddLines(points, ARRAYSIZE(points)); pSink->EndFigure(D2D1_FIGURE_END_CLOSED); } hr = pSink->Close(); SafeRelease(&pSink); return hr; } }
void STDMETHODCALLTYPE AddLines(CONST D2D1_POINT_2F *points, UINT pointsCount) { if( m_BaseSink ) m_BaseSink->AddLines(points, pointsCount); for( size_t i=0; i<pointsCount; ++i ) { m_Lines.push_back(m_Cursor); m_Lines.push_back(points[i]); m_Cursor = points[i]; } }
// 创建路径几何 void ImageRenderer::MakePath() { SafeRelease(m_pNowPath); m_pd2dFactory->CreatePathGeometry(&m_pNowPath); if (m_pNowPath) { ID2D1GeometrySink* sink = nullptr; HRESULT hr = m_pNowPath->Open(&sink); if (SUCCEEDED(hr)) { sink->BeginFigure(points[0], D2D1_FIGURE_BEGIN_FILLED); sink->AddLines(points + 1, m_cPointCount); sink->EndFigure(D2D1_FIGURE_END_CLOSED); sink->Close(); } SafeRelease(sink); } }
Home::Home(float xPos, float yPos, ID2D1Factory *pID2D1Factory) : Actor(xPos, yPos) { r = 0.0f; g = 1.0f; b = 0.0f; a = 1.0f; HRESULT hr = pID2D1Factory->CreatePathGeometry(&pHomeGeometry); if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = pHomeGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(25, -25), D2D1_FIGURE_BEGIN_FILLED ); D2D1_POINT_2F points[] = { D2D1::Point2F(25, 25), D2D1::Point2F(-25, 25), D2D1::Point2F(-25, -25) }; pSink->AddLines(points, ARRAYSIZE(points)); pSink->EndFigure(D2D1_FIGURE_END_CLOSED); } hr = pSink->Close(); pSink->Release(); } D2D1_MATRIX_3X2_F mov; mov = D2D1::Matrix3x2F::Translation( D2D1::SizeF(x, y) ); D2D1_MATRIX_3X2_F transform = mov; hr = pID2D1Factory->CreateTransformedGeometry( pHomeGeometry, &transform, &pTransformed ); }
HRESULT DemoApp::CreateDeviceIndependentResources() { HRESULT hr; ID2D1GeometrySink *pSink = NULL; // Create a Direct2D factory. hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory); if (SUCCEEDED(hr)) { // Create the path geometry. hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometry); } if (SUCCEEDED(hr)) { // Write to the path geometry using the geometry sink. We are going to create a // spiral hr = m_pPathGeometry->Open(&pSink); } if (SUCCEEDED(hr)) { D2D1_POINT_2F currentLocation = {0, 0}; pSink->BeginFigure(currentLocation, D2D1_FIGURE_BEGIN_FILLED); D2D1_POINT_2F locDelta = {2, 2}; float radius = 3; for (UINT i = 0; i < 30; ++i) { currentLocation.x += radius * locDelta.x; currentLocation.y += radius * locDelta.y; pSink->AddArc( D2D1::ArcSegment( currentLocation, D2D1::SizeF(2*radius, 2*radius), // radiusx/y 0.0f, // rotation angle D2D1_SWEEP_DIRECTION_CLOCKWISE, D2D1_ARC_SIZE_SMALL ) ); locDelta = D2D1::Point2F(-locDelta.y, locDelta.x); radius += 3; } pSink->EndFigure(D2D1_FIGURE_END_OPEN); hr = pSink->Close(); } if (SUCCEEDED(hr)) { // Create the path geometry. hr = m_pD2DFactory->CreatePathGeometry(&m_pObjectGeometry); } if (SUCCEEDED(hr)) { // Write to the object geometry using the geometry sink. // We are going to create a simple triangle hr = m_pObjectGeometry->Open(&pSink); } if (SUCCEEDED(hr)) { pSink->BeginFigure( D2D1::Point2F(0.0f, 0.0f), D2D1_FIGURE_BEGIN_FILLED ); const D2D1_POINT_2F ptTriangle[] = {{-10.0f, -10.0f}, {-10.0f, 10.0f}, {0.0f, 0.0f}}; pSink->AddLines(ptTriangle, 3); pSink->EndFigure(D2D1_FIGURE_END_OPEN); hr = pSink->Close(); } SafeRelease(&pSink); return hr; }
VOID CreateD2DResource(HWND hWnd) { // This function was called in the DrawRectangle function which in turn called to response the // WM_PAINT Message, to avoid creating resource every time, we test the pointer to g_pRenderTarget // If the resource already create, skip the function if (!g_pRenderTarget) { HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory) ; if (FAILED(hr)) { MessageBox(hWnd, "Create D2D factory failed!", "Error", 0) ; return ; } // Obtain the size of the drawing area RECT rc ; GetClientRect(hWnd, &rc) ; // Create a Direct2D render target hr = g_pD2DFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties( hWnd, D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top) ), &g_pRenderTarget ) ; if (FAILED(hr)) { MessageBox(hWnd, "Create render target failed!", "Error", 0) ; return ; } // Create a brush hr = g_pRenderTarget->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::Black), &g_pSceneBrush ) ; if (FAILED(hr)) { MessageBox(hWnd, "Create brush failed!", "Error", 0) ; return ; } // Create left mountain geometry hr = g_pD2DFactory->CreatePathGeometry(&g_pLeftMountainGeometry) ; if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = g_pLeftMountainGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(346,255), D2D1_FIGURE_BEGIN_FILLED ); D2D1_POINT_2F points[5] = { D2D1::Point2F(267, 177), D2D1::Point2F(236, 192), D2D1::Point2F(212, 160), D2D1::Point2F(156, 255), D2D1::Point2F(346, 255), }; pSink->AddLines(points, ARRAYSIZE(points)); pSink->EndFigure(D2D1_FIGURE_END_CLOSED); } pSink->Close() ; SAFE_RELEASE(pSink) ; } // Create right mountain geometry hr = g_pD2DFactory->CreatePathGeometry(&g_pRightMountainGeometry) ; if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = g_pRightMountainGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(575,263), D2D1_FIGURE_BEGIN_FILLED ); D2D1_POINT_2F points[] = { D2D1::Point2F(481, 146), D2D1::Point2F(449, 181), D2D1::Point2F(433, 159), D2D1::Point2F(401, 214), D2D1::Point2F(381, 199), D2D1::Point2F(323, 263), D2D1::Point2F(575, 263) }; pSink->AddLines(points, ARRAYSIZE(points)); pSink->EndFigure(D2D1_FIGURE_END_CLOSED); } hr = pSink->Close(); SAFE_RELEASE(pSink); } // Create sun geometry hr = g_pD2DFactory->CreatePathGeometry(&g_pSunGeometry) ; if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = g_pSunGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(270, 255), D2D1_FIGURE_BEGIN_FILLED ); pSink->AddArc( D2D1::ArcSegment( D2D1::Point2F(440, 255), // end point D2D1::SizeF(85, 85), 0.0f, // rotation angle D2D1_SWEEP_DIRECTION_CLOCKWISE, D2D1_ARC_SIZE_SMALL )); pSink->EndFigure(D2D1_FIGURE_END_CLOSED); pSink->BeginFigure( D2D1::Point2F(299, 182), D2D1_FIGURE_BEGIN_HOLLOW ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(299, 182), D2D1::Point2F(294, 176), D2D1::Point2F(285, 178) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(276, 179), D2D1::Point2F(272, 173), D2D1::Point2F(272, 173) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); pSink->BeginFigure( D2D1::Point2F(354, 156), D2D1_FIGURE_BEGIN_HOLLOW ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(354, 156), D2D1::Point2F(358, 149), D2D1::Point2F(354, 142) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(349, 134), D2D1::Point2F(354, 127), D2D1::Point2F(354, 127) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); pSink->BeginFigure( D2D1::Point2F(322,164), D2D1_FIGURE_BEGIN_HOLLOW ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(322, 164), D2D1::Point2F(322, 156), D2D1::Point2F(314, 152) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(306, 149), D2D1::Point2F(305, 141), D2D1::Point2F(305, 141) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); pSink->BeginFigure( D2D1::Point2F(385, 164), D2D1_FIGURE_BEGIN_HOLLOW ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(385,164), D2D1::Point2F(392,161), D2D1::Point2F(394,152) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(395,144), D2D1::Point2F(402,141), D2D1::Point2F(402,142) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); pSink->BeginFigure( D2D1::Point2F(408,182), D2D1_FIGURE_BEGIN_HOLLOW ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(408,182), D2D1::Point2F(416,184), D2D1::Point2F(422,178) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(428,171), D2D1::Point2F(435,173), D2D1::Point2F(435,173) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); } hr = pSink->Close(); SAFE_RELEASE(pSink); } // Create river geometry hr = g_pD2DFactory->CreatePathGeometry(&g_pRiverGeometry) ; if (SUCCEEDED(hr)) { ID2D1GeometrySink *pSink = NULL; hr = g_pRiverGeometry->Open(&pSink); if (SUCCEEDED(hr)) { pSink->SetFillMode(D2D1_FILL_MODE_WINDING); pSink->BeginFigure( D2D1::Point2F(183, 392), D2D1_FIGURE_BEGIN_FILLED ); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(238, 284), D2D1::Point2F(472, 345), D2D1::Point2F(356, 303) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(237, 261), D2D1::Point2F(333, 256), D2D1::Point2F(333, 256) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(335, 257), D2D1::Point2F(241, 261), D2D1::Point2F(411, 306) )); pSink->AddBezier( D2D1::BezierSegment( D2D1::Point2F(574, 350), D2D1::Point2F(288, 324), D2D1::Point2F(296, 392) )); pSink->EndFigure(D2D1_FIGURE_END_OPEN); } pSink->Close() ; SAFE_RELEASE(pSink) ; } } }