From fb92037ad37ed35dd38b60872c997cf0e6c9fb93 Mon Sep 17 00:00:00 2001 From: yixy <34703796+yixy-only@users.noreply.github.com> Date: Fri, 12 Jul 2024 22:52:22 +0800 Subject: [PATCH] =?UTF-8?q?Perf:=20=20=E6=8F=90=E9=AB=98=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E7=9A=84=E7=9B=B4=E7=BA=BF=E7=9A=84=E7=BB=98?= =?UTF-8?q?=E5=88=B6=E9=80=9F=E5=BA=A6=20(#214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ignore .history (generated by Local History extension) * perf: 提高默认样式的直线的绘制速度(PS_GEOMETRIC 笔类型的绘制速度极慢) * perf: 绘制直线时,如线型为 NULL_LINE 则跳过绘制只移动当前点 --- .gitignore | 1 + src/egegapi.cpp | 101 +++++++++++++++++++++++++++++++----------------- 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index ed85a74..cc44239 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs/ +.history *.tlog *.obj [Dd]ebug/ diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 4418b7d..902da7a 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -256,26 +256,44 @@ void moverel(int dx, int dy, PIMAGE pimg) void line(int x1, int y1, int x2, int y2, PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); - MoveToEx(img->m_hDC, x1, y1, NULL); - LineTo(img->m_hDC, x2, y2); + if (img) { + if (img->m_linestyle.linestyle != NULL_LINE) { + MoveToEx(img->m_hDC, x1, y1, NULL); + LineTo(img->m_hDC, x2, y2); + } else { + MoveToEx(img->m_hDC, x2, y2, NULL); + } + } CONVERT_IMAGE_END; } void linerel(int dx, int dy, PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); - POINT pt; - GetCurrentPositionEx(img->m_hDC, &pt); - dx += pt.x; - dy += pt.y; - LineTo(img->m_hDC, dx, dy); + if (img) { + POINT pt; + GetCurrentPositionEx(img->m_hDC, &pt); + dx += pt.x; + dy += pt.y; + if (img->m_linestyle.linestyle != NULL_LINE) { + LineTo(img->m_hDC, dx, dy); + } else { + MoveToEx(img->m_hDC, dx, dy, NULL); + } + } CONVERT_IMAGE_END; } void lineto(int x, int y, PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); - LineTo(img->m_hDC, x, y); + if (img) { + if (img->m_linestyle.linestyle != NULL_LINE) { + LineTo(img->m_hDC, x, y); + } else { + MoveToEx(img->m_hDC, x, y, NULL); + } + } CONVERT_IMAGE_END; } @@ -531,46 +549,59 @@ static int upattern2array(unsigned short pattern, DWORD style[]) static void update_pen(PIMAGE img) { - LOGBRUSH lbr; - lbr.lbColor = ARGBTOZBGR(img->m_linecolor); - lbr.lbStyle = BS_SOLID; - lbr.lbHatch = 0; - const int linestyle = img->m_linestyle.linestyle; const unsigned short pattern = img->m_linestyle.upattern; const int thickness = img->m_linestyle.thickness; - // 添加这些属性以获得正确的显示效果 - int ls = linestyle | PS_GEOMETRIC; + HPEN hpen; - switch (img->m_linestartcap) { - case LINECAP_FLAT : ls |= PS_ENDCAP_FLAT; break; - case LINECAP_ROUND: ls |= PS_ENDCAP_ROUND; break; - case LINECAP_SQUARE: ls |= PS_ENDCAP_SQUARE; break; - default: ls |= PS_ENDCAP_FLAT; break; - } + if ((thickness == 1) && ((linestyle == SOLID_LINE) || (linestyle == NULL_LINE))) { + LOGPEN logPen; + logPen.lopnStyle = linestyle; // Other styles may be drawn incorrectly + logPen.lopnWidth.x = 1; // Width + logPen.lopnWidth.y = 1; // Unuse + logPen.lopnColor = ARGBTOZBGR(img->m_linecolor); - 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; - } + hpen = CreatePenIndirect(&logPen); + } else { + unsigned int penStyle = linestyle; - SetMiterLimit(img->m_hDC, img->m_linejoinmiterlimit, NULL); + penStyle |= PS_GEOMETRIC; - HPEN hpen; - if (linestyle == USERBIT_LINE) { - DWORD style[20] = {0}; - int bn = upattern2array(pattern, style); - hpen = ExtCreatePen(ls, thickness, &lbr, bn, style); - } else { - hpen = ExtCreatePen(ls, thickness, &lbr, 0, NULL); + switch (img->m_linestartcap) { + case LINECAP_FLAT : penStyle |= PS_ENDCAP_FLAT; break; + case LINECAP_ROUND: penStyle |= PS_ENDCAP_ROUND; break; + case LINECAP_SQUARE: penStyle |= PS_ENDCAP_SQUARE; break; + default: penStyle |= PS_ENDCAP_FLAT; break; + } + + switch(img->m_linejoin) { + case LINEJOIN_MITER: penStyle |= PS_JOIN_MITER; break; + case LINEJOIN_BEVEL: penStyle |= PS_JOIN_BEVEL; break; + case LINEJOIN_ROUND: penStyle |= PS_JOIN_ROUND; break; + default: penStyle |= PS_JOIN_MITER; break; + } + + LOGBRUSH lbr; + lbr.lbColor = ARGBTOZBGR(img->m_linecolor); + lbr.lbStyle = BS_SOLID; + lbr.lbHatch = 0; + + if (linestyle == USERBIT_LINE) { + DWORD style[20] = {0}; + int bn = upattern2array(pattern, style); + hpen = ExtCreatePen(penStyle, thickness, &lbr, bn, style); + } else { + hpen = ExtCreatePen(penStyle, thickness, &lbr, 0, NULL); + } } + if (hpen) { DeleteObject(SelectObject(img->m_hDC, hpen)); } + SetMiterLimit(img->m_hDC, img->m_linejoinmiterlimit, NULL); + // why update pen not in IMAGE??? #ifdef EGE_GDIPLUS Gdiplus::Pen* pen = img->getPen();