jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
245 stars 43 forks source link

path geometry refactoring, wip #102

Open jingwood opened 1 year ago

jingwood commented 1 year ago

fix #98, wip

MarkhusGenferei commented 1 year ago

Hi,

I have noticed the addition of the GeometryType enum and its inclusion in the D2DGeometryContext structure. And also, that it is used in the switch statement of DrawGeometry() in Geometry.cpp to cast the geometry pointer according to the geometry type before drawing.

My understanding is that is is not required because all geometry types inherit from ID2D1Geometry. DrawGeometry() could draw any geometry type from a ID2D1Geometry argument.

Actually, the following functions in Geometry.cpp should work on all geometry types if they were adapted to take a D2DGeometryContext handle as argument instead of a D2DPathContext :

void DrawGeometry(HANDLE geometryHandler, D2D1_COLOR_F strokeColor, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle);
HANDLE CreateCombinedGeometry(HANDLE d2dCtx, HANDLE pathCtx1, HANDLE pathCtx2, D2D1_COMBINE_MODE combineMode, FLOAT flatteningTolerance); sorry :-\
void DrawPathWithPen(HANDLE pathCtx, HANDLE strokePen, FLOAT strokeWidth);
void FillPathD(HANDLE pathCtx, D2D1_COLOR_F fillColor);
void FillPathWithBrush(HANDLE ctx, HANDLE brushHandle);
bool PathFillContainsPoint(HANDLE pathCtx, D2D1_POINT_2F point);
bool PathStrokeContainsPoint(HANDLE pathCtx, D2D1_POINT_2F point, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle);
void GetGeometryBounds(HANDLE pathCtx, __out D2D1_RECT_F* rect);
void GetGeometryTransformedBounds(HANDLE pathCtx, __in D2D1_MATRIX_3X2_F* mat3x2, __out D2D1_RECT_F* rect);

In these functions, the minimal adaptation consists in replacing

D2DPathContext* pathContext = reinterpret_cast<D2DPathContext*>(pathCtx);

by

D2DGeometryContext* geometryContext = reinterpret_cast<D2DGeometryContext*>(pathCtx);

and

pathContext->path

by

geometryContext->geometry

This will work because D2DPathContext inherits from D2DGeometryContext, and because D2DPathContext's constructor always sets its base class geometryContext member. CreateRectangleGeometry and CreateEllipseGeometry do the same.

Cheers, Markhus

MarkhusGenferei commented 1 year ago

This is what was proposed in #100

jingwood commented 1 year ago

Thanks @MarkhusGenferei, I think you are correct! I am considering merge the #103, which is the rebased branch from #100.