Skip to content

Commit ac1d239

Browse files
committed
feat: 新增 INIT_EVENTLOOP 初始化模式,事件循环由主线程执行
1 parent e14b08e commit ac1d239

8 files changed

Lines changed: 88 additions & 32 deletions

File tree

demo/game_tetris.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static int g_map_mod[] = {1, 4, 4, 4, 2, 2, 2, 1, 0};
1717

1818
/* 初始化全局数据及图形显示 */
1919
void initgr() {
20-
initgraph(g_width, g_height);
20+
initgraph(g_width, g_height, INIT_RENDERMANUAL | INIT_EVENTLOOP);
2121
setfont(12, 6, "宋体");
2222
int Trs_map[8][4][4][4] =
2323
{
@@ -385,7 +385,7 @@ int main() {
385385
game.m_droptime = nfps / 2;
386386
game.m_movxtime = 10;
387387

388-
setrendermode(RENDER_MANUAL);
388+
//setrendermode(RENDER_MANUAL);
389389
for ( ; is_run(); delay_fps(nfps)) {
390390
game.update();
391391
game.render();

demo/test_doubleclick.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int mouseKeyToIndex(int keyMask)
1616
int main()
1717
{
1818
const int winWidth = 1200, winHeight = 300;
19-
initgraph(winWidth, winHeight, INIT_RENDERMANUAL);
19+
initgraph(winWidth, winHeight, INIT_RENDERMANUAL | INIT_EVENTLOOP);
2020
ege_enable_aa(true);
2121

2222
setlinecolor(EGEARGB(128, 81, 128, 222));

include/ege.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ enum initmode_flag
217217
INIT_UNICODE = 0x20, ///< Unicode character messages (equivalent to setunicodecharmessage(true))
218218
INIT_HIDE = 0x40, ///< Hidden window
219219
INIT_WITHLOGO = 0x100, ///< Show EGE Logo animation on startup (not shown by default in Debug version)
220+
INIT_EVENTLOOP = 0x200, ///< Event loop
221+
220222
INIT_ANIMATION = INIT_DEFAULT | INIT_RENDERMANUAL | INIT_NOFORCEEXIT ///< Animation mode
221223
};
222224

src/ege_graph.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ class egeControlBase; // 前置声明
1818

1919
initmode_flag getinitmode();
2020

21+
int initWindow(_graph_setting* pg);
22+
2123
void logoscene();
2224

25+
int messageHandle();
26+
2327
int dealmessage(_graph_setting* pg, bool force_update);
2428

2529
bool needToUpdate(_graph_setting* pg);
2630

2731
int graphupdate(_graph_setting* pg);
2832

33+
int graphInitOption();
34+
2935
void guiupdate(_graph_setting* pg, egeControlBase* root);
3036

3137
int waitdealmessage(_graph_setting* pg);

src/ege_head.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ struct _graph_setting
195195
std::wstring window_caption;
196196
HICON window_hicon;
197197
color_t window_initial_color;
198+
int init_option;
198199
int exit_flag;
199200
int exit_window;
200201
int update_mark_count; // 更新标记

src/egegapi.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,21 +1450,26 @@ void setfillstyle(int pattern, color_t color, PIMAGE pimg)
14501450

14511451
void setrendermode(rendermode_e mode)
14521452
{
1453+
struct _graph_setting* pg = &graph_setting;
1454+
/* INIT_EVENTLOOP 模式下,固定为 RENDER_MANUAL 模式*/
1455+
if (pg->init_option & INIT_EVENTLOOP) {
1456+
return;
1457+
}
1458+
14531459
if (mode == RENDER_MANUAL) {
1454-
struct _graph_setting* pg = &graph_setting;
14551460
if (pg->lock_window) {
14561461
;
14571462
} else {
14581463
KillTimer(pg->hwnd, RENDER_TIMER_ID);
14591464
pg->timer_stop_mark = true;
14601465
PostMessageW(pg->hwnd, WM_TIMER, RENDER_TIMER_ID, 0);
14611466
pg->lock_window = true;
1467+
14621468
while (pg->timer_stop_mark) {
14631469
::Sleep(1);
14641470
}
14651471
}
14661472
} else {
1467-
struct _graph_setting* pg = &graph_setting;
14681473
delay_ms(0);
14691474
SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL);
14701475
pg->skip_timer_mark = false;

src/graphics.cpp

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ unsigned long getlogodatasize();
9999
}
100100
#endif
101101

102-
DWORD WINAPI messageloopthread(LPVOID lpParameter);
102+
DWORD WINAPI messageLoopThread(LPVOID lpParameter);
103103

104104
_graph_setting::_graph_setting()
105105
{
@@ -285,10 +285,12 @@ void guiupdate(_graph_setting* pg, egeControlBase* root)
285285
root->update();
286286
}
287287

288-
/*private function*/
289288
int waitdealmessage(_graph_setting* pg)
290289
{
291-
// MSG msg;
290+
if (pg->init_option & INIT_EVENTLOOP) {
291+
messageHandle();
292+
}
293+
292294
if (pg->update_mark_count < UPDATE_MAX_CALL) {
293295
egeControlBase* root = pg->egectrl_root;
294296
root->draw(NULL);
@@ -857,6 +859,7 @@ void initgraph(int* gdriver, int* gmode, const char* path)
857859
{
858860
struct _graph_setting* pg = &graph_setting;
859861

862+
pg->init_option = getinitmode();
860863
pg->exit_flag = 0;
861864
pg->exit_window = 0;
862865

@@ -886,17 +889,22 @@ void initgraph(int* gdriver, int* gmode, const char* path)
886889
register_classW(pg, pg->instance);
887890

888891
// SECURITY_ATTRIBUTES sa = {0};
889-
DWORD pid;
890-
pg->threadui_handle = CreateThread(NULL, 0, messageloopthread, pg, CREATE_SUSPENDED, &pid);
891-
ResumeThread(pg->threadui_handle);
892892

893-
while (!pg->has_init) {
894-
::Sleep(1);
893+
if (pg->init_option & INIT_EVENTLOOP) {
894+
initWindow(pg);
895+
} else {
896+
DWORD pid;
897+
pg->threadui_handle = CreateThread(NULL, 0, messageLoopThread, pg, CREATE_SUSPENDED, &pid);
898+
ResumeThread(pg->threadui_handle);
899+
900+
while (!pg->has_init) {
901+
::Sleep(1);
902+
}
895903
}
896904

897905
UpdateWindow(pg->hwnd);
898906

899-
if (!(g_initoption & INIT_HIDE)) {
907+
if (!(pg->init_option & INIT_HIDE)) {
900908
ShowWindow(pg->hwnd, SW_SHOWNORMAL);
901909
BringWindowToTop(pg->hwnd);
902910
SetForegroundWindow(pg->hwnd);
@@ -914,11 +922,11 @@ void initgraph(int* gdriver, int* gmode, const char* path)
914922

915923
static egeControlBase _egeControlBase;
916924

917-
if ((g_initoption & INIT_WITHLOGO) && !(g_initoption & INIT_HIDE)) {
925+
if ((pg->init_option & INIT_WITHLOGO) && !(pg->init_option & INIT_HIDE)) {
918926
logoscene();
919927
}
920928

921-
if (g_initoption & INIT_RENDERMANUAL) {
929+
if (pg->init_option & INIT_RENDERMANUAL) {
922930
setrendermode(RENDER_MANUAL);
923931
}
924932

@@ -945,12 +953,8 @@ void closegraph()
945953
ShowWindow(pg->hwnd, SW_HIDE);
946954
}
947955

948-
/*private function*/
949-
DWORD WINAPI messageloopthread(LPVOID lpParameter)
956+
int initWindow(_graph_setting* pg)
950957
{
951-
_graph_setting* pg = (_graph_setting*)lpParameter;
952-
MSG msg;
953-
954958
/* 执行应用程序初始化: */
955959
if (!init_instance(pg->instance)) {
956960
return 0xFFFFFFFF;
@@ -963,25 +967,50 @@ DWORD WINAPI messageloopthread(LPVOID lpParameter)
963967

964968
pg->mouse_show = 0;
965969
pg->exit_flag = 0;
966-
pg->use_force_exit = (g_initoption & INIT_NOFORCEEXIT ? false : true);
970+
pg->use_force_exit = (pg->init_option & INIT_NOFORCEEXIT ? false : true);
967971

968-
if (g_initoption & INIT_NOFORCEEXIT) {
972+
if (pg->init_option & INIT_NOFORCEEXIT) {
969973
SetCloseHandler(DefCloseHandler);
970974
}
971975

972976
pg->close_manually = true;
973-
pg->skip_timer_mark = false;
974-
SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL);
977+
978+
/* 仅在 INIT_EVENTLOOP 模式下设置定时器(INIT_EVENTLOOP 模式下事件在主线程中处理,固定为手动渲染模式,无需设置定时器。 */
979+
if (pg->init_option & INIT_EVENTLOOP) {
980+
pg->lock_window = true;
981+
pg->skip_timer_mark = true;
982+
} else {
983+
pg->skip_timer_mark = false;
984+
SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL);
985+
}
975986

976987
pg->has_init = true;
988+
}
977989

978-
while (!pg->exit_window) {
979-
if (GetMessageW(&msg, NULL, 0, 0)) {
980-
TranslateMessage(&msg);
981-
DispatchMessageW(&msg);
982-
} else {
983-
Sleep(1);
990+
int messageHandle()
991+
{
992+
MSG msg;
993+
int handleCount = 0;
994+
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
995+
if (msg.message == WM_QUIT) {
996+
return -1;
984997
}
998+
999+
TranslateMessage(&msg);
1000+
DispatchMessageW(&msg);
1001+
handleCount++;
1002+
}
1003+
1004+
return handleCount;
1005+
}
1006+
1007+
/*private function*/
1008+
DWORD WINAPI messageLoopThread(LPVOID lpParameter)
1009+
{
1010+
_graph_setting* pg = (_graph_setting*)lpParameter;
1011+
initWindow(pg);
1012+
while (messageHandle() != -1) {
1013+
Sleep(1);
9851014
}
9861015

9871016
return 0;
@@ -1054,7 +1083,7 @@ BOOL init_instance(HINSTANCE hInstance)
10541083
//DeleteObject(hfont);
10551084
} //*/
10561085

1057-
if (!(g_initoption & INIT_HIDE)) {
1086+
if (!(pg->init_option & INIT_HIDE)) {
10581087
SetActiveWindow(pg->hwnd);
10591088
}
10601089

src/time.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ void delay_ms(long ms)
7272

7373
const double targetTime = get_highfeq_time_ls(pg) * 1000.0 + ms;
7474

75+
if (pg->init_option & INIT_EVENTLOOP) {
76+
messageHandle();
77+
}
78+
7579
/* 处理 UI 事件,更新 UI 控件数据 */
7680
guiupdate(pg, root);
7781

@@ -129,6 +133,10 @@ void delay_fps(double fps)
129133
dw = pg->delay_fps_dwLast;
130134
}
131135

136+
if (pg->init_option & INIT_EVENTLOOP) {
137+
messageHandle();
138+
}
139+
132140
root->draw(NULL);
133141

134142
for (; nloop >= 0; --nloop) {
@@ -182,6 +190,11 @@ void delay_jfps(double fps)
182190
dw = pg->delay_fps_dwLast;
183191
}
184192

193+
194+
if (pg->init_option & INIT_EVENTLOOP) {
195+
messageHandle();
196+
}
197+
185198
root->draw(NULL);
186199

187200
for (; nloop >= 0; --nloop) {

0 commit comments

Comments
 (0)