Advertisement
AliAbdulKareem

inaccurate sleep

Mar 2nd, 2024
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1.     while(Run) 
  2.     {
  3.  
  4.         FILETIME GameDllLastWrite = Win32GetFileTime(DLLSourcePath);
  5.         if (CompareFileTime(&GameDllWriteTime, &GameDllLastWrite))
  6.         {
  7.            
  8.             Win32UnloadCode(&Win32Code, DLLCopyPath);
  9.             BOOL CopyResult = CopyFile(DLLSourcePath, DLLCopyPath, FALSE);
  10.             if(!CopyResult)
  11.             {
  12.  
  13.                 Win32Code = Win32LoadCode(DLLCopyPath); // we got here after we unloaded the library yet we failed to copy to allow loading the new library
  14.                 goto EndCompareTime;
  15.             }
  16.  
  17.             Win32Code = Win32LoadCode(DLLCopyPath);
  18.             GameDllWriteTime = GameDllLastWrite;
  19.  
  20.             EndCompareTime:
  21.             (void)0;
  22.         }
  23.  
  24.         MSG Msg;
  25.         while (PeekMessageA(&Msg, WindowHandle, 0, 0, PM_REMOVE))
  26.         {
  27.             UINT Message = Msg.message;
  28.             switch (Message)
  29.             {
  30.             case WM_KEYUP:
  31.             case WM_KEYDOWN:
  32.             case WM_SYSKEYDOWN:
  33.             case WM_SYSKEYUP:
  34.                 {
  35.                     ProcessKeyboard(Message, Msg.wParam, Msg.lParam, &GameState);      
  36.                 } break;
  37.  
  38.             case WM_SIZE:
  39.                 {
  40.                     ProcessResize(WindowHandle, &BitMapInfo, &Win32Code, &GameState);
  41.                 } break;
  42.             }
  43.  
  44.             TranslateMessage(&Msg);        
  45.             DispatchMessage(&Msg);
  46.  
  47.         }
  48.  
  49.         Win32Code.GameUpdate(&GameState);
  50.         Win32PresentBitMapToScreen(WindowHandle, DeviceContext, &BitMapInfo, GameState.BackBuffer);
  51.  
  52.         QueryPerformanceCounter(&EndingTime);
  53.         u64 ElapsedMicroSeconds = EndingTime.QuadPart - StartingTime.QuadPart;
  54.  
  55.         ElapsedMicroSeconds *= 1000000;
  56.         ElapsedMicroSeconds /= Frequency.QuadPart;
  57.  
  58.  
  59. #if SLEEP_ON
  60.         u64 FrameTimeInMicroSeconds = 16000;
  61.         u64 SleepTime = FrameTimeInMicroSeconds - ElapsedMicroSeconds;  // this might wrap if we sleep for too long
  62.         if (SleepTime > 0 && SleepTime < FrameTimeInMicroSeconds)
  63.         {
  64.  
  65.             Sleep((DWORD)SleepTime/1000);
  66.  
  67.         }
  68. #endif
  69.         QueryPerformanceCounter(&EndingTime);
  70.         u64 ElapsedMicrosecondsAfterSleep = EndingTime.QuadPart - StartingTime.QuadPart;
  71.         ElapsedMicrosecondsAfterSleep *= 1000000;
  72.         ElapsedMicrosecondsAfterSleep /= Frequency.QuadPart;
  73.  
  74.         StartingTime = EndingTime; // reset our starting time
  75. #if DEBUG_PRINT_MS
  76.         char Buffer[1024];
  77.         wsprintf(Buffer, "Elapsed Microseconds = %d \t| AfterSleep =%d \t| SleepTime = %d\n", ElapsedMicroSeconds, (s32)ElapsedMicrosecondsAfterSleep, (s32)SleepTime);
  78.         OutputDebugString(Buffer);
  79. #endif
  80.     }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement