From 8b4875b3d945125006bf2ed631ce781bf2da9743 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Wed, 27 Jan 2021 15:54:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mousepressed=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- man/api/input/index.htm | 2 ++ man/api/input/mousepressed.htm | 48 ++++++++++++++++++++++++++++++++++ src/ege.h | 6 ++++- src/ege_head.h | 9 ++++++- src/egegapi.cpp | 13 +++++++++ src/graphics.cpp | 29 ++++++++++++++++++++ 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 man/api/input/mousepressed.htm diff --git a/man/api/input/index.htm b/man/api/input/index.htm index 523200a9..229cca88 100644 --- a/man/api/input/index.htm +++ b/man/api/input/index.htm @@ -29,6 +29,8 @@ mousepos获取当前鼠标位置 showmouse设置鼠标显示状态 flushmouse清空鼠标消息缓存区 +mousepressed检测是否有鼠标点击发生 +clearmousepressed(不建议普通用户使用)清除鼠标点击事件 FlushMouseMsgBuffer(过时函数)清空鼠标消息缓冲区 GetMouseMsg(过时函数)获取一个鼠标消息。如果当前鼠标消息队列中没有,就一直等待 diff --git a/man/api/input/mousepressed.htm b/man/api/input/mousepressed.htm new file mode 100644 index 00000000..8c791553 --- /dev/null +++ b/man/api/input/mousepressed.htm @@ -0,0 +1,48 @@ + + + mousepressed + + + +
主页->库函数目录->键盘鼠标输入函数->mousepressed
+
+功能:
+这个函数用于检测是否有鼠标点击事件发生并立即返回。
+当执行该函数并且检测到鼠标点击事件后,执行delay_fps/delay_ms/delay_jfps函数会清除鼠标点击状态。
+
+声明:
+
int mousepressed();
+参数: +(无) + +返回值: +如果有鼠标点击发生,返回 1;否则返回 0。 + +示例: +#include + +int main() { + initgraph(800,600); + setrendermode(RENDER_MANUAL); + + + int count = 0; + for(;is_run();delay_fps(60)) { + if (mousepressed()) { + //检测到鼠标点击事件 + count += 1; + //清除屏幕原有内容,然后输出点击提示 + cleardevice(); + xyprintf(50,50,"clicked %d times",count); + } + } + closegraph(); + return 0; +} + +
+
+ + + + diff --git a/src/ege.h b/src/ege.h index 24bff277..cf51d58b 100644 --- a/src/ege.h +++ b/src/ege.h @@ -1108,6 +1108,8 @@ double EGEAPI randomf(); int EGEAPI inputbox_getline(LPCSTR title, LPCSTR text, LPSTR buf, int len); //Իû룬ǰͣط0ʾЧ0ΪЧ int EGEAPI inputbox_getline(LPCWSTR title, LPCWSTR text, LPWSTR buf, int len); //Իû룬ǰͣط0ʾЧ0ΪЧ +//ͨú +int EGEAPI keystate(int key); // üΪkeyļkey_code_eǷ£keyʹ갴ļ룬õ״̬ //̴ int EGEAPI kbmsg(); @@ -1116,7 +1118,6 @@ EGE_DEPRECATE(getchEx) int EGEAPI getchEx(int flag); EGE_DEPRECATE(kbhitEx) int EGEAPI kbhitEx(int flag); -int EGEAPI keystate(int key); // üΪkeyļkey_code_eǷ£keyʹ갴ļ룬õ״̬ void EGEAPI flushkey(); // ռϢ #if !defined(_INC_CONIO) && !defined(_CONIO_H_) @@ -1138,6 +1139,9 @@ EGE_DEPRECATE(GetMouseMsg) void EGEAPI flushmouse(); // Ϣ int EGEAPI showmouse(int bShow); // Ƿʾ int EGEAPI mousepos(int *x, int *y); // ȡǰλ +int EGEAPI mousepressed(); // Ƿ +void EGEAPI clearmousepressed(); //¼ + /* callback function define as: diff --git a/src/ege_head.h b/src/ege_head.h index db9af5df..7655e8e7 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -439,6 +439,11 @@ class IMAGE friend void getimage_from_png_struct(PIMAGE, void*, void*); }; +#define LBUTTON_PRESSED 0x01 +#define MBUTTON_PRESSED 0x02 +#define RBUTTON_PRESSED 0x04 + + // egeȫ״̬ struct _graph_setting { bool has_init; @@ -476,7 +481,7 @@ struct _graph_setting { bool use_force_exit; //ǿƹرս̱ bool lock_window; bool timer_stop_mark; - bool skip_timer_mark; + bool skip_timer_mark; thread_queue *msgkey_queue, *msgmouse_queue; @@ -489,6 +494,8 @@ struct _graph_setting { int mouse_lastclick_x, mouse_lastclick_y; int mouse_lastup_x, mouse_lastup_y; int mouse_show; + int mouse_pressed; + int mouse_pressed_tested; LPMSG_KEY_PROC callback_key; void* callback_key_param; diff --git a/src/egegapi.cpp b/src/egegapi.cpp index abe6c294..2ffa98f7 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -314,6 +314,7 @@ api_sleep(long dwMilliseconds) { void ege_sleep(long ms) { if (ms <= 0) return; + if (0) { // ģʽռCPU ::Sleep(ms); } else if (0) { //ȷģʽռCPUԸ @@ -353,6 +354,11 @@ void delay_ms(long ms) { struct _graph_setting * pg = &graph_setting; egeControlBase* &root = pg->egectrl_root; + + //Ϣ + if (pg->mouse_pressed_tested) + clearmousepressed(); + pg->skip_timer_mark = true; if (ms == 0) { if (pg->update_mark_count < UPDATE_MAX_CALL) { @@ -430,6 +436,9 @@ delay_fps(double fps) { double dw = get_highfeq_time_ls(pg) * 1000.0; int nloop = 0; + //Ϣ + if (pg->mouse_pressed_tested) + clearmousepressed(); if (pg->delay_fps_dwLast == 0) { pg->delay_fps_dwLast = get_highfeq_time_ls(pg) * 1000.0; } @@ -476,6 +485,10 @@ delay_jfps(double fps) { double dw = get_highfeq_time_ls(pg) * 1000.0; int nloop = 0; + //Ϣ + if (pg->mouse_pressed_tested) + clearmousepressed(); + if (pg->delay_fps_dwLast == 0) { pg->delay_fps_dwLast = get_highfeq_time_ls(pg) * 1000.0; } diff --git a/src/graphics.cpp b/src/graphics.cpp index 0be70776..816375eb 100755 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -480,6 +480,30 @@ flushmouse() { return ; } +int +mousepressed() { + int result; + struct _graph_setting * pg = &graph_setting; + if (pg->exit_window) + return 0; + result = (pg->mouse_pressed != 0); + if (result) { + pg->mouse_pressed_tested = 1; + } else { + pg->mouse_pressed_tested = 0; + } + return result; +} + +void +clearmousepressed() { + struct _graph_setting * pg = &graph_setting; + if (pg->exit_window) + return; + pg->mouse_pressed_tested = 0; + pg->mouse_pressed = 0; +} + int mousemsg() { struct _graph_setting * pg = &graph_setting; @@ -966,6 +990,7 @@ wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { } break; case WM_LBUTTONDOWN: + pg->mouse_pressed |= LBUTTON_PRESSED; case WM_LBUTTONDBLCLK: pg->mouse_lastclick_x = (short int)((UINT)lParam & 0xFFFF); pg->mouse_lastclick_y = (short int)((UINT)lParam >> 16); @@ -975,6 +1000,7 @@ wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (hWnd == pg->hwnd) push_mouse_msg(pg, message, wParam, lParam); break; case WM_MBUTTONDOWN: + pg->mouse_pressed |= MBUTTON_PRESSED; case WM_MBUTTONDBLCLK: pg->mouse_lastclick_x = (short int)((UINT)lParam & 0xFFFF); pg->mouse_lastclick_y = (short int)((UINT)lParam >> 16); @@ -984,6 +1010,7 @@ wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (hWnd == pg->hwnd) push_mouse_msg(pg, message, wParam, lParam); break; case WM_RBUTTONDOWN: + pg->mouse_pressed |= RBUTTON_PRESSED; case WM_RBUTTONDBLCLK: pg->mouse_lastclick_x = (short int)((UINT)lParam & 0xFFFF); pg->mouse_lastclick_y = (short int)((UINT)lParam >> 16); @@ -1342,6 +1369,8 @@ messageloopthread(LPVOID lpParameter) { graph_init(pg); { + pg->mouse_pressed = 0; + pg->mouse_pressed_tested = 0; pg->mouse_show = 0; pg->exit_flag = 0; pg->use_force_exit = (g_initoption & INIT_NOFORCEEXIT ? false : true); From 93896a80df6f3bf3ba979c63c6db4d14ae0a16ba Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Wed, 27 Jan 2021 16:03:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ege.h | 4 ++-- src/ege_head.h | 2 +- src/egegapi.cpp | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/ege.h b/src/ege.h index cf51d58b..c0eb5fb3 100644 --- a/src/ege.h +++ b/src/ege.h @@ -1139,8 +1139,8 @@ EGE_DEPRECATE(GetMouseMsg) void EGEAPI flushmouse(); // Ϣ int EGEAPI showmouse(int bShow); // Ƿʾ int EGEAPI mousepos(int *x, int *y); // ȡǰλ -int EGEAPI mousepressed(); // Ƿ -void EGEAPI clearmousepressed(); //¼ +int EGEAPI mousepressed(); // Ƿ +void EGEAPI clearmousepressed(); //¼ /* diff --git a/src/ege_head.h b/src/ege_head.h index 7655e8e7..d1089666 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -481,7 +481,7 @@ struct _graph_setting { bool use_force_exit; //ǿƹرս̱ bool lock_window; bool timer_stop_mark; - bool skip_timer_mark; + bool skip_timer_mark; thread_queue *msgkey_queue, *msgmouse_queue; diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 2ffa98f7..506b9877 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -313,8 +313,7 @@ api_sleep(long dwMilliseconds) { void ege_sleep(long ms) { - if (ms <= 0) return; - + if (ms <= 0) return; if (0) { // ģʽռCPU ::Sleep(ms); } else if (0) { //ȷģʽռCPUԸ @@ -357,8 +356,7 @@ delay_ms(long ms) { //Ϣ if (pg->mouse_pressed_tested) - clearmousepressed(); - + clearmousepressed(); pg->skip_timer_mark = true; if (ms == 0) { if (pg->update_mark_count < UPDATE_MAX_CALL) { @@ -488,7 +486,6 @@ delay_jfps(double fps) { //Ϣ if (pg->mouse_pressed_tested) clearmousepressed(); - if (pg->delay_fps_dwLast == 0) { pg->delay_fps_dwLast = get_highfeq_time_ls(pg) * 1000.0; }