mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-09-06 03:18:13 +00:00
Improved timing accuracy of SDL_WaitEventTimeout() on Windows
We don't need to use the hack of setting a timer and waiting for a timer message, MsgWaitForMultipleObjects() will allow us to wait for input directly with a timeout. Before this change, sleeping for 20 ms would actually sleep for around 30 ms, with this change the sleep time is pretty accurate at 20-21 ms. (cherry picked from commit2670eb44af
) (cherry picked from commit08caafe2f1
)
This commit is contained in:
@@ -1802,29 +1802,8 @@ void SDL_SetWindowsMessageHook(SDL_WindowsMessageHook callback, void *userdata)
|
|||||||
|
|
||||||
int WIN_WaitEventTimeout(_THIS, int timeout)
|
int WIN_WaitEventTimeout(_THIS, int timeout)
|
||||||
{
|
{
|
||||||
MSG msg;
|
|
||||||
if (g_WindowsEnableMessageLoop) {
|
if (g_WindowsEnableMessageLoop) {
|
||||||
BOOL message_result;
|
if (MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD)timeout, QS_ALLINPUT)) {
|
||||||
UINT_PTR timer_id = 0;
|
|
||||||
if (timeout > 0) {
|
|
||||||
timer_id = SetTimer(NULL, 0, timeout, NULL);
|
|
||||||
message_result = GetMessage(&msg, 0, 0, 0);
|
|
||||||
KillTimer(NULL, timer_id);
|
|
||||||
} else if (timeout == 0) {
|
|
||||||
message_result = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
|
|
||||||
} else {
|
|
||||||
message_result = GetMessage(&msg, 0, 0, 0);
|
|
||||||
}
|
|
||||||
if (message_result) {
|
|
||||||
if (msg.message == WM_TIMER && !msg.hwnd && msg.wParam == timer_id) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (g_WindowsMessageHook) {
|
|
||||||
g_WindowsMessageHook(g_WindowsMessageHookData, msg.hwnd, msg.message, msg.wParam, msg.lParam);
|
|
||||||
}
|
|
||||||
/* Always translate the message in case it's a non-SDL window (e.g. with Qt integration) */
|
|
||||||
TranslateMessage(&msg);
|
|
||||||
DispatchMessage(&msg);
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user