RenderableGrid::RenderableGrid(Direct2D1& _d2d1, IJSON& _parameters) : Renderable(_d2d1, _parameters), m_geometry(NULL), m_tgeometry(NULL) { IJSON& params = *_parameters[L"parameters"]; D2D1_SIZE_F size, steps; size.width = (*params[L"size"])[0]->AsNumber(); size.height = (*params[L"size"])[1]->AsNumber(); steps.width = (*params[L"steps"])[0]->AsNumber(); steps.height = (*params[L"steps"])[1]->AsNumber(); if (SUCCEEDED(this->m_d2d1.D2D1().CreatePathGeometry(&this->m_geometry))) { ID2D1GeometrySink *pSink = NULL; if (SUCCEEDED(this->m_geometry->Open(&pSink))) { D2D1_POINT_2F p1, p2; pSink->SetFillMode(D2D1_FILL_MODE_WINDING); p1.y = -size.height / 2.0f; p2.y = size.height / 2.0f; for (float x = -size.width / 2.0f ; (size.width / 2.0f) >= x ; x += steps.width) { p1.x = x; p2.x = x; pSink->BeginFigure(p1, D2D1_FIGURE_BEGIN_HOLLOW); pSink->AddLine(p2); pSink->EndFigure(D2D1_FIGURE_END_OPEN); } p1.x = -size.width / 2.0f; p2.x = size.width / 2.0f; for (float y = -size.height / 2.0f ; (size.height / 2.0f) >= y ; y += steps.height) { p1.y = y; p2.y = y; pSink->BeginFigure(p1, D2D1_FIGURE_BEGIN_HOLLOW); pSink->AddLine(p2); pSink->EndFigure(D2D1_FIGURE_END_OPEN); } pSink->Close(); pSink->Release(); } } }
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 ); }
//----------------------------------------------------------------------------- ID2D1PathGeometry* D2DGraphicsPath::getPath (int32_t fillMode) { if (path == 0 || fillMode != currentPathFillMode) { dirty (); if (!SUCCEEDED (getD2DFactory ()->CreatePathGeometry (&path))) return 0; if (fillMode == -1) fillMode = 0; currentPathFillMode = fillMode; ID2D1GeometrySink* sink = 0; if (!SUCCEEDED (path->Open (&sink))) { path->Release (); path = 0; return 0; } sink->SetFillMode ((D2D1_FILL_MODE)fillMode); bool figureOpen = false; CPoint lastPos; for (ElementList::const_iterator it = elements.begin (); it != elements.end (); it++) { Element e = (*it); switch (e.type) { case Element::kArc: { bool clockwise = e.instruction.arc.clockwise; double startAngle = e.instruction.arc.startAngle; double endAngle = e.instruction.arc.endAngle; CRect o_r (e.instruction.arc.rect.left, e.instruction.arc.rect.top, e.instruction.arc.rect.right, e.instruction.arc.rect.bottom); CRect r (o_r); o_r.originize (); CPoint center = o_r.getCenter (); CPoint start; start.x = r.left + center.x + center.x * cos (radians (startAngle)); start.y = r.top + center.y + center.y * sin (radians (startAngle)); if (!figureOpen) { sink->BeginFigure (makeD2DPoint (start), D2D1_FIGURE_BEGIN_FILLED); figureOpen = true; } else if (lastPos != start) { sink->AddLine (makeD2DPoint (start)); } double sweepangle = endAngle - startAngle; if (clockwise) { // sweepangle positive while (sweepangle < 0.0) sweepangle += 360.0; while (sweepangle > 360.0) sweepangle -= 360.0; } else { // sweepangle negative while (sweepangle > 0.0) sweepangle -= 360.0; while (sweepangle < -360.0) sweepangle += 360.0; } CPoint endPoint; endPoint.x = r.left + center.x + center.x * cos (radians (endAngle)); endPoint.y = r.top + center.y + center.y * sin (radians (endAngle)); D2D1_ARC_SEGMENT arc; arc.size = makeD2DSize (r.getWidth ()/2., r.getHeight ()/2.); arc.rotationAngle = 0; arc.sweepDirection = clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE; arc.point = makeD2DPoint (endPoint); arc.arcSize = fabs(sweepangle) <= 180. ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE; sink->AddArc (arc); lastPos = endPoint; break; } case Element::kEllipse: { CRect r (e.instruction.rect.left, e.instruction.rect.top, e.instruction.rect.right, e.instruction.rect.bottom); CPoint top (r.getTopLeft ()); top.x += r.getWidth () / 2.; CPoint bottom (r.getBottomLeft ()); bottom.x += r.getWidth () / 2.; if (figureOpen && lastPos != CPoint (e.instruction.rect.left, e.instruction.rect.top)) { sink->EndFigure (D2D1_FIGURE_END_OPEN); figureOpen = false; } if (!figureOpen) { sink->BeginFigure (makeD2DPoint (top), D2D1_FIGURE_BEGIN_FILLED); figureOpen = true; } D2D1_ARC_SEGMENT arc = D2D1::ArcSegment (makeD2DPoint (bottom), D2D1::SizeF ((FLOAT)r.getWidth ()/2.f, (FLOAT)r.getHeight ()/2.f), 180.f, D2D1_SWEEP_DIRECTION_CLOCKWISE, D2D1_ARC_SIZE_SMALL); sink->AddArc (arc); arc.point = makeD2DPoint (top); sink->AddArc (arc); lastPos = top; break; } case Element::kRect: { D2D1_POINT_2F points[4] = { {(FLOAT)e.instruction.rect.right, (FLOAT)e.instruction.rect.top}, {(FLOAT)e.instruction.rect.right, (FLOAT)e.instruction.rect.bottom}, {(FLOAT)e.instruction.rect.left, (FLOAT)e.instruction.rect.bottom}, {(FLOAT)e.instruction.rect.left, (FLOAT)e.instruction.rect.top} }; if (figureOpen && lastPos != CPoint (e.instruction.rect.left, e.instruction.rect.top)) { sink->EndFigure (D2D1_FIGURE_END_OPEN); figureOpen = false; } if (figureOpen == false) { sink->BeginFigure (points[3], D2D1_FIGURE_BEGIN_FILLED); figureOpen = true; } sink->AddLine (points[0]); sink->AddLine (points[1]); sink->AddLine (points[2]); sink->AddLine (points[3]); lastPos = CPoint (e.instruction.rect.left, e.instruction.rect.top); break; } case Element::kLine: { if (figureOpen) { D2D1_POINT_2F end = {(FLOAT)e.instruction.point.x, (FLOAT)e.instruction.point.y}; sink->AddLine (end); lastPos = CPoint (e.instruction.point.x, e.instruction.point.y); } break; } case Element::kBezierCurve: { if (figureOpen) { D2D1_POINT_2F control1 = {(FLOAT)e.instruction.curve.control1.x, (FLOAT)e.instruction.curve.control1.y}; D2D1_POINT_2F control2 = {(FLOAT)e.instruction.curve.control2.x, (FLOAT)e.instruction.curve.control2.y}; D2D1_POINT_2F end = {(FLOAT)e.instruction.curve.end.x, (FLOAT)e.instruction.curve.end.y}; D2D1_BEZIER_SEGMENT bezier = D2D1::BezierSegment (control1, control2, end); sink->AddBezier (bezier); lastPos = CPoint (e.instruction.curve.end.x, e.instruction.curve.end.y); } break; } case Element::kBeginSubpath: { if (figureOpen) sink->EndFigure (D2D1_FIGURE_END_OPEN); D2D1_POINT_2F start = {(FLOAT)e.instruction.point.x, (FLOAT)e.instruction.point.y}; sink->BeginFigure (start, D2D1_FIGURE_BEGIN_FILLED); figureOpen = true; lastPos = CPoint (e.instruction.point.x, e.instruction.point.y); break; } case Element::kCloseSubpath: { if (figureOpen) { sink->EndFigure (D2D1_FIGURE_END_CLOSED); figureOpen = false; } break; } } } if (figureOpen) sink->EndFigure (D2D1_FIGURE_END_OPEN); HRESULT res = sink->Close (); if (!SUCCEEDED (res)) { path->Release (); path = 0; } sink->Release (); } return path; }
// // Generates new graph geometry, which may change every frame. // HRESULT D3D12MemoryManagement::GenerateMemoryGraphGeometry(const RectF& Bounds, UINT GraphSizeMB, ID2D1PathGeometry** ppPathGeometry) { HRESULT hr; ID2D1PathGeometry* pPathGeometry = nullptr; ID2D1GeometrySink* pGeometrySink = nullptr; hr = m_pD2DFactory->CreatePathGeometry(&pPathGeometry); if (FAILED(hr)) { LOG_WARNING("Failed to create path geometry, hr=0x%.8x"); goto cleanup; } hr = pPathGeometry->Open(&pGeometrySink); if (FAILED(hr)) { LOG_WARNING("Failed to open path geometry sink, hr=0x%.8x"); goto cleanup; } { float GraphHeightPadded = GRAPH_HEIGHT * GRAPH_HEIGHT_RATIO / 100; float XStep = GRAPH_WIDTH / GRAPH_SEGMENTS; D2D1_POINT_2F Point; Point.x = Bounds.Left; Point.y = Bounds.Bottom; pGeometrySink->BeginFigure(Point, D2D1_FIGURE_BEGIN_FILLED); for (UINT i = 0; i < NUM_GRAPH_POINTS; ++i) { UINT32 GraphPoint = (i + m_CurrentGraphPoint) % NUM_GRAPH_POINTS; Point.y = Bounds.Bottom - (m_GraphPoints[GraphPoint] / 1024 / 1024) / (float)GraphSizeMB * GraphHeightPadded; pGeometrySink->AddLine(Point); Point.x += XStep; } Point.x = Bounds.Right; Point.y = Bounds.Bottom; pGeometrySink->AddLine(Point); pGeometrySink->EndFigure(D2D1_FIGURE_END_CLOSED); } hr = pGeometrySink->Close(); if (FAILED(hr)) { LOG_ERROR("Failed to close path geometry sink, hr=0x%.8x", hr); goto cleanup; } pGeometrySink->Release(); *ppPathGeometry = pPathGeometry; return S_OK; cleanup: SafeRelease(pGeometrySink); SafeRelease(pPathGeometry); return hr; }