Don't run at max speed without vsync to reduce CPU usage

This commit is contained in:
Zachary Hall 2023-07-17 17:35:21 -07:00
parent cafced3bdb
commit 6541988fa1

View file

@ -18,6 +18,7 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_image.h> #include <SDL_image.h>
#include <filesystem> #include <filesystem>
#include <SDL_video.h>
#if defined(IMGUI_IMPL_OPENGL_ES2) #if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL_opengles2.h> #include <SDL_opengles2.h>
#else #else
@ -202,6 +203,7 @@ int main(int, char**)
bool theme_editor = false; bool theme_editor = false;
bool stopped = true; bool stopped = true;
bool vsync = false; bool vsync = false;
int framerate = 60;
{ {
Json::Value config; Json::Value config;
std::ifstream stream; std::ifstream stream;
@ -224,6 +226,9 @@ int main(int, char**)
if (config.isMember("vsync")) { if (config.isMember("vsync")) {
vsync = config["vsync"].asBool(); vsync = config["vsync"].asBool();
} }
if (config.isMember("framerate")) {
framerate = config["framerate"].asUInt();
}
stream.close(); stream.close();
} }
if (is_empty(Theme::themeDir)) { if (is_empty(Theme::themeDir)) {
@ -262,6 +267,7 @@ int main(int, char**)
SDL_SetWindowMinimumSize(window, 475, min_y); SDL_SetWindowMinimumSize(window, 475, min_y);
} }
}*/ }*/
auto next_frame = std::chrono::steady_clock::now() + std::chrono::milliseconds(1000 / framerate);
position = playback->GetPosition(); position = playback->GetPosition();
// Poll and handle events (inputs, window resize, etc.) // Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
@ -377,6 +383,9 @@ int main(int, char**)
if (ImGui::Checkbox("Enable VSync", &vsync)) { if (ImGui::Checkbox("Enable VSync", &vsync)) {
SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_SetSwapInterval(vsync ? 1 : 0);
} }
ImGui::SameLine();
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - ImGui::GetStyle().WindowPadding.x);
ImGui::SliderInt("##Framerate", &framerate, 10, 480, "Max framerate without VSync: %d");
if (ImGui::Button(ICON_FK_MAGIC "Theme Editor", ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), 0))) { if (ImGui::Button(ICON_FK_MAGIC "Theme Editor", ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), 0))) {
theme_editor = true; theme_editor = true;
} }
@ -463,6 +472,9 @@ int main(int, char**)
} }
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
if (!vsync) {
std::this_thread::sleep_until(next_frame);
}
} }
// Cleanup // Cleanup
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
@ -491,6 +503,7 @@ int main(int, char**)
config["accent_color"] = accent_color; config["accent_color"] = accent_color;
config["demo_window"] = show_demo_window; config["demo_window"] = show_demo_window;
config["vsync"] = vsync; config["vsync"] = vsync;
config["framerate"] = framerate;
stream << config; stream << config;
stream.close(); stream.close();
} }