From daf4a60b65e354788a1c9e0b4b3e929699b092e9 Mon Sep 17 00:00:00 2001 From: BearOso Date: Wed, 22 Feb 2023 14:30:08 -0600 Subject: [PATCH] win32: Fix CPU usage with new throttle. --- win32/wsnes9x.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/win32/wsnes9x.cpp b/win32/wsnes9x.cpp index 81e48bcf..db777fcc 100644 --- a/win32/wsnes9x.cpp +++ b/win32/wsnes9x.cpp @@ -3369,6 +3369,8 @@ int WINAPI WinMain( MSG msg; + HANDLE throttle_timer = CreateWaitableTimer(NULL, true, NULL); + while (TRUE) { EnsureInputDisplayUpdated(); @@ -3460,6 +3462,26 @@ int WINAPI WinMain( S9xMainLoop(); GUI.FrameCount++; + if (GUI.CursorTimer) + { + if (--GUI.CursorTimer == 0) + { + if (GUI.ControllerOption != SNES_SUPERSCOPE && GUI.ControllerOption != SNES_JUSTIFIER && GUI.ControllerOption != SNES_JUSTIFIER_2 && GUI.ControllerOption != SNES_MACSRIFLE) + SetCursor(NULL); + } + } + } + else if ((PCEnd - PCStart) > PCFrameTime / 10) + { + auto time_left = ((PCFrameTime - (PCEnd - PCStart)) * 1000000 / PCBase) - 100; + LARGE_INTEGER li; + li.QuadPart = -time_left; + SetWaitableTimer(throttle_timer, &li, 0, NULL, NULL, false); + WaitForSingleObject(throttle_timer, INFINITE); + } + else + { + Sleep(0); } #ifdef NETPLAY_SUPPORT @@ -3471,14 +3493,6 @@ int WINAPI WinMain( Settings.FrameAdvance = false; CPU.Flags &= ~DEBUG_MODE_FLAG; } - if (GUI.CursorTimer) - { - if (--GUI.CursorTimer == 0) - { - if (GUI.ControllerOption != SNES_SUPERSCOPE && GUI.ControllerOption != SNES_JUSTIFIER && GUI.ControllerOption != SNES_JUSTIFIER_2 && GUI.ControllerOption != SNES_MACSRIFLE) - SetCursor (NULL); - } - } } loop_exit: