-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathgraphics.cpp
More file actions
1227 lines (1060 loc) · 35.4 KB
/
graphics.cpp
File metadata and controls
1227 lines (1060 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* EGE (Easy Graphics Engine)
* FileName graphics.cpp
* HomePage1 http://misakamm.github.com/xege
* HomePage2 http://misakamm.bitbucket.org/index.htm
* teiba1 http://tieba.baidu.com/f?kw=ege
* teiba2 http://tieba.baidu.com/f?kw=ege%C4%EF
* Blog: http://misakamm.com
* E-Mail: mailto:misakamm[at gmail com]
编译说明:编译为动态库时,需要定义 PNG_BULIDDLL,以导出dll函数
本图形库创建时间2010 0916
本文件定义平台密切相关的操作及接口
*/
// 整个项目和其他源文件中不需要定义 UNICODE 宏, 这里是为了解决 VC6 下 initicon 中代码的编译问题加的
#define UNICODE 1
#ifndef _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
#define _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
#endif
#ifndef _ALLOW_RUNTIME_LIBRARY_MISMATCH
#define _ALLOW_RUNTIME_LIBRARY_MISMATCH
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifndef _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_NON_CONFORMING_SWPRINTFS
#endif
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windowsx.h>
#include "ege_head.h"
#include "ege_common.h"
#include "ege_extension.h"
#ifdef _ITERATOR_DEBUG_LEVEL
#undef _ITERATOR_DEBUG_LEVEL
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4100 4127 4706)
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "Imm32.lib")
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib, "Winmm.lib")
#ifdef EGE_GDIPLUS
#if _MSC_VER > 1200
#pragma comment(lib, "gdiplus.lib")
#endif
#endif
#endif
// VC6 compatible
#ifndef IS_LOW_SURROGATE
#define IS_LOW_SURROGATE(wch) (((wch) >= 0xdc00) && ((wch) <= 0xdfff))
#endif
#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(wch) (((wch) >= 0xd800) && ((wch) <= 0xdbff))
#endif
namespace ege
{
// 静态分配,零初始化
struct _graph_setting graph_setting;
static initmode_flag g_initoption = INIT_DEFAULT;
static DWORD g_windowstyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_VISIBLE;
static DWORD g_windowexstyle = WS_EX_LEFT | WS_EX_LTRREADING;
static int g_windowpos_x = CW_USEDEFAULT;
static int g_windowpos_y = CW_USEDEFAULT;
#ifdef __cplusplus
extern "C"
{
#endif
char* getlogodata();
unsigned long getlogodatasize();
#ifdef __cplusplus
}
#endif
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)
{
struct _graph_setting* pg = &graph_setting;
if ((qmsg.flag & 1)) {
return;
}
qmsg.flag |= 1;
if (qmsg.message >= WM_KEYFIRST && qmsg.message <= WM_KEYLAST) {
if (qmsg.message == WM_KEYDOWN) {
pg->egectrl_root->keymsgdown((unsigned)qmsg.wParam, 0); // 以后补加flag
} else if (qmsg.message == WM_KEYUP) {
pg->egectrl_root->keymsgup((unsigned)qmsg.wParam, 0); // 以后补加flag
} else if (qmsg.message == WM_CHAR) {
pg->egectrl_root->keymsgchar((unsigned)qmsg.wParam, 0); // 以后补加flag
}
} else if (qmsg.message >= WM_MOUSEFIRST && qmsg.message <= WM_MOUSELAST) {
int x = (short int)((UINT)qmsg.lParam & 0xFFFF), y = (short int)((UINT)qmsg.lParam >> 16);
if (qmsg.message == WM_LBUTTONDOWN) {
pg->egectrl_root->mouse(x, y, mouse_msg_down | mouse_flag_left);
} else if (qmsg.message == WM_LBUTTONUP) {
pg->egectrl_root->mouse(x, y, mouse_msg_up | mouse_flag_left);
} else if (qmsg.message == WM_RBUTTONDOWN) {
pg->egectrl_root->mouse(x, y, mouse_msg_down | mouse_flag_right);
} else if (qmsg.message == WM_RBUTTONUP) {
pg->egectrl_root->mouse(x, y, mouse_msg_up | mouse_flag_right);
} else if (qmsg.message == WM_MOUSEMOVE) {
int flag = 0;
if (pg->keystatemap[VK_LBUTTON]) {
flag |= mouse_flag_left;
}
if (pg->keystatemap[VK_RBUTTON]) {
flag |= mouse_flag_right;
}
pg->egectrl_root->mouse(x, y, mouse_msg_move | flag);
}
}
}
/*private function*/
/*
static int redraw_window(_graph_setting* pg, HDC dc)
{
int page = pg->visual_page;
HDC hDC = pg->img_page[page]->m_hDC;
int left = pg->img_page[page]->m_vpt.left, top = pg->img_page[page]->m_vpt.top;
// HRGN rgn = pg->img_page[page]->m_rgn;
BitBlt(dc, 0, 0, pg->base_w, pg->base_h, hDC, pg->base_x - left, pg->base_y - top, SRCCOPY);
pg->update_mark_count = UPDATE_MAX_CALL;
return 0;
}
*/
/**
* @brief 将后台帧缓冲中指定区域的内容复制到前台帧缓冲指定区域
* @param frontDC 前台帧缓冲设备句柄
* @param frontPoint 前台帧缓冲指定区域的左上角坐标(设备坐标)
* @param backDC 后台帧缓冲设备句柄
* @param rect 后台帧缓冲中要复制内容的区域(设备坐标)
* @return 错误码
* @warning 内部使用 BitBlt 进行复制,会受 GDI 坐标变换和视口原点影响,旋转和剪切变换会发生错误。
*/
int frameBufferCopy(HDC frontDC, const Point& frontPoint, HDC backDC, const Rect& rect)
{
/* Note: BitBlt 参数指定的位置受 GDI 坐标变换和视口原点影响 */
/* 保存影响 BitBlt 的设置 */
POINT oldViewportOrigin, oldWindowOrigin;
SetViewportOrgEx(backDC, 0, 0, &oldViewportOrigin);
SetWindowOrgEx(backDC, 0, 0, &oldWindowOrigin);
int oldMapMode = SetMapMode(backDC, MM_TEXT);
XFORM xform;
int oldGraphicsMode = GetGraphicsMode(backDC);
if (oldGraphicsMode == GM_ADVANCED) {
GetWorldTransform(backDC, &xform);
ModifyWorldTransform(backDC, NULL, MWT_IDENTITY);
SetGraphicsMode(backDC, GM_COMPATIBLE);
}
bool copyResult = BitBlt(frontDC, frontPoint.x, frontPoint.y, rect.width, rect.height, backDC, rect.x, rect.y, SRCCOPY);
/* 恢复之前的设置 */
SetViewportOrgEx(backDC, oldViewportOrigin.x, oldViewportOrigin.y, NULL);
SetWindowOrgEx(backDC, oldWindowOrigin.x, oldWindowOrigin.y, NULL);
SetMapMode(backDC, oldMapMode);
if (oldGraphicsMode == GM_ADVANCED) {
SetGraphicsMode(backDC, oldGraphicsMode);
SetWorldTransform(backDC, &xform);
}
return copyResult ? grOk : grError;
}
int swapbuffers()
{
if (!isinitialized())
return grNoInitGraph;
struct _graph_setting* pg = &graph_setting;
PIMAGE backFrameBuffer = pg->img_page[pg->visual_page];
HDC backFrameBufferDC = backFrameBuffer->getdc();
HDC frontFrameBufferDC = GetDC(getHWnd());
Rect backRect(0, 0, pg->base_w, pg->base_h);
frameBufferCopy(frontFrameBufferDC, Point(0, 0), backFrameBufferDC, backRect);
ReleaseDC(getHWnd(), frontFrameBufferDC);
return grOk;
}
bool needToUpdate(_graph_setting* pg)
{
return (pg != NULL) && (pg->update_mark_count < UPDATE_MAX_CALL);
}
int graphupdate(_graph_setting* pg)
{
if (pg->exit_window) {
return grNoInitGraph;
}
if (IsWindowVisible(pg->hwnd)) {
swapbuffers();
updateFrameRate();
} else {
updateFrameRate(false);
}
pg->update_mark_count = UPDATE_MAX_CALL;
RECT rect, crect;
HWND hwnd;
int _dw, _dh;
GetClientRect(pg->hwnd, &crect);
GetWindowRect(pg->hwnd, &rect);
int w = pg->dc_w, h = pg->dc_h;
_dw = w - (crect.right - crect.left);
_dh = h - (crect.bottom - crect.top);
if (_dw != 0 || _dh != 0) {
hwnd = ::GetParent(pg->hwnd);
if (hwnd) {
POINT pt = {0, 0};
ClientToScreen(hwnd, &pt);
rect.left -= pt.x;
rect.top -= pt.y;
rect.right -= pt.x;
rect.bottom -= pt.y;
}
SetWindowPos(pg->hwnd, NULL, 0, 0, rect.right + _dw - rect.left, rect.bottom + _dh - rect.top,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}
return grOk;
}
int dealmessage(_graph_setting* pg, bool force_update)
{
if (force_update || pg->update_mark_count < UPDATE_MAX_CALL) {
graphupdate(pg);
}
return !pg->exit_window;
}
/*private function*/
void guiupdate(_graph_setting* pg, egeControlBase* root)
{
pg->msgkey_queue->process(ui_msg_process);
pg->msgmouse_queue->process(ui_msg_process);
root->update();
}
int waitdealmessage(_graph_setting* pg)
{
if (pg->init_option & INIT_EVENTLOOP) {
messageHandle();
}
if (pg->update_mark_count < UPDATE_MAX_CALL) {
egeControlBase* root = pg->egectrl_root;
root->draw(NULL);
graphupdate(pg);
guiupdate(pg, root);
}
ege_sleep(1);
return !pg->exit_window;
}
/*private function*/
void setmode(int gdriver, int gmode)
{
struct _graph_setting* pg = &graph_setting;
if (gdriver == TRUECOLORSIZE) {
RECT rect;
HWND parentWindow = getParentWindow();
if (parentWindow) {
GetClientRect(parentWindow, &rect);
} else {
GetWindowRect(GetDesktopWindow(), &rect);
}
pg->dc_w = (short)(gmode & 0xFFFF);
pg->dc_h = (short)((unsigned int)gmode >> 16);
if (pg->dc_w < 0) {
pg->dc_w = rect.right - rect.left;
}
if (pg->dc_h < 0) {
pg->dc_h = rect.bottom - rect.top;
}
} else {
pg->dc_w = 640;
pg->dc_h = 480;
}
}
/*private callback function*/
static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
{
HICON hico = (HICON)LoadImageW(hModule, lpszName, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
if (hico) {
*((HICON*)lParam) = hico;
return FALSE;
}
return TRUE;
}
void DefCloseHandler()
{
struct _graph_setting* pg = &graph_setting;
pg->exit_flag = 1;
}
/*private function*/
static void on_repaint(struct _graph_setting* pg, HWND hwnd, HDC dc)
{
int page = pg->visual_page;
bool release = false;
pg->img_timer_update->copyimage(pg->img_page[page]);
if (dc == NULL) {
dc = GetDC(hwnd);
release = true;
}
frameBufferCopy(dc, Point(0, 0), pg->img_timer_update->m_hDC, Rect(0, 0, pg->base_w, pg->base_h));
if (release) {
ReleaseDC(hwnd, dc);
}
}
/*private function*/
static void on_timer(struct _graph_setting* pg, HWND hwnd, unsigned id)
{
if (!pg->skip_timer_mark && id == RENDER_TIMER_ID) {
if (pg->update_mark_count < UPDATE_MAX_CALL) {
pg->update_mark_count = UPDATE_MAX_CALL;
on_repaint(pg, hwnd, NULL);
}
if (pg->timer_stop_mark) {
pg->timer_stop_mark = false;
pg->skip_timer_mark = true;
}
}
}
/*private function*/
static void on_paint(struct _graph_setting* pg, HWND hwnd)
{
if (!pg->lock_window) {
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
on_repaint(pg, hwnd, hdc);
EndPaint(hwnd, &ps);
} else {
ValidateRect(hwnd, NULL);
pg->update_mark_count--;
}
}
/*private function*/
static void on_destroy(struct _graph_setting* pg)
{
pg->exit_window = 1;
dll::freeDlls();
PostQuitMessage(0);
if (pg->close_manually && pg->use_force_exit) {
exit(0);
}
}
/*private function*/
static void on_setcursor(struct _graph_setting* pg, HWND hwnd)
{
if (pg->mouse_show) {
SetCursor(LoadCursor(NULL, IDC_ARROW));
} else {
RECT rect;
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
GetClientRect(hwnd, &rect);
if (pt.x >= rect.left && pt.x < rect.right && pt.y >= rect.top && pt.y <= rect.bottom) {
SetCursor(NULL);
} else {
SetCursor(LoadCursor(NULL, IDC_ARROW));
}
}
}
/*private function*/
static void on_ime_control(struct _graph_setting* pg, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
if (wparam == IMC_SETSTATUSWINDOWPOS) {
HIMC hImc = dll::ImmGetContext(hwnd);
COMPOSITIONFORM cpf = {0};
cpf.dwStyle = CFS_POINT;
cpf.ptCurrentPos = *(LPPOINT)lparam;
dll::ImmSetCompositionWindow(hImc, &cpf);
}
}
/*private function*/
static void windowmanager(ege::_graph_setting* pg, bool create, struct msg_createwindow* msg)
{
if (create) {
msg->hwnd = ::CreateWindowExW(msg->exstyle, msg->classname, NULL, msg->style, 0, 0, 0, 0, getHWnd(),
(HMENU)msg->id, getHInstance(), NULL);
if (msg->hEvent) {
::SetEvent(msg->hEvent);
}
} else {
if (msg->hwnd) {
::DestroyWindow(msg->hwnd);
}
if (msg->hEvent) {
::SetEvent(msg->hEvent);
}
}
}
/*private function*/
static void on_key(struct _graph_setting* pg, UINT message, unsigned long keycode, LPARAM keyflag)
{
/* https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-keydown */
unsigned msg = 0;
if (message == WM_KEYDOWN && keycode < MAX_KEY_VCODE) {
msg = 1;
pg->keystatemap[keycode] = true;
/* 按键按下计数,LPARAM 参数第 30 位为 0 代表消息发送前按键状态为 UP */
if ((keyflag & 0x40000000) == 0) {
/* 按键按下时初次发送的消息*/
if (pg->key_press_count[keycode] < UINT16_MAX) {
pg->key_press_count[keycode]++;
}
} else {
/* 按键长按时重复发送的消息 */
if (pg->key_repeat_count[keycode] < UINT16_MAX) {
int repeatCount = keyflag & 0xFFFF; /* 重复次数 */
pg->key_repeat_count[keycode] += repeatCount;
}
}
}
if (message == WM_KEYUP && keycode < MAX_KEY_VCODE) {
pg->keystatemap[keycode] = false;
if (pg->key_release_count[keycode] < UINT16_MAX) {
pg->key_release_count[keycode]++;
}
}
if (pg->callback_key) {
int ret;
if (message == WM_CHAR) {
msg = 2;
}
ret = pg->callback_key(pg->callback_key_param, msg, (int)keycode);
if (ret == 0) {
return;
}
}
{
EGEMSG msg = {0};
msg.hwnd = pg->hwnd;
msg.message = message;
msg.wParam = keycode;
msg.lParam = keyflag;
msg.time = ::GetTickCount();
pg->msgkey_queue->push(msg);
}
}
/*private function*/
static void push_mouse_msg(struct _graph_setting* pg, UINT message, WPARAM wparam, LPARAM lparam, int time)
{
EGEMSG msg = {0};
msg.hwnd = pg->hwnd;
msg.message = message;
msg.wParam = wparam;
msg.lParam = lparam;
msg.mousekey |= pg->keystatemap[VK_LBUTTON] ? mouse_flag_left : 0;
msg.mousekey |= pg->keystatemap[VK_RBUTTON] ? mouse_flag_right : 0;
msg.mousekey |= pg->keystatemap[VK_MBUTTON] ? mouse_flag_mid : 0;
msg.mousekey |= pg->keystatemap[VK_XBUTTON1] ? mouse_flag_x1 : 0;
msg.mousekey |= pg->keystatemap[VK_XBUTTON2] ? mouse_flag_x2 : 0;
msg.time = time;
pg->msgmouse_queue->push(msg);
}
static void mouseProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/* up 消息会后紧跟一条 move 消息,标记并将其忽略 */
static bool skipNextMoveMessage = false;
if ((message < WM_MOUSEFIRST) || (message > WM_MOUSELAST))
return;
_graph_setting* pg = &graph_setting;
bool curMsgIsNeedToPush = true;
int key = 0;
/* WINAPI bug: WM_MOUSEWHEEL 提供的是屏幕坐标,DPI 不等于 100% 时 ScreenToClient 的计算
* 结果与其它鼠标消息提供的坐标不一致,故忽略 lParam 提供的值,直接使用之前记录的客户区坐标
*/
if (message == WM_MOUSEWHEEL) {
lParam = MAKELPARAM(pg->mouse_pos.x, pg->mouse_pos.y);
}
mouse_msg msg = mouseMessageConvert(message, wParam, lParam, &key);
Point curPos(msg.x, msg.y);
if (msg.is_up()) {
skipNextMoveMessage = true;
} else if (msg.is_move()) {
/* 忽略 up 消息后伴随的同位置 move 消息 */
if (skipNextMoveMessage && (curPos == pg->mouse_pos)) {
curMsgIsNeedToPush = false;
skipNextMoveMessage = false;
}
}
/* 鼠标按键动作 */
if (key != 0) {
/* 鼠标按键状态更新*/
pg->keystatemap[key] = msg.is_down();
/* 鼠标按键计数 */
uint16_t* keyCountArray = msg.is_down() ? pg->key_press_count : pg->key_release_count;
if (keyCountArray[key] < UINT16_MAX) {
keyCountArray[key]++;
}
/* 设置鼠标消息捕获 */
if (msg.is_down()) {
SetCapture(hWnd);
} else {
const int keyStateMask = MK_LBUTTON | MK_RBUTTON | MK_MBUTTON | MK_XBUTTON1 | MK_XBUTTON2;
if ((wParam & keyStateMask) == 0) {
ReleaseCapture();
}
}
}
if (curMsgIsNeedToPush && (hWnd == pg->hwnd)) {
push_mouse_msg(pg, message, wParam, lParam, GetMessageTime());
}
pg->mouse_pos = curPos;
}
/*private function*/
static LRESULT CALLBACK wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
struct _graph_setting* pg_w = NULL;
struct _graph_setting* pg = &graph_setting;
// int wmId, wmEvent;
pg_w = (struct _graph_setting*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
if (pg_w == NULL || pg->img_page[0] == NULL) {
return DefWindowProcW(hWnd, message, wParam, lParam);
}
switch (message) {
case WM_TIMER:
if (pg == pg_w) {
on_timer(pg, hWnd, (unsigned int)wParam);
}
break;
case WM_PAINT:
if (pg == pg_w) {
on_paint(pg, hWnd);
}
break;
case WM_CLOSE:
if (pg == pg_w) {
if (pg->callback_close) {
pg->callback_close();
} else {
return DefWindowProcW(hWnd, message, wParam, lParam);
}
}
break;
case WM_DESTROY:
if (pg == pg_w) {
on_destroy(pg);
}
break;
case WM_ERASEBKGND:
if (pg == pg_w) {
return TRUE;
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
// if (hWnd == pg->hwnd)
{
if (pg->unicode_char_message) {
on_key(pg, message, (unsigned long)wParam, lParam);
} else {
// 将 UTF-16 编码的消息转换为相应多字节编码的消息
// UTF-16 可能是代理对, 这里处理下
wchar_t wc = (wchar_t)wParam;
wchar_t wBuf[3] = {0};
bool skip_this_message = false;
int wCount = 0;
if (IS_LOW_SURROGATE(wc)) {
pg->wchar_message_low_surrogate_cache = wc;
skip_this_message = true;
} else if (IS_HIGH_SURROGATE(wc)) {
wBuf[0] = pg->wchar_message_low_surrogate_cache;
wBuf[1] = wc;
pg->wchar_message_low_surrogate_cache = L'\0';
wCount = 2;
} else {
wBuf[0] = wc;
pg->wchar_message_low_surrogate_cache = L'\0';
wCount = 1;
}
if (!skip_this_message) {
char mbBuf[8];
int mbCount = WideCharToMultiByte(getcodepage(), 0, wBuf, wCount, mbBuf, 8, NULL, NULL);
for (int i = 0; i < mbCount; ++i) {
on_key(pg, message, (unsigned long)(unsigned char)mbBuf[i], lParam);
}
}
}
}
break;
case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK:
case WM_MBUTTONDOWN: case WM_MBUTTONUP: case WM_MBUTTONDBLCLK:
case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_RBUTTONDBLCLK:
case WM_XBUTTONDOWN: case WM_XBUTTONUP: case WM_XBUTTONDBLCLK:
case WM_MOUSEMOVE: case WM_MOUSEWHEEL:
mouseProc(hWnd, message, wParam, lParam);
break;
case WM_SETCURSOR:
if (pg == pg_w) {
on_setcursor(pg, hWnd);
return TRUE;
}
break;
case WM_IME_CONTROL:
on_ime_control(pg, hWnd, message, wParam, lParam);
break;
case WM_USER + 1:
windowmanager(pg, (wParam != 0), (struct msg_createwindow*)lParam);
break;
case WM_USER + 2: {
struct msg_createwindow* msg = (struct msg_createwindow*)lParam;
::SetFocus(msg->hwnd);
::SetEvent(msg->hEvent);
} break;
case WM_CTLCOLOREDIT: {
egeControlBase* ctl = (egeControlBase*)GetWindowLongPtrW((HWND)lParam, GWLP_USERDATA);
return ctl->onMessage(message, wParam, lParam);
} break;
default:
if (pg != pg_w) {
return ((egeControlBase*)pg_w)->onMessage(message, wParam, lParam);
}
return DefWindowProcW(hWnd, message, wParam, lParam);
}
if (pg != pg_w) {
return ((egeControlBase*)pg_w)->onMessage(message, wParam, lParam);
}
return 0;
}
PVOID getProcfunc()
{
return (PVOID)wndproc;
}
/* private function */
int graph_init(_graph_setting* pg)
{
pg->img_timer_update = newimage();
pg->msgkey_queue = new thread_queue<EGEMSG>;
pg->msgmouse_queue = new thread_queue<EGEMSG>;
setactivepage(0);
settarget(NULL);
setvisualpage(0);
window_setviewport(0, 0, pg->dc_w, pg->dc_h);
return 0;
}
void logoscene()
{
int nsize = getlogodatasize();
PIMAGE pimg = newimage();
int alpha = 0, b_nobreak = 1, n;
pimg->getimage((void*)getlogodata(), nsize);
if (getwidth() <= getwidth(pimg)) {
PIMAGE pimg1 = newimage(getwidth(pimg) / 2, getheight(pimg) / 2);
putimage(pimg1, 0, 0, getwidth(pimg) / 2, getheight(pimg) / 2, pimg, 0, 0, getwidth(pimg), getheight(pimg));
delimage(pimg);
pimg = pimg1;
}
setrendermode(RENDER_MANUAL);
for (n = 0; n < 20 * 1 && b_nobreak; n++, delay_fps(60)) {
cleardevice();
}
for (alpha = 0; alpha <= 0xFF && b_nobreak; alpha += 8, delay_fps(60)) {
setbkcolor_f(EGERGB(alpha, alpha, alpha));
cleardevice();
while (kbhit()) {
getkey();
}
}
setbkcolor_f(0xFFFFFF);
for (alpha = 0; alpha <= 0xFF && b_nobreak; alpha += 8, delay_fps(60)) {
cleardevice();
putimage_alphablend(
NULL, pimg, (getwidth() - pimg->getwidth()) / 2, (getheight() - pimg->getheight()) / 2, (UCHAR)alpha);
while (kbhit()) {
getkey();
b_nobreak = 0;
}
}
setbkcolor_f(0xFFFFFF);
for (n = 0; n < 60 * 1 && b_nobreak; n++, delay_fps(60)) {
cleardevice();
putimage((getwidth() - pimg->getwidth()) / 2, (getheight() - pimg->getheight()) / 2, pimg);
while (kbhit()) {
getkey();
b_nobreak = 0;
}
}
setbkcolor_f(0xFFFFFF);
for ((alpha > 0xFF) && (alpha -= 4); alpha >= 0; alpha -= 8, delay_fps(60)) {
cleardevice();
putimage_alphablend(
NULL, pimg, (getwidth() - pimg->getwidth()) / 2, (getheight() - pimg->getheight()) / 2, (UCHAR)alpha);
while (kbhit()) {
getkey();
b_nobreak = 0;
}
}
cleardevice();
for (alpha = 0xFF; alpha >= 0; alpha -= 8, delay_fps(60)) {
setbkcolor_f(EGERGB(alpha, alpha, alpha));
cleardevice();
while (kbhit()) {
getkey();
b_nobreak = 0;
}
}
delimage(pimg);
setbkcolor_f(BLACK);
cleardevice();
setrendermode(RENDER_AUTO);
}
inline void init_img_page(struct _graph_setting* pg)
{
if (!pg->has_init) {
#ifdef EGE_GDIPLUS
gdiplusinit();
#endif
}
}
void initicon(void)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
HICON hIcon = NULL;
struct _graph_setting* pg = &graph_setting;
// 提前设置了图标
if (pg->window_hicon != 0) {
return;
}
EnumResourceNames(hInstance, RT_ANIICON, EnumResNameProc, (LONG_PTR)&hIcon);
if (hIcon) {
pg->window_hicon = hIcon;
return;
}
EnumResourceNames(hInstance, RT_GROUP_ICON, EnumResNameProc, (LONG_PTR)&hIcon);
if (hIcon) {
pg->window_hicon = hIcon;
return;
}
EnumResourceNames(hInstance, RT_ICON, EnumResNameProc, (LONG_PTR)&hIcon);
if (hIcon) {
pg->window_hicon = hIcon;
return;
}
// default icon
pg->window_hicon = LoadIcon(NULL, IDI_APPLICATION);
}
void setcodepage(unsigned int codepage)
{
graph_setting.codepage = codepage;
}
unsigned int getcodepage()
{
return graph_setting.codepage;
}
void setunicodecharmessage(bool enable)
{
graph_setting.unicode_char_message = enable;
}
bool getunicodecharmessage()
{
return graph_setting.unicode_char_message;
}
void initgraph(int* gdriver, int* gmode, const char* path)
{
struct _graph_setting* pg = &graph_setting;
pg->init_option = getinitmode();
pg->exit_flag = 0;
pg->exit_window = 0;
dll::loadDllsIfNot();
// 已创建则转为改变窗口大小
if (pg->has_init) {
int width = (short)(*gmode & 0xFFFF);
int height = (short)((unsigned int)(*gmode) >> 16);
resizewindow(width, height);
HWND hwnd = getHWnd();
if (!::IsWindowVisible(hwnd)) {
::ShowWindow(hwnd, SW_SHOW);
}
return;
}
// 初始化环境
setmode(*gdriver, *gmode);
init_img_page(pg);
pg->instance = GetModuleHandle(NULL);
initicon();
// 注册窗口类,设置默认消息处理函数, 此处创建 Unicode 窗口
register_classW(pg, pg->instance);
// SECURITY_ATTRIBUTES sa = {0};
if (pg->init_option & INIT_EVENTLOOP) {
initWindow(pg);
} else {
DWORD pid;
pg->threadui_handle = CreateThread(NULL, 0, messageLoopThread, pg, CREATE_SUSPENDED, &pid);
ResumeThread(pg->threadui_handle);
while (!pg->has_init) {
::Sleep(1);
}
}
UpdateWindow(pg->hwnd);
if (!(pg->init_option & INIT_HIDE)) {
ShowWindow(pg->hwnd, SW_SHOWNORMAL);
BringWindowToTop(pg->hwnd);
SetForegroundWindow(pg->hwnd);
}
if (g_windowexstyle & WS_EX_TOPMOST) {
SetWindowPos(pg->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
// 初始化鼠标位置数据
POINT pt;
GetCursorPos(&pt);
ScreenToClient(pg->hwnd, &pt);
pg->mouse_pos = Point(pt.x, pt.y);
static egeControlBase _egeControlBase;
if ((pg->init_option & INIT_WITHLOGO) && !(pg->init_option & INIT_HIDE)) {
logoscene();
}
if (pg->init_option & INIT_RENDERMANUAL) {
setrendermode(RENDER_MANUAL);
}
pg->first_show = true;
pg->mouse_show = true;
}
void initgraph(int width, int height, initmode_flag mode)
{
int g = TRUECOLORSIZE, m = (width) | (height << 16);
setinitmode(mode, g_windowpos_x, g_windowpos_y);
initgraph(&g, &m, "");
}
void detectgraph(int* gdriver, int* gmode)
{
*gdriver = VGA;
*gmode = VGAHI;
}
void closegraph()
{
struct _graph_setting* pg = &graph_setting;
ShowWindow(pg->hwnd, SW_HIDE);
}
int initWindow(_graph_setting* pg)
{
/* 执行应用程序初始化: */
if (!init_instance(pg->instance)) {
return 0xFFFFFFFF;
}
// 图形初始化
if (pg->dc == 0) {
graph_init(pg);
}
pg->mouse_show = 0;
pg->exit_flag = 0;
pg->use_force_exit = (pg->init_option & INIT_NOFORCEEXIT ? false : true);
if (pg->init_option & INIT_NOFORCEEXIT) {
SetCloseHandler(DefCloseHandler);
}
pg->close_manually = true;
/* 仅在 INIT_EVENTLOOP 模式下设置定时器(INIT_EVENTLOOP 模式下事件在主线程中处理,固定为手动渲染模式,无需设置定时器。 */
if (pg->init_option & INIT_EVENTLOOP) {
pg->lock_window = true;
pg->skip_timer_mark = true;
} else {
pg->skip_timer_mark = false;
SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL);
}
pg->has_init = true;
}
int messageHandle()
{
MSG msg;
int handleCount = 0;
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
return -1;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);