SaveTool: add a frame limiter.

The FPS slider isn't the most accurate, but it just works™.
Closes #10.
This commit is contained in:
Guillaume Jacquemin 2021-08-18 16:53:29 +02:00
parent c4fc910ab0
commit 52f5e8eb0a
3 changed files with 92 additions and 0 deletions

View file

@ -136,6 +136,19 @@ SaveTool::SaveTool(const Arguments& arguments):
initialiseConfiguration();
switch(_framelimit) {
case Framelimit::Vsync:
setSwapInterval(1);
break;
case Framelimit::HalfVsync:
setSwapInterval(2);
break;
case Framelimit::FpsCap:
setSwapInterval(0);
setMinimalLoopPeriod(1000/_fpsCap);
break;
}
if(_checkUpdatesOnStartup) {
_thread = std::thread{[this]{ checkForUpdates(); }};
_queue.addToast(Toast::Type::Default, "Checking for updates...");
@ -148,6 +161,19 @@ SaveTool::~SaveTool() {
_conf.setValue("cheat_mode", _cheatMode);
_conf.setValue("unsafe_mode", _unsafeMode);
_conf.setValue("startup_update_check", _checkUpdatesOnStartup);
switch(_framelimit) {
case Framelimit::Vsync:
_conf.setValue("frame_limit", "vsync");
break;
case Framelimit::HalfVsync:
_conf.setValue("frame_limit", "half_vsync");
break;
case Framelimit::FpsCap:
_conf.setValue<UnsignedInt>("frame_limit", _fpsCap);
break;
}
_conf.save();
}
@ -379,6 +405,23 @@ void SaveTool::initialiseConfiguration() {
_conf.setValue("startup_update_check", _checkUpdatesOnStartup);
}
if(_conf.hasValue("frame_limit")) {
std::string frame_limit = _conf.value("frame_limit");
if(frame_limit == "vsync") {
_framelimit = Framelimit::Vsync;
}
else if(frame_limit == "half_vsync") {
_framelimit = Framelimit::HalfVsync;
}
else {
_framelimit = Framelimit::FpsCap;
_fpsCap = std::stoul(frame_limit);
}
}
else {
_conf.setValue("frame_limit", "vsync");
}
_conf.save();
}

View file

@ -197,6 +197,13 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener
};
Containers::StaticArray<2, efsw::WatchID> _watchIDs;
enum class Framelimit: UnsignedByte {
Vsync,
HalfVsync,
FpsCap
} _framelimit{Framelimit::Vsync};
UnsignedInt _fpsCap{60};
bool _checkUpdatesOnStartup{true};
bool _unsafeMode{false};

View file

@ -55,6 +55,48 @@ void SaveTool::drawMainMenu() {
ImGui::Separator();
if(ImGui::BeginMenu(ICON_FA_COG " Settings")) {
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted("Frame limiter:");
ImGui::SameLine();
static UnsignedByte selection = static_cast<UnsignedByte>(_framelimit);
static const char* framelimit_labels[3] = {
"V-sync",
"Half V-sync",
"FPS cap, no V-sync"
};
ImGui::SetNextItemWidth(150.0f);
if(ImGui::BeginCombo("##FrameLimit", framelimit_labels[selection])) {
if(ImGui::Selectable(framelimit_labels[0], _framelimit == Framelimit::Vsync)) {
selection = 0;
_framelimit = Framelimit::Vsync;
setSwapInterval(1);
}
if(ImGui::Selectable(framelimit_labels[1], _framelimit == Framelimit::HalfVsync)) {
selection = 1;
_framelimit = Framelimit::HalfVsync;
setSwapInterval(2);
}
if(ImGui::Selectable(framelimit_labels[2], _framelimit == Framelimit::FpsCap)) {
selection = 2;
_framelimit = Framelimit::FpsCap;
setSwapInterval(0);
setMinimalLoopPeriod(1000 / _fpsCap);
}
ImGui::EndCombo();
}
if(_framelimit == Framelimit::FpsCap) {
static constexpr UnsignedInt min_fps = 15;
static constexpr UnsignedInt max_fps = 150;
if(ImGui::SliderScalar("##FpsSlider", ImGuiDataType_U32, &_fpsCap, &min_fps, &max_fps, "%u FPS", ImGuiSliderFlags_AlwaysClamp)) {
setMinimalLoopPeriod(1000 / _fpsCap);
}
}
ImGui::Checkbox("Cheat mode", &_cheatMode);
ImGui::SameLine();
ImGui::AlignTextToFramePadding();