Skip to content

Commit

Permalink
Reduce the FPS when the window is unfocused (#8837)
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog committed Oct 3, 2020
1 parent 4b423ee commit 9dc29a7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions builtin/settingtypes.txt
Expand Up @@ -606,8 +606,8 @@ arm_inertia (Arm inertia) bool true
# to not waste CPU power for no benefit.
fps_max (Maximum FPS) int 60 1

# Maximum FPS when game is paused.
pause_fps_max (FPS in pause menu) int 20 1
# Maximum FPS when the window is not focused, or when the game is paused.
fps_max_unfocused (FPS when unfocused or paused) int 20 1

# Open the pause menu when the window's focus is lost. Does not pause if a formspec is
# open.
Expand Down
7 changes: 4 additions & 3 deletions src/client/game.cpp
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/defaultsettings.cpp
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
17 changes: 9 additions & 8 deletions src/gui/guiEngine.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/guiEngine.h
Expand Up @@ -277,7 +277,7 @@ class GUIEngine {
/** 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 {
Expand Down

0 comments on commit 9dc29a7

Please sign in to comment.