diff --git a/include/window.hpp b/include/window.hpp index c4834c930..b5d77af01 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -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 s_currShortcut = { -1, -1 }; std::list m_recentFiles; diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 228c40e57..85626690b 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -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(setting) : 60; + + if (ImGui::SliderInt(name.data(), &fps, 15, 60)) { + setting = fps; + return true; + } + + return false; + }); + } } \ No newline at end of file diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index b948d308c..9e2b8da40 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -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" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 8d2d05a42..5bc6ec5cc 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -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" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index ac2d797ef..d540f6eca 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -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" }, diff --git a/source/window.cpp b/source/window.cpp index 6212eb177..92f39bfa4 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -3,9 +3,11 @@ #include #include +#include #include #include #include +#include #include #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(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() {