Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path geometry refactoring, wip #102

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/D2DLibExport/D2DDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public D2DRectangleGeometry CreateRectangleGeometry(FLOAT width, FLOAT height)

public D2DRectangleGeometry CreateRectangleGeometry(D2DRect rect)
{
HANDLE rectGeometryHandle = D2D.CreateRectangleGeometry(this.Handle, ref rect);
HANDLE rectGeometryHandle = D2D.CreateRectangleGeometry(this.Handle, rect);
return new D2DRectangleGeometry(this, rectGeometryHandle);
}

Expand Down
2 changes: 1 addition & 1 deletion src/D2DLibExport/D2DLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static extern HANDLE CreateTextPathGeometry(HANDLE ctx, [In] string text,
#region Geometry

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern HANDLE CreateRectangleGeometry([In] HANDLE ctx, [In] ref D2DRect rect);
public static extern HANDLE CreateRectangleGeometry([In] HANDLE ctx, [In] D2DRect rect);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyGeometry(HANDLE geometryContext);
Expand Down
6 changes: 6 additions & 0 deletions src/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<Using Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup>
<Compile Update="SampleCode\Test.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions src/Examples/SampleCode/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2009-2021 Jingwood, unvell.com. All right reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace unvell.D2DLib.Examples.SampleCode
{
public partial class TestForm : ExampleForm
{
public TestForm()
{
Text = "HelloWorld - d2dlib Examples";
}

protected override void OnRender(D2DGraphics g)

{

g.Clear(D2DColor.FromGDIColor(this.BackColor));



using (var rect = this.Device.CreateRectangleGeometry(new D2DRect(10, 10, 20, 20)))

{

g.DrawPath(rect, D2DColor.BlueViolet, 1, D2DDashStyle.Dash);

}



using (var circle = this.Device.CreateEllipseGeometry(new D2DPoint(100, 100), new D2DSize(50, 50)))

{

using (var pen = this.Device.CreatePen(D2DColor.BlueViolet, D2DDashStyle.Dash))

{

if (pen != null)

{

g.DrawPath(circle, pen, 2);

g.FillPath(circle, D2DColor.DarkBlue);

}

}

}

}

}

}
1 change: 1 addition & 0 deletions src/d2dlib/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ enum GeometryType {

typedef struct D2DGeometryContext {
D2DContext* d2context;
GeometryType geometryType;
ID2D1Geometry* geometry;
} D2DGeometryContext;

Expand Down
54 changes: 38 additions & 16 deletions src/d2dlib/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ void DestroyGeometry(HANDLE geometryHandle) {
delete context;
}

HANDLE CreateRectangleGeometry(HANDLE ctx, D2D1_RECT_F& rect)
HANDLE CreateRectangleGeometry(HANDLE ctx, D2D1_RECT_F rect)
{
RetrieveContext(ctx);

ID2D1RectangleGeometry* rectGeo;
context->factory->CreateRectangleGeometry(rect, &rectGeo);
HRESULT hr = context->factory->CreateRectangleGeometry(&rect, &rectGeo);
if (!SUCCEEDED(hr)) {
return NULL;
}

D2DGeometryContext* pathCtx = new D2DGeometryContext();
pathCtx->d2context = context;
Expand Down Expand Up @@ -181,32 +184,51 @@ void AddPathArc(HANDLE ctx, D2D1_POINT_2F endPoint, D2D1_SIZE_F size, FLOAT swee
pathContext->sink->AddArc(&seg);
}

void DrawPath(HANDLE pathCtx, D2D1_COLOR_F strokeColor, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle)
void DrawGeometry(HANDLE geometryHandler, D2D1_COLOR_F strokeColor, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle)
{
D2DPathContext* pathContext = reinterpret_cast<D2DPathContext*>(pathCtx);
D2DContext* context = pathContext->d2context;

D2DGeometryContext* geoContext = reinterpret_cast<D2DPathContext*>(geometryHandler);
if (geoContext == NULL) {
return;
}

D2DContext* context = geoContext->d2context;
ID2D1Factory* factory = context->factory;
ID2D1RenderTarget* renderTarget = context->renderTarget;

ID2D1SolidColorBrush* strokeBrush = NULL;
renderTarget->CreateSolidColorBrush(strokeColor, &strokeBrush);
HRESULT hr = renderTarget->CreateSolidColorBrush(strokeColor, &strokeBrush);
if (!SUCCEEDED(hr) || strokeBrush == NULL) {
return;
}

ID2D1StrokeStyle *strokeStyle = NULL;
ID2D1StrokeStyle* strokeStyle = NULL;

if (dashStyle != D2D1_DASH_STYLE_SOLID)
{
factory->CreateStrokeStyle(D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_ROUND,
D2D1_LINE_JOIN_MITER,
10.0f,
dashStyle,
0.0f), NULL, 0, &strokeStyle);
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_ROUND,
D2D1_LINE_JOIN_MITER,
10.0f,
dashStyle,
0.0f), NULL, 0, &strokeStyle);
}

renderTarget->DrawGeometry(pathContext->path, strokeBrush, strokeWidth, strokeStyle);
switch(geoContext->geometryType) {
default:
// unknown type, abort to render
break;

case GeometryType::GeoType_RectangleGeometry:
ID2D1RectangleGeometry* rectGeomtry = reinterpret_cast<ID2D1RectangleGeometry*>(geoContext->geometry);
renderTarget->DrawGeometry(rectGeomtry, strokeBrush, strokeWidth, strokeStyle);
break;

case GeometryType::GeoType_PathGeometry:
D2DPathContext* pathContext = reinterpret_cast<D2DPathContext*>(geoContext);
renderTarget->DrawGeometry(pathContext->path, strokeBrush, strokeWidth, strokeStyle);
break;

SafeRelease(&strokeBrush);
SafeRelease(&strokeStyle);
Expand Down