diff options
-rw-r--r-- | src/video/allegro_v.cpp | 9 | ||||
-rw-r--r-- | src/video/cocoa/cocoa_v.mm | 9 | ||||
-rw-r--r-- | src/video/dedicated_v.cpp | 9 | ||||
-rw-r--r-- | src/video/sdl2_v.cpp | 13 | ||||
-rw-r--r-- | src/video/sdl_v.cpp | 13 | ||||
-rw-r--r-- | src/video/win32_v.cpp | 17 |
6 files changed, 55 insertions, 15 deletions
diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp index de329db9e..800cce40c 100644 --- a/src/video/allegro_v.cpp +++ b/src/video/allegro_v.cpp @@ -521,8 +521,15 @@ void VideoDriver_Allegro::MainLoop() DrawSurfaceToScreen(); } + /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ if (!_fast_forward || _pause_mode) { - CSleep(1); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto next_tick = std::min(next_draw_tick, next_game_tick); + auto now = std::chrono::steady_clock::now(); + + if (next_tick > now) { + std::this_thread::sleep_for(next_tick - now); + } } } } diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 20f3904ae..7c8981f61 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -704,8 +704,15 @@ void VideoDriver_Cocoa::GameLoop() this->Draw(); } + /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ if (!_fast_forward || _pause_mode) { - CSleep(1); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto next_tick = std::min(next_draw_tick, next_game_tick); + auto now = std::chrono::steady_clock::now(); + + if (next_tick > now) { + std::this_thread::sleep_for(next_tick - now); + } } } } diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index 7b1abbadd..845b0bf8c 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -311,9 +311,14 @@ void VideoDriver_Dedicated::MainLoop() /* Sleep longer on a dedicated server, if the game is paused and no clients connected. * That can allow the CPU to better use deep sleep states. */ if (_pause_mode != 0 && !HasClients()) { - CSleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } else { - CSleep(1); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto now = std::chrono::steady_clock::now(); + + if (next_game_tick > now) { + std::this_thread::sleep_for(next_game_tick - now); + } } } } diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index a8d8fc3e1..7c04b8743 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -824,10 +824,17 @@ void VideoDriver_SDL::LoopOnce() /* Emscripten is running an event-based mainloop; there is already some * downtime between each iteration, so no need to sleep. */ #ifndef __EMSCRIPTEN__ + /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ if (!_fast_forward || _pause_mode) { - if (_draw_mutex != nullptr) draw_lock.unlock(); - CSleep(1); - if (_draw_mutex != nullptr) draw_lock.lock(); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto next_tick = std::min(next_draw_tick, next_game_tick); + auto now = std::chrono::steady_clock::now(); + + if (next_tick > now) { + if (_draw_mutex != nullptr) draw_lock.unlock(); + std::this_thread::sleep_for(next_tick - now); + if (_draw_mutex != nullptr) draw_lock.lock(); + } } #endif } diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index 11da9c5a3..9f28b641c 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -782,10 +782,17 @@ void VideoDriver_SDL::MainLoop() } } + /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ if (!_fast_forward || _pause_mode) { - if (_draw_mutex != nullptr) draw_lock.unlock(); - CSleep(1); - if (_draw_mutex != nullptr) draw_lock.lock(); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto next_tick = std::min(next_draw_tick, next_game_tick); + auto now = std::chrono::steady_clock::now(); + + if (next_tick > now) { + if (_draw_mutex != nullptr) draw_lock.unlock(); + std::this_thread::sleep_for(next_tick - now); + if (_draw_mutex != nullptr) draw_lock.lock(); + } } } diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 2fe5f67f0..42fcf3f4b 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1258,13 +1258,20 @@ void VideoDriver_Win32::MainLoop() CheckPaletteAnim(); } + /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ if (!_fast_forward || _pause_mode) { - /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */ - GdiFlush(); + /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */ + auto next_tick = std::min(next_draw_tick, next_game_tick); + auto now = std::chrono::steady_clock::now(); - if (_draw_mutex != nullptr) draw_lock.unlock(); - CSleep(1); - if (_draw_mutex != nullptr) draw_lock.lock(); + if (next_tick > now) { + /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */ + GdiFlush(); + + if (_draw_mutex != nullptr) draw_lock.unlock(); + std::this_thread::sleep_for(next_tick - now); + if (_draw_mutex != nullptr) draw_lock.lock(); + } } } |