From 9dd8de73c7c8a1023ff54680a54ba170ab885c48 Mon Sep 17 00:00:00 2001 From: yixy-only Date: Thu, 26 Dec 2024 22:30:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20setbkcolor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ege_graph.h | 3 +++ src/egegapi.cpp | 18 +++--------------- src/graphics.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/ege_graph.h b/src/ege_graph.h index c2f2bc9..ef176a4 100644 --- a/src/ege_graph.h +++ b/src/ege_graph.h @@ -43,4 +43,7 @@ int swapbuffers(); bool isinitialized(); +void replacePixels(PIMAGE pimg, color_t src, color_t dst, bool ignoreAlpha = false); + + } diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 8f9b988..afc9d28 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -689,21 +689,9 @@ color_t gettextcolor(PCIMAGE pimg) void setbkcolor(color_t color, PIMAGE pimg) { - PIMAGE img = CONVERT_IMAGE(pimg); - - if (img && img->m_hDC) { - PDWORD p = img->m_pBuffer; - int size = img->m_width * img->m_height; - color_t col = img->m_bk_color; - img->m_bk_color = color; - SetBkColor(img->m_hDC, ARGBTOZBGR(color)); - for (int n = 0; n < size; n++, p++) { - if (*p == col) { - *p = color; - } - } - } - CONVERT_IMAGE_END; + color_t oldBkColor = getbkcolor(pimg); + setbkcolor_f(color, pimg); + replacePixels(pimg, oldBkColor, color); } void setbkcolor_f(color_t color, PIMAGE pimg) diff --git a/src/graphics.cpp b/src/graphics.cpp index 18aca33..640ea07 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -1078,4 +1078,29 @@ Gdiplus::Graphics* recreateGdiplusGraphics(HDC hdc, const Gdiplus::Graphics* old return newGraphics; } +void replacePixels(PIMAGE pimg, color_t src, color_t dst, bool ignoreAlpha) +{ + PIMAGE img = CONVERT_IMAGE(pimg); + if (img && img->m_hDC) { + color_t* bufferBegin = img->getbuffer(); + const color_t* bufferEnd = bufferBegin + img->m_width * img->m_height; + + if (ignoreAlpha) { + for (color_t* itor = bufferBegin; itor != bufferEnd; ++itor) { + if ((*itor & 0x00FFFFFF) == (src & 0x00FFFFFF)) { + *itor = dst; + } + } + } else { + for (color_t* itor = bufferBegin; itor != bufferEnd; ++itor) { + if (*itor == src) { + *itor = dst; + } + } + } + } + + CONVERT_IMAGE_END +} + } // namespace ege From c0b5448bcdab29e05727b1c56b5582b10d81f7a1 Mon Sep 17 00:00:00 2001 From: yixy-only Date: Thu, 26 Dec 2024 23:18:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?adjust:=20=E8=B0=83=E6=95=B4=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E7=8E=AF=E5=A2=83=E5=89=8D=E6=89=80=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=9A=84=E9=A2=9C=E8=89=B2=E5=80=BC=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E5=85=81=E8=AE=B8=E9=A2=84=E5=85=88=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E8=83=8C=E6=99=AF=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ege_head.h | 4 +++- src/egegapi.cpp | 41 ++++++++++++++++++++++++++++----------- src/graphics.cpp | 50 ++++++++++++++++++++++++++---------------------- src/image.cpp | 46 +++++++++++++++++++++++++++++++++++++------- src/image.h | 16 +++++++++++++--- 5 files changed, 112 insertions(+), 45 deletions(-) diff --git a/src/ege_head.h b/src/ege_head.h index 5b8ef26..74aef25 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -186,6 +186,7 @@ struct _graph_setting HWND hwnd; std::wstring window_caption; HICON window_hicon; + color_t window_initial_color; int exit_flag; int exit_window; int update_mark_count; // 更新标记 @@ -235,7 +236,8 @@ struct _graph_setting /* 函数用临时缓冲区 */ DWORD g_t_buff[1024 * 8]; - _graph_setting() { window_caption = EGE_TITLE_W; } +public: + _graph_setting(); }; template struct count_ptr diff --git a/src/egegapi.cpp b/src/egegapi.cpp index afc9d28..3907f47 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -518,7 +518,7 @@ color_t getlinecolor(PCIMAGE pimg) return img->m_linecolor; } CONVERT_IMAGE_END; - return 0xFFFFFFFF; + return IMAGE::initial_line_color; } // 将描述线形的位模式转换为 style 数组 @@ -662,7 +662,7 @@ color_t getfillcolor(PCIMAGE pimg) return img->m_fillcolor; } CONVERT_IMAGE_END; - return 0xFFFFFFFF; + return IMAGE::initial_fill_color; } color_t getbkcolor(PCIMAGE pimg) @@ -670,21 +670,30 @@ color_t getbkcolor(PCIMAGE pimg) PCIMAGE img = CONVERT_IMAGE_CONST(pimg); if (img) { - return img->m_bk_color; + if (img->m_hDC) { + return img->m_bk_color; + } + } else { + _graph_setting* pg = &graph_setting; + if (!pg->has_init) { + return pg->window_initial_color; + } } + CONVERT_IMAGE_END; - return 0xFFFFFFFF; + + return IMAGE::initial_bk_color; } color_t gettextcolor(PCIMAGE pimg) { PCIMAGE img = CONVERT_IMAGE_CONST(pimg); - if (img) { + if (img && img->m_hDC) { return img->m_textcolor; } CONVERT_IMAGE_END; - return 0xFFFFFFFF; + return IMAGE::initial_text_color; } void setbkcolor(color_t color, PIMAGE pimg) @@ -698,10 +707,18 @@ void setbkcolor_f(color_t color, PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); - if (img && img->m_hDC) { - img->m_bk_color = color; - SetBkColor(img->m_hDC, ARGBTOZBGR(color)); + if (img) { + if (img->m_hDC) { + img->m_bk_color = color; + SetBkColor(img->m_hDC, ARGBTOZBGR(color)); + } + } else { + _graph_setting* pg = &graph_setting; + if (!pg->has_init) { + pg->window_initial_color = color; + } } + CONVERT_IMAGE_END; } @@ -1461,8 +1478,10 @@ void setactivepage(int page) if (0 <= page && page < BITMAP_PAGE_SIZE) { pg->active_page = page; + /* 为未创建的绘图页分配图像 */ if (pg->img_page[page] == NULL) { - pg->img_page[page] = new IMAGE(pg->dc_w, pg->dc_h); + color_t bkColor = (page == 0) ? pg->window_initial_color : BLACK; + pg->img_page[page] = new IMAGE(pg->dc_w, pg->dc_h, bkColor); } pg->imgtarget = pg->img_page[page]; @@ -1476,7 +1495,7 @@ void setvisualpage(int page) if (0 <= page && page < BITMAP_PAGE_SIZE) { pg->visual_page = page; if (pg->img_page[page] == NULL) { - pg->img_page[page] = new IMAGE(pg->dc_w, pg->dc_h); + pg->img_page[page] = new IMAGE(pg->dc_w, pg->dc_h, BLACK); } pg->update_mark_count = 0; } diff --git a/src/graphics.cpp b/src/graphics.cpp index 640ea07..749a4fb 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -101,6 +101,12 @@ unsigned long getlogodatasize(); DWORD WINAPI messageloopthread(LPVOID lpParameter); +_graph_setting::_graph_setting() +{ + window_caption = EGE_TITLE_W; + window_initial_color = IMAGE::initial_bk_color; +} + /*private function*/ static void ui_msg_process(EGEMSG& qmsg) { @@ -873,31 +879,29 @@ DWORD WINAPI messageloopthread(LPVOID lpParameter) { _graph_setting* pg = (_graph_setting*)lpParameter; MSG msg; - { - /* 执行应用程序初始化: */ - if (!init_instance(pg->instance)) { - return 0xFFFFFFFF; - } - // 图形初始化 - if (pg->dc == 0) { - graph_init(pg); - } + /* 执行应用程序初始化: */ + if (!init_instance(pg->instance)) { + return 0xFFFFFFFF; + } - { - pg->mouse_show = 0; - pg->exit_flag = 0; - pg->use_force_exit = (g_initoption & INIT_NOFORCEEXIT ? false : true); - if (g_initoption & INIT_NOFORCEEXIT) { - SetCloseHandler(DefCloseHandler); - } - pg->close_manually = true; - } - { - pg->skip_timer_mark = false; - SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL); - } + // 图形初始化 + if (pg->dc == 0) { + graph_init(pg); + } + + pg->mouse_show = 0; + pg->exit_flag = 0; + pg->use_force_exit = (g_initoption & INIT_NOFORCEEXIT ? false : true); + + if (g_initoption & INIT_NOFORCEEXIT) { + SetCloseHandler(DefCloseHandler); } + + pg->close_manually = true; + pg->skip_timer_mark = false; + SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL); + pg->has_init = true; while (!pg->exit_window) { @@ -1057,7 +1061,7 @@ Gdiplus::Graphics* recreateGdiplusGraphics(HDC hdc, const Gdiplus::Graphics* old newGraphics->SetCompositingQuality(oldGraphics->GetCompositingQuality()); newGraphics->SetTextContrast(oldGraphics->GetTextContrast()); - /* 组合模式设置*/ + /* 组合模式设置 */ newGraphics->SetCompositingMode(oldGraphics->GetCompositingMode()); /* 裁剪区域设置 */ diff --git a/src/image.cpp b/src/image.cpp index 03bb6f7..b163b74 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -60,6 +60,12 @@ void IMAGE::reset() #endif } +/** + * 构造宽高为 width x height 的图像。 + * @param width + * @param height + * @note: 创建的图像内容未定义,但经检测像素值均为 0。 + */ void IMAGE::construct(int width, int height) { HDC refDC = NULL; @@ -79,9 +85,22 @@ void IMAGE::construct(int width, int height) } } +/** + * 构造宽高为 width x height 的图像,将 color 设为背景色并以 color 填充整个图像。 + * @param width + * @param height + * @param color + */ +void IMAGE::construct(int width, int height, color_t color) +{ + construct(width, height); + setbkcolor_f(color, this); + cleardevice(this); +} + IMAGE::IMAGE() { - construct(1, 1); + construct(1, 1, BLACK); } IMAGE::IMAGE(int width, int height) @@ -96,6 +115,18 @@ IMAGE::IMAGE(int width, int height) construct(width, height); } +IMAGE::IMAGE(int width, int height, color_t color) +{ + // 截止到 0 + if (width < 0) { + width = 0; + } + if (height < 0) { + height = 0; + } + construct(width, height, color); +} + IMAGE::IMAGE(const IMAGE& img) { reset(); @@ -223,10 +254,11 @@ void IMAGE::initimage(HDC refDC, int width, int height) void IMAGE::setdefaultattribute() { - setcolor(LIGHTGRAY, this); - setbkcolor(BLACK, this); - SetBkMode(m_hDC, OPAQUE); // TRANSPARENT); - setfillstyle(SOLID_FILL, 0, this); + setlinecolor(initial_line_color, this); + settextcolor(initial_text_color, this); + setbkcolor_f(initial_bk_color, this); + SetBkMode(m_hDC, OPAQUE); + setfillstyle(SOLID_FILL, initial_fill_color, this); setlinestyle(PS_SOLID, 0, 1, this); settextjustify(LEFT_TEXT, TOP_TEXT, this); setfont(16, 0, "SimSun", this); @@ -2774,7 +2806,7 @@ int gety(PCIMAGE pimg) PIMAGE newimage() { - return new IMAGE(1, 1); + return new IMAGE(1, 1, BLACK); } PIMAGE newimage(int width, int height) @@ -2785,7 +2817,7 @@ PIMAGE newimage(int width, int height) if (height < 1) { height = 1; } - return new IMAGE(width, height); + return new IMAGE(width, height, BLACK); } void delimage(PCIMAGE pImg) diff --git a/src/image.h b/src/image.h index d7d1f65..61bd2d9 100644 --- a/src/image.h +++ b/src/image.h @@ -1,6 +1,7 @@ #pragma once #include "ege_head.h" + #include @@ -10,6 +11,13 @@ namespace ege // 定义图像对象 class IMAGE { +public: + /* 初始颜色配置 */ + static const color_t initial_line_color = LIGHTGRAY; + static const color_t initial_text_color = LIGHTGRAY; + static const color_t initial_fill_color = BLACK; + static const color_t initial_bk_color = BLACK; +private: int m_initflag; public: @@ -32,6 +40,7 @@ class IMAGE bool m_aa; void initimage(HDC refDC, int width, int height); void construct(int width, int height); + void construct(int width, int height, color_t color); void setdefaultattribute(); int deleteimage(); void reset(); @@ -53,12 +62,13 @@ class IMAGE public: IMAGE(); IMAGE(int width, int height); - IMAGE(const IMAGE& img); // 拷贝构造函数 - IMAGE& operator=(const IMAGE& img); // 赋值运算符重载函数 + IMAGE(int width, int height, color_t color); + IMAGE(const IMAGE& img); + IMAGE& operator=(const IMAGE& img); ~IMAGE(); + void gentexture(bool gen); -public: HDC getdc() const { return m_hDC; } int getwidth() const { return m_width; } int getheight() const { return m_height; }