From 9dc29a75b416c6dab27ce93d0129383309cbf2c2 Mon Sep 17 00:00:00 2001 From: HybridDog <3192173+HybridDog@users.noreply.github.com> Date: Sat, 3 Oct 2020 18:33:51 +0200 Subject: Reduce the FPS when the window is unfocused (#8837) --- src/client/game.cpp | 7 ++++--- src/defaultsettings.cpp | 4 ++-- src/gui/guiEngine.cpp | 17 +++++++++-------- src/gui/guiEngine.h | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/client/game.cpp b/src/client/game.cpp index 920383aaf..8f9d51417 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -3996,9 +3996,10 @@ inline void Game::limitFps(FpsControl *fps_timings, f32 *dtime) else fps_timings->busy_time = 0; - u32 frametime_min = 1000 / (g_menumgr.pausesGame() - ? g_settings->getFloat("pause_fps_max") - : g_settings->getFloat("fps_max")); + u32 frametime_min = 1000 / ( + device->isWindowFocused() && !g_menumgr.pausesGame() + ? g_settings->getFloat("fps_max") + : g_settings->getFloat("fps_max_unfocused")); if (fps_timings->busy_time < frametime_min) { fps_timings->sleep_time = frametime_min - fps_timings->busy_time; diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 3a0b88dc2..8f5ed3e17 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -165,7 +165,7 @@ void set_default_settings(Settings *settings) settings->setDefault("tooltip_show_delay", "400"); settings->setDefault("tooltip_append_itemname", "false"); settings->setDefault("fps_max", "60"); - settings->setDefault("pause_fps_max", "20"); + settings->setDefault("fps_max_unfocused", "20"); settings->setDefault("viewing_range", "100"); #if ENABLE_GLES settings->setDefault("near_plane", "0.1"); @@ -477,7 +477,7 @@ void set_default_settings(Settings *settings) settings->setDefault("max_block_generate_distance", "5"); settings->setDefault("enable_3d_clouds", "false"); settings->setDefault("fps_max", "30"); - settings->setDefault("pause_fps_max", "10"); + settings->setDefault("fps_max_unfocused", "10"); settings->setDefault("max_objects_per_block", "20"); settings->setDefault("sqlite_synchronous", "1"); settings->setDefault("server_map_save_interval", "15"); diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index b40707d01..4a13f0b11 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -297,10 +297,14 @@ void GUIEngine::run() driver->endScene(); + IrrlichtDevice *device = RenderingEngine::get_raw_device(); + u32 frametime_min = 1000 / (device->isWindowFocused() + ? g_settings->getFloat("fps_max") + : g_settings->getFloat("fps_max_unfocused")); if (m_clouds_enabled) - cloudPostProcess(); + cloudPostProcess(frametime_min, device); else - sleep_ms(25); + sleep_ms(frametime_min); m_script->step(); @@ -367,9 +371,8 @@ void GUIEngine::cloudPreProcess() } /******************************************************************************/ -void GUIEngine::cloudPostProcess() +void GUIEngine::cloudPostProcess(u32 frametime_min, IrrlichtDevice *device) { - float fps_max = g_settings->getFloat("pause_fps_max"); // Time of frame without fps limit u32 busytime_u32; @@ -380,12 +383,10 @@ void GUIEngine::cloudPostProcess() else busytime_u32 = 0; - // FPS limiter - u32 frametime_min = 1000./fps_max; - + // FPS limit if (busytime_u32 < frametime_min) { u32 sleeptime = frametime_min - busytime_u32; - RenderingEngine::get_raw_device()->sleep(sleeptime); + device->sleep(sleeptime); } } diff --git a/src/gui/guiEngine.h b/src/gui/guiEngine.h index f9ad0fb0a..e5b3edce7 100644 --- a/src/gui/guiEngine.h +++ b/src/gui/guiEngine.h @@ -277,7 +277,7 @@ private: /** do preprocessing for cloud subsystem */ void cloudPreProcess(); /** do postprocessing for cloud subsystem */ - void cloudPostProcess(); + void cloudPostProcess(u32 frametime_min, IrrlichtDevice *device); /** internam data required for drawing clouds */ struct clouddata { -- cgit v1.2.3