Skip to content

Commit

Permalink
feat: 增加线条连接点样式设置,默认样式为尖角 (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
yixy-only authored Jun 5, 2024
1 parent c5f3446 commit eafc794
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
13 changes: 13 additions & 0 deletions include/ege.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ enum linecaptype
LINECAP_ROUND,
};

enum linejointype
{
LINEJOIN_MITER = 0,
LINEJOIN_BEVEL,
LINEJOIN_ROUND,
};

typedef struct key_msg
{
int key;
Expand Down Expand Up @@ -873,11 +880,17 @@ void EGEAPI window_setviewport(int left, int top, int right, int bottom);
void EGEAPI getlinestyle(int* linestyle, unsigned short* pattern = NULL, int* thickness = NULL, PCIMAGE pimg = NULL);
void EGEAPI setlinestyle(int linestyle, unsigned short pattern = 0, int thickness = 1, PIMAGE pimg = NULL);
void EGEAPI setlinewidth(float width, PIMAGE pimg = NULL);

void EGEAPI setlinecap(linecaptype linecap, PIMAGE pimg = NULL);
void EGEAPI setlinecap(linecaptype startCap, linecaptype endCap, PIMAGE pimg = NULL);
void EGEAPI getlinecap(linecaptype* startCap, linecaptype* endCap, PIMAGE pimg = NULL);
linecaptype EGEAPI getlinecap(PIMAGE pimg = NULL);

void EGEAPI setlinejoin(linejointype linejoin, PIMAGE pimg = NULL);
void EGEAPI setlinejoin(linejointype linejoin, float miterLimit, PIMAGE pimg = NULL);
void EGEAPI getlinejoin(linejointype* linejoin, float* miterLimit, PIMAGE pimg = NULL);
linejointype EGEAPI getlinejoin(PIMAGE pimg = NULL);

//void getfillstyle(color_t *pcolor, int *ppattern = NULL, PIMAGE pimg = NULL); // ###
void EGEAPI setfillstyle(int pattern, color_t color, PIMAGE pimg = NULL);

Expand Down
2 changes: 2 additions & 0 deletions src/ege_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Gdiplus::Graphics* recreateGdiplusGraphics(HDC hdc, const Gdiplus::Graphics* old

Gdiplus::LineCap convertToGdiplusLineCap(linecaptype linecap);

Gdiplus::LineJoin convertToGdiplusLineJoin(linejointype linejoin);

int swapbuffers();

bool isinitialized();
Expand Down
74 changes: 71 additions & 3 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,15 @@ static void update_pen(PIMAGE img)
default: ls |= PS_ENDCAP_FLAT; break;
}

switch(img->m_linejoin) {
case LINEJOIN_MITER: ls |= PS_JOIN_MITER; break;
case LINEJOIN_BEVEL: ls |= PS_JOIN_BEVEL; break;
case LINEJOIN_ROUND: ls |= PS_JOIN_ROUND; break;
default: ls |= PS_JOIN_MITER; break;
}

SetMiterLimit(img->m_hDC, img->m_linejoinmiterlimit, NULL);

HPEN hpen;
if (linestyle == USERBIT_LINE) {
DWORD style[20] = {0};
Expand All @@ -569,8 +578,10 @@ static void update_pen(PIMAGE img)
pen->SetWidth(img->m_linewidth);
pen->SetDashStyle(linestyle_to_dashstyle(img->m_linestyle.linestyle));

pen->SetLineCap(convertToGdiplusLineCap(img->m_linestartcap), convertToGdiplusLineCap(img->m_lineendcap),
Gdiplus::DashCapFlat);
pen->SetStartCap(convertToGdiplusLineCap(img->m_linestartcap));
pen->SetEndCap(convertToGdiplusLineCap(img->m_lineendcap));
pen->SetLineJoin(convertToGdiplusLineJoin(img->m_linejoin));
pen->SetMiterLimit(img->m_linejoinmiterlimit);
#endif
}

Expand Down Expand Up @@ -1232,12 +1243,23 @@ Gdiplus::LineCap convertToGdiplusLineCap(linecaptype linecap)
case LINECAP_FLAT: cap = Gdiplus::LineCapFlat; break;
case LINECAP_SQUARE: cap = Gdiplus::LineCapSquare; break;
case LINECAP_ROUND: cap = Gdiplus::LineCapRound; break;
default: cap = Gdiplus::LineCapFlat; break;
}

return cap;
}

Gdiplus::LineJoin convertToGdiplusLineJoin(linejointype linejoin)
{
Gdiplus::LineJoin joinType = Gdiplus::LineJoinMiter;
switch(linejoin) {
case LINEJOIN_MITER: joinType = Gdiplus::LineJoinMiter; break;
case LINEJOIN_BEVEL: joinType = Gdiplus::LineJoinBevel; break;
case LINEJOIN_ROUND: joinType = Gdiplus::LineJoinRound; break;
}

return joinType;
}

void setlinecap(linecaptype linecap, PIMAGE pimg)
{
PIMAGE img = CONVERT_IMAGE_CONST(pimg);
Expand Down Expand Up @@ -1290,6 +1312,52 @@ linecaptype getlinecap(PIMAGE pimg)
return LINECAP_FLAT;
}

void setlinejoin(linejointype linejoin, PIMAGE pimg)
{
float miterLimit;
getlinejoin(NULL, &miterLimit, pimg);
setlinejoin(linejoin, miterLimit, pimg);
}

void setlinejoin(linejointype linejoin, float miterLimit, PIMAGE pimg)
{
PIMAGE img = CONVERT_IMAGE_CONST(pimg);

if (img && img->m_hDC) {
miterLimit = MAX(1.0f, miterLimit);
img->m_linejoin = linejoin;
img->m_linejoinmiterlimit = miterLimit;
update_pen(img);
}
CONVERT_IMAGE_END;
}

void getlinejoin(linejointype *linejoin, float *miterLimit, PIMAGE pimg)
{
PIMAGE img = CONVERT_IMAGE_CONST(pimg);
if (img && img->m_hDC) {
if (linejoin != NULL) {
*linejoin = img->m_linejoin;
}

if (miterLimit != NULL) {
*miterLimit = img->m_linejoinmiterlimit;
}
}
CONVERT_IMAGE_END
}

linejointype getlinejoin(PIMAGE pimg)
{
PIMAGE img = CONVERT_IMAGE_CONST(pimg);

if (img && img->m_hDC) {
return img->m_linejoin;
}
CONVERT_IMAGE_END;
return LINEJOIN_MITER;
}

void setfillstyle(int pattern, color_t color, PIMAGE pimg)
{
PIMAGE img = CONVERT_IMAGE_CONST(pimg);
Expand Down
2 changes: 2 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void IMAGE::reset()
m_linewidth = 0.0f;
m_linestartcap = LINECAP_FLAT;
m_lineendcap = LINECAP_FLAT;
m_linejoin = LINEJOIN_MITER;
m_linejoinmiterlimit = 10.0f;
m_texture = NULL;
#ifdef EGE_GDIPLUS
m_graphics = NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class IMAGE
float m_linewidth;
linecaptype m_linestartcap;
linecaptype m_lineendcap;
linejointype m_linejoin;
float m_linejoinmiterlimit;
void* m_texture;

private:
Expand Down

0 comments on commit eafc794

Please sign in to comment.