Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreground FPS limit #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
#define CFG_VID_DEFDISPLAY "-1"
#define CFG_VID_DEFFULLSCREEN "0"
#define CFG_VID_DEFRESIZABLE "0"
#define CFG_VID_DEFFGFPS "30"
#define CFG_VID_DEFBGFPS "5"
#define CFG_VID_MINWIDTH 1080
#define CFG_VID_MINHEIGHT 720
6 changes: 6 additions & 0 deletions engine/core/core_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class core_video_c: public core_IVideo, public conCmdHandler_c {
conVar_c* vid_fullscreen;
conVar_c* vid_resizable;
conVar_c* vid_last;
conVar_c* vid_fgfps;
conVar_c* vid_bgfps;

void C_Vid_Apply(IConsole* conHnd, args_c &args);
void C_Vid_ModeList(IConsole* conHnd, args_c &args);
Expand All @@ -71,6 +73,8 @@ core_video_c::core_video_c(sys_IMain* sysHnd)
vid_fullscreen = sys->con->Cvar_Add("vid_fullscreen", CV_ARCHIVE, CFG_VID_DEFFULLSCREEN);
vid_resizable = sys->con->Cvar_Add("vid_resizable", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFRESIZABLE, 0, 3);
vid_last = sys->con->Cvar_Add("vid_last", CV_ARCHIVE, "");
vid_fgfps = sys->con->Cvar_Add("vid_fgfps", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFFGFPS, 5, 120);
vid_bgfps = sys->con->Cvar_Add("vid_bgfps", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFBGFPS, 5, 120);

Cmd_Add("vid_apply", 0, "", this, &core_video_c::C_Vid_Apply);
Cmd_Add("vid_modeList", 0, "", this, &core_video_c::C_Vid_ModeList);
Expand Down Expand Up @@ -111,6 +115,8 @@ void core_video_c::Apply(bool shown)
set.depth = 0;
set.minSize[0] = CFG_VID_MINWIDTH;
set.minSize[1] = CFG_VID_MINHEIGHT;
set.fgfps = vid_fgfps->intVal;
set.bgfps = vid_bgfps->intVal;
sys->video->Apply(&set);
}

Expand Down
4 changes: 4 additions & 0 deletions engine/system/sys_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct sys_vidSave_s {
int size[2] = {};
int pos[2] = {};
bool maximised = false;
int fgfps = 0;
int bgfps = 0;
};

// Video settings structure
Expand All @@ -32,6 +34,8 @@ struct sys_vidSet_s {
int mode[2] = {}; // Resolution or window size
int depth = 0; // Bit depth
int minSize[2] = {}; // Minimum size for resizable windows
int fgfps = 0; // Foreground FPS limit
int bgfps = 0; // Background FPS limit
sys_vidSave_s save; // Saved state
};

Expand Down
3 changes: 3 additions & 0 deletions engine/system/win/sys_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ int sys_video_c::Apply(sys_vidSet_s* set)
vid.size[0] = wrec.right;
vid.size[1] = wrec.bottom;

vid.fgfps = cur.fgfps;
vid.bgfps = cur.bgfps;

// Process any messages generated during application
sys->RunMessages();

Expand Down
20 changes: 16 additions & 4 deletions ui_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "ui_local.h"

#include <chrono>
#include <thread>

// ======
// Locals
// ======
Expand Down Expand Up @@ -373,15 +376,16 @@ void ui_main_c::ScriptInit()

void ui_main_c::Frame()
{
const auto frameStart = std::chrono::high_resolution_clock::now();
auto fpsLimit = sys->video->vid.fgfps;
if (!sys->video->IsVisible() || sys->conWin->IsVisible() || restartFlag || didExit) {
framesSinceWindowHidden = 0;
}
else if (framesSinceWindowHidden <= 10) {
framesSinceWindowHidden++;
}
else if (!sys->video->IsActive() && !sys->video->IsCursorOverWindow()) {
sys->Sleep(100);
return;
fpsLimit = sys->video->vid.bgfps;
}

if (renderer) {
Expand Down Expand Up @@ -425,8 +429,8 @@ void ui_main_c::Frame()
}

//sys->con->Printf("Finishing up...\n");
if ( !sys->video->IsActive() ) {
sys->Sleep(100);
if ( sys->video->IsActive() ) {
fpsLimit = sys->video->vid.fgfps;
}

while (restartFlag) {
Expand All @@ -436,6 +440,14 @@ void ui_main_c::Frame()
}
ScriptInit();
}

using FpMilliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>;
const auto minimumFrameDuration = FpMilliseconds(1000.0f / fpsLimit);
const auto frameEnd = std::chrono::high_resolution_clock::now();
const auto frameDuration = std::chrono::duration_cast<FpMilliseconds>(frameEnd - frameStart);
if (frameDuration < minimumFrameDuration) {
std::this_thread::sleep_for(minimumFrameDuration - frameDuration);
}
}

void ui_main_c::ScriptShutdown()
Expand Down