Jump to content
  • Advertisement
Sign in to follow this  
VinQbator

Filling/brushing irregular shape C++, Win32 GDI

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for this noob topic, but...


I'm trying to design chess pieces using GDI and I have ran into some problems. When I create an outline of a shape I can't figure out how to fill the whole shape with a color.
PAINTSTRUCT ps;
HDC hdc;
HBRUSH brush;
HPEN hpen;

POINT PtLeft[4] = { { iSqrSz * ( 0.50 - 0.06), iSqrSz * ( 0.35 + 0.103923) },
{ iSqrSz * ((0.50 - 0.06) - 0.07), iSqrSz * (( 0.35 + 0.103923) + 0.04) },
{ iSqrSz * ((0.50 - 0.06) - 0.07), iSqrSz * (( 0.35 + 0.103923) + 0.04) },
{ iSqrSz * ( 0.50 - 0.06), iSqrSz * ((0.35 + 0.103923) + 0.08) }};

POINT PtRight[4] = { { iSqrSz * ( 0.50 + 0.06), iSqrSz * ((0.35 + 0.103923) + 0.08) },
{ iSqrSz * ((0.50 + 0.06) + 0.07), iSqrSz * (( 0.35 + 0.103923) + 0.04) },
{ iSqrSz * ((0.50 + 0.06) + 0.07), iSqrSz * (( 0.35 + 0.103923) + 0.04) },
{ iSqrSz * ( 0.50 + 0.06), iSqrSz * ( 0.35 + 0.103923) }};

POINT PtBaseLeft[4] = { { iSqrSz * ( 0.50 - 0.06), iSqrSz * ((0.35 + 0.103923) + 0.08) },
{ iSqrSz * 0.38, iSqrSz * 0.75 },
{ iSqrSz * 0.15, iSqrSz * 0.90 },
{ iSqrSz * 0.30, iSqrSz * 0.95 }};

POINT PtBaseRight[4] = { { iSqrSz * 0.70, iSqrSz * 0.95 },
{ iSqrSz * 0.85, iSqrSz * 0.90 },
{ iSqrSz * 0.62, iSqrSz * 0.75 },
{ iSqrSz * ( 0.50 + 0.06), iSqrSz * ((0.35 + 0.103923) + 0.08) }};

hdc = BeginPaint(hWnd, &ps);
// Create and select brush
brush = CreateSolidBrush(RGB(128, 128, 128));
SelectObject(hdc, brush);

// Create and select pen
hpen = CreatePen(PS_SOLID, 1, RGB(128, 0, 128));
SelectObject(hdc, hpen);

BeginPath(hdc);
// Bottom right base corner
PolyBezier(hdc, PtBaseRight, 4);
// Bottom right neck corner
PolyBezier(hdc, PtRight, 4);
// Right head corner
Arc(hdc, iSqrSz * 0.38, iSqrSz * 0.23, iSqrSz * 0.62, iSqrSz * 0.47,
iSqrSz * ( 0.50 + 0.06), iSqrSz * ( 0.35 + 0.103923),
iSqrSz * ( 0.50 - 0.06), iSqrSz * ( 0.35 + 0.103923));
// Top left neck corner
PolyBezier(hdc, PtLeft, 4);
// Top left base corner
PolyBezier(hdc, PtBaseLeft, 4);
// Join
MoveToEx(hdc, iSqrSz * 0.30, iSqrSz * 0.95, NULL);
LineTo(hdc, iSqrSz * 0.70, iSqrSz * 0.95);

EndPath(hdc);


StrokeAndFillPath(hdc);

EndPaint(hWnd, &ps);
DeleteObject(brush);

BeginPath() and StrokeAndFillPath() gives me every single shape with connected ends, but doesn't make the Path to one shape.
capturerln.png

I tried adding two triangles and one rectangle to fill the shape, but that doesn't seem to be a elegant solution.
capture1lg.png
I used purple pen just to visualize the code better.

The problem with adding rectangles and triangles is that when bezier curve is more S shaped than in this piece then the FillPath() will draw a line from one end of the curve to the other and fills both the closed parts.

I tried using PathToRegion(), but this got me nowhere. I have browsed different Win32 GDI tutorials but no help there.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!