1
0
mirror of synced 2024-09-24 19:48:25 +02:00

sys: Added FPS limit, some power saving

#189
This commit is contained in:
WerWolv 2021-03-06 13:09:20 +01:00
parent 4f98149fa7
commit 6cba868e20
6 changed files with 38 additions and 1 deletions

View File

@ -46,9 +46,12 @@ namespace hex {
float m_globalScale = 1.0f, m_fontScale = 1.0f;
bool m_fpsVisible = false;
double m_targetFps;
bool m_demoWindowOpen = false;
bool m_layoutConfigured = false;
double m_lastFrameTime;
static inline std::tuple<int, int> s_currShortcut = { -1, -1 };
std::list<std::string> m_recentFiles;

View File

@ -61,6 +61,17 @@ namespace hex::plugin::builtin {
return false;
});
ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.fps", 60, [](auto name, nlohmann::json &setting) {
static int fps = setting.is_number() ? static_cast<int>(setting) : 60;
if (ImGui::SliderInt(name.data(), &fps, 15, 60)) {
setting = fps;
return true;
}
return false;
});
}
}

View File

@ -463,6 +463,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.setting.interface.color.light", "Hell" },
{ "hex.builtin.setting.interface.color.classic", "Klassisch" },
{ "hex.builtin.setting.interface.language", "Sprache" },
{ "hex.builtin.setting.interface.fps", "FPS Limite" },
{ "hex.builtin.provider.file.path", "Dateipfad" },
{ "hex.builtin.provider.file.size", "Größe" },

View File

@ -463,6 +463,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.setting.interface.color.light", "Light" },
{ "hex.builtin.setting.interface.color.classic", "Classic" },
{ "hex.builtin.setting.interface.language", "Language" },
{ "hex.builtin.setting.interface.fps", "FPS Limit" },
{ "hex.builtin.provider.file.path", "File path" },
{ "hex.builtin.provider.file.size", "Size" },

View File

@ -463,6 +463,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.setting.interface.color.light", "Chiaro" },
{ "hex.builtin.setting.interface.color.classic", "Classico" },
{ "hex.builtin.setting.interface.language", "Lingua" },
{ "hex.builtin.setting.interface.fps", "FPS limite" },
{ "hex.builtin.provider.file.path", "Percorso del File" },
{ "hex.builtin.provider.file.size", "Dimensione" },

View File

@ -3,9 +3,11 @@
#include <hex.hpp>
#include <hex/api/content_registry.hpp>
#include <chrono>
#include <iostream>
#include <numeric>
#include <typeinfo>
#include <thread>
#include <imgui.h>
#define IMGUI_DEFINE_MATH_OPERATORS
@ -26,6 +28,8 @@
namespace hex {
using namespace std::literals::chrono_literals;
void *ImHexSettingsHandler_ReadOpenFn(ImGuiContext *ctx, ImGuiSettingsHandler *, const char *) {
return ctx; // Unused, but the return value has to be non-null
}
@ -57,7 +61,7 @@ namespace hex {
this->initGLFW();
this->initImGui();
EventManager::subscribe(Events::SettingsChanged, this, [](auto) -> std::any {
EventManager::subscribe(Events::SettingsChanged, this, [this](auto) -> std::any {
{
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
@ -91,6 +95,13 @@ namespace hex {
LangEntry::loadLanguage(static_cast<std::string>(language));
}
{
auto targetFps = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.fps");
if (targetFps.is_number())
this->m_targetFps = targetFps;
}
return { };
});
@ -156,6 +167,7 @@ namespace hex {
}
void Window::loop() {
this->m_lastFrameTime = glfwGetTime();
while (!glfwWindowShouldClose(this->m_window)) {
this->frameBegin();
@ -245,6 +257,10 @@ namespace hex {
}
void Window::frameBegin() {
if (!glfwGetWindowAttrib(this->m_window, GLFW_VISIBLE) || glfwGetWindowAttrib(this->m_window, GLFW_ICONIFIED))
glfwWaitEvents();
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
@ -373,6 +389,10 @@ namespace hex {
glfwMakeContextCurrent(backup_current_context);
glfwSwapBuffers(this->m_window);
while (glfwGetTime() < this->m_lastFrameTime + (1 / this->m_targetFps))
std::this_thread::sleep_for(500us);
this->m_lastFrameTime = glfwGetTime();
}
void Window::drawWelcomeScreen() {