From c5a99f1515eaf36e786926b72a6f496ba5ead831 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 31 Jul 2024 16:36:47 -0700 Subject: [PATCH] Fixed detecting wrapping Windows message time It's possible to get message times out of order when processing the Windows message queue, so this passes those times through unchanged, while still detecting when the message tick wraps. --- src/video/windows/SDL_windowsevents.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index bdd19c8c2e..fd41ab15bc 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -124,17 +124,12 @@ static Uint64 timestamp_offset; static void WIN_SetMessageTick(DWORD tick) { - if (message_tick) { - if (tick < message_tick && timestamp_offset) { - /* The tick counter rolled over, bump our offset */ - timestamp_offset += SDL_MS_TO_NS(0x100000000LL); - } - } message_tick = tick; } static Uint64 WIN_GetEventTimestamp(void) { + const Uint64 TIMESTAMP_WRAP_OFFSET = SDL_MS_TO_NS(0x100000000LL); Uint64 timestamp, now; if (!SDL_processing_messages) { @@ -144,13 +139,20 @@ static Uint64 WIN_GetEventTimestamp(void) now = SDL_GetTicksNS(); timestamp = SDL_MS_TO_NS(message_tick); - - if (!timestamp_offset) { - timestamp_offset = (now - timestamp); - } timestamp += timestamp_offset; - - if (timestamp > now) { + if (!timestamp_offset) { + // Initializing timestamp offset + //SDL_Log("Initializing timestamp offset\n"); + timestamp_offset = (now - timestamp); + timestamp = now; + } else if ((Sint64)(now - timestamp - TIMESTAMP_WRAP_OFFSET) >= 0) { + // The windows message tick wrapped + //SDL_Log("Adjusting timestamp offset for wrapping tick\n"); + timestamp_offset += TIMESTAMP_WRAP_OFFSET; + timestamp += TIMESTAMP_WRAP_OFFSET; + } else if (timestamp > now) { + // We got a newer timestamp, but it can't be newer than now, so adjust our offset + //SDL_Log("Adjusting timestamp offset, %.2f ms newer\n", (double)(timestamp - now) / SDL_NS_PER_MS); timestamp_offset -= (timestamp - now); timestamp = now; }