Skip to content

Commit

Permalink
adjust: 调整初始化环境之前所返回的颜色值,以及允许预先设置窗口背景色 (#253)
Browse files Browse the repository at this point in the history
* refactor: 重构 setbkcolor
* adjust: 调整初始化环境前所返回的颜色值,以及允许预先设置窗口背景色
  • Loading branch information
yixy-only authored Dec 28, 2024
1 parent 9f16bbe commit 57d518e
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 60 deletions.
3 changes: 3 additions & 0 deletions src/ege_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ int swapbuffers();

bool isinitialized();

void replacePixels(PIMAGE pimg, color_t src, color_t dst, bool ignoreAlpha = false);


}
4 changes: 3 additions & 1 deletion src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 更新标记
Expand Down Expand Up @@ -235,7 +236,8 @@ struct _graph_setting
/* 函数用临时缓冲区 */
DWORD g_t_buff[1024 * 8];

_graph_setting() { window_caption = EGE_TITLE_W; }
public:
_graph_setting();
};

template <typename T> struct count_ptr
Expand Down
59 changes: 33 additions & 26 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ color_t getlinecolor(PCIMAGE pimg)
return img->m_linecolor;
}
CONVERT_IMAGE_END;
return 0xFFFFFFFF;
return IMAGE::initial_line_color;
}

// 将描述线形的位模式转换为 style 数组
Expand Down Expand Up @@ -662,58 +662,63 @@ color_t getfillcolor(PCIMAGE pimg)
return img->m_fillcolor;
}
CONVERT_IMAGE_END;
return 0xFFFFFFFF;
return IMAGE::initial_fill_color;
}

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)
{
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)
{
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;
}

Expand Down Expand Up @@ -1473,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];
Expand All @@ -1488,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;
}
Expand Down
75 changes: 52 additions & 23 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());

/* 裁剪区域设置 */
Expand All @@ -1078,4 +1082,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
46 changes: 39 additions & 7 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions src/image.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "ege_head.h"

#include <windows.h>


Expand All @@ -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:
Expand All @@ -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();
Expand All @@ -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; }
Expand Down

0 comments on commit 57d518e

Please sign in to comment.