Skip to content

Commit

Permalink
EGE/: 修复函数 getch 阻塞不返回(自从 19.01 )。
Browse files Browse the repository at this point in the history
参见版本 b543949 。

Signed-off-by: FrankHB <frankhb1989@gmail.com>
  • Loading branch information
FrankHB committed Apr 14, 2020
1 parent 09f6808 commit f5f1452
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
1 change: 1 addition & 0 deletions Compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ YEGE 以 [misakamm 的 xege](http://github.com/misakamm/xege) 为基础修改,
* 修改后的格式和 `YSLib::Pixel` 在 Win32 上的实现以及 [wysaid/xege pull request 12](https://github.com/wysaid/xege/pull/12) 中的像素格式保持一致,存储格式都为 BGRA8888 。
* 函数 `clearviewport` 使用背景颜色填充。
* 参见 [wysaid/xege pull request 12](https://github.com/wysaid/xege/pull/12) 。
* 修复函数 `getch` 阻塞不返回(自从 19.01 )。

### 非外部依赖项代码风格和格式

Expand Down
9 changes: 9 additions & 0 deletions EGE/include/thread_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class thread_queue
for(auto& e : _queue)
f(e);
}

template<typename F>
auto
process_queue(F f) -> decltype(f(_queue))
{
std::lock_guard<std::mutex> lck(mtx);

return f(_queue);
}
};

}
Expand Down
66 changes: 50 additions & 16 deletions EGE/src/ege/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ wndproc(::HWND hWnd, unsigned message, ::WPARAM wParam, ::LPARAM lParam)
return 0;
}

std::uint16_t
filter_getch(int kv, unsigned w) ynothrow
{
if(((kv & KEYMSG_DOWN) && (w >= 0x70 && w < 0x80)) || (w > ' ' && w < '0'))
kv |= 0x100;
return kv & 0xFFFF;
}

} // unnamed namespace;


Expand Down Expand Up @@ -276,14 +284,38 @@ EGEApplication::_get_input(get_input_op op)
switch(op)
{
case get_input_op::getch:
do
{
int key = _peekkey();
if(key < 0)
break;
if(key > 0)
key = _getkey_p();
} while(_is_run() && _waitdealmessage());
const auto dw(GetTickCount());

do
{
int key = _peekkey();
if(key < 0)
break;
if(key > 0)
{
key = _getkey_p();
if(key != 0)
{
const auto k(msgkey_queue.process_queue(
[&](std::deque<EGEMSG>& q) -> std::uint32_t{
if(!q.empty())
{
const auto& msg(q.back());

if(dw < msg.time + 1000)
return filter_getch(key, msg.wParam);
return 0xFFFFFFFF;
}
return filter_getch(key, key & 0xFFFF);
}));

if(k != 0xFFFFFFFF)
return k;
}
}
} while(_is_run() && _waitdealmessage());
}
break;
case get_input_op::kbhit:
return _peekkey();
Expand Down Expand Up @@ -577,7 +609,8 @@ EGEApplication::_on_destroy()
#endif

void
EGEApplication::_on_key(unsigned message, unsigned long keycode, ::LPARAM keyflag)
EGEApplication::_on_key(unsigned message, unsigned long keycode,
::LPARAM keyflag)
{
if(message == WM_KEYDOWN && keycode < MAX_KEY_VCODE)
keystatemap[keycode] = 1;
Expand All @@ -588,8 +621,8 @@ EGEApplication::_on_key(unsigned message, unsigned long keycode, ::LPARAM keyfla
}

void
EGEApplication::_on_mouse_button_up(::HWND h_wnd, unsigned msg, ::WPARAM w_param,
::LPARAM l_param)
EGEApplication::_on_mouse_button_up(::HWND h_wnd, unsigned msg,
::WPARAM w_param, ::LPARAM l_param)
{
auto* l = &mouse_state_l;
auto vk = VK_LBUTTON;
Expand Down Expand Up @@ -641,7 +674,8 @@ EGEApplication::_on_setcursor(::HWND hwnd)
::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)
if(pt.x >= rect.left && pt.x < rect.right
&& pt.y >= rect.top && pt.y <= rect.bottom)
return {};
}
return ::LoadCursor({}, IDC_ARROW);
Expand Down Expand Up @@ -669,8 +703,8 @@ EGEApplication::_peekkey()
if(msg.message == WM_KEYDOWN)
{
if(msg.wParam >= 0x70 && msg.wParam < 0x80)
return (KEYMSG_DOWN | (int(msg.wParam) + 0x100));
return (KEYMSG_DOWN | (int(msg.wParam) & 0xFFFF));
return KEYMSG_DOWN | (int(msg.wParam) + 0x100);
return KEYMSG_DOWN | (int(msg.wParam) & 0xFFFF);
}
else if(msg.message == WM_KEYUP)
return KEYMSG_UP | (int(msg.wParam) & 0xFFFF);
Expand All @@ -693,10 +727,10 @@ EGEApplication::_peekallkey(int flag)
(msg.message == WM_KEYDOWN && (flag & KEYMSG_DOWN_FLAG)))
{
if(msg.message == WM_CHAR)
return (KEYMSG_CHAR | (int(msg.wParam) & 0xFFFF));
return KEYMSG_CHAR | (int(msg.wParam) & 0xFFFF);
else if(msg.message == WM_KEYDOWN)
return (KEYMSG_DOWN | (int(msg.wParam) & 0xFFFF)
| (msg.lParam & 0x40000000 ? 0 : KEYMSG_FIRSTDOWN));
return KEYMSG_DOWN | (int(msg.wParam) & 0xFFFF)
| (msg.lParam & 0x40000000 ? 0 : KEYMSG_FIRSTDOWN);
else if(msg.message == WM_KEYUP)
return KEYMSG_UP | (int(msg.wParam) & 0xFFFF);
}
Expand Down

0 comments on commit f5f1452

Please sign in to comment.