Browse Source

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.
Sam Lantinga 1 year ago
parent
commit
c5a99f1515
1 changed files with 13 additions and 11 deletions
  1. 13 11
      src/video/windows/SDL_windowsevents.c

+ 13 - 11
src/video/windows/SDL_windowsevents.c

@@ -124,17 +124,12 @@ static Uint64 timestamp_offset;
 
 
 static void WIN_SetMessageTick(DWORD tick)
 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;
     message_tick = tick;
 }
 }
 
 
 static Uint64 WIN_GetEventTimestamp(void)
 static Uint64 WIN_GetEventTimestamp(void)
 {
 {
+    const Uint64 TIMESTAMP_WRAP_OFFSET = SDL_MS_TO_NS(0x100000000LL);
     Uint64 timestamp, now;
     Uint64 timestamp, now;
 
 
     if (!SDL_processing_messages) {
     if (!SDL_processing_messages) {
@@ -144,13 +139,20 @@ static Uint64 WIN_GetEventTimestamp(void)
 
 
     now = SDL_GetTicksNS();
     now = SDL_GetTicksNS();
     timestamp = SDL_MS_TO_NS(message_tick);
     timestamp = SDL_MS_TO_NS(message_tick);
-
+    timestamp += timestamp_offset;
     if (!timestamp_offset) {
     if (!timestamp_offset) {
+        // Initializing timestamp offset
+        //SDL_Log("Initializing timestamp offset\n");
         timestamp_offset = (now - timestamp);
         timestamp_offset = (now - timestamp);
-    }
-    timestamp += timestamp_offset;
-
-    if (timestamp > now) {
+        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_offset -= (timestamp - now);
         timestamp = now;
         timestamp = now;
     }
     }