mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpigui] Limit frame rate to 120 fps by default (#5030)
Limiting with vsync is apparently unreliable on a number of systems; this resulted in high CPU/GPU usage. Also add current actual frame rate to about dialog of GUI tools.
This commit is contained in:
@@ -4,9 +4,13 @@
|
||||
|
||||
#include "wpigui.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <IconsFontAwesome6.h>
|
||||
@@ -88,6 +92,8 @@ static void IniReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
|
||||
impl->userScale = num;
|
||||
} else if (std::strncmp(lineStr, "style=", 6) == 0) {
|
||||
impl->style = num;
|
||||
} else if (std::strncmp(lineStr, "fps=", 4) == 0) {
|
||||
impl->fps = num;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,9 +104,10 @@ static void IniWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
|
||||
}
|
||||
out_buf->appendf(
|
||||
"[MainWindow][GLOBAL]\nwidth=%d\nheight=%d\nmaximized=%d\n"
|
||||
"xpos=%d\nypos=%d\nuserScale=%d\nstyle=%d\n\n",
|
||||
"xpos=%d\nypos=%d\nuserScale=%d\nstyle=%d\nfps=%d\n\n",
|
||||
gContext->width, gContext->height, gContext->maximized ? 1 : 0,
|
||||
gContext->xPos, gContext->yPos, gContext->userScale, gContext->style);
|
||||
gContext->xPos, gContext->yPos, gContext->userScale, gContext->style,
|
||||
gContext->fps);
|
||||
}
|
||||
|
||||
void gui::CreateContext() {
|
||||
@@ -331,6 +338,8 @@ bool gui::Initialize(const char* title, int width, int height,
|
||||
void gui::Main() {
|
||||
// Main loop
|
||||
while (!glfwWindowShouldClose(gContext->window) && !gContext->exit) {
|
||||
double startTime = glfwGetTime();
|
||||
|
||||
// Poll and handle events (inputs, window resize, etc.)
|
||||
glfwPollEvents();
|
||||
gContext->isPlatformRendering = true;
|
||||
@@ -351,6 +360,15 @@ void gui::Main() {
|
||||
io.WantSaveIniSettings = false; // reset flag
|
||||
}
|
||||
}
|
||||
|
||||
// if FPS limiting, sleep until next frame time
|
||||
if (gContext->fps != 0) {
|
||||
double sleepTime = (1.0 / gContext->fps) - (glfwGetTime() - startTime);
|
||||
if (sleepTime > 1e-6) {
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::microseconds(static_cast<int64_t>(sleepTime * 1e6)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save (if custom save)
|
||||
@@ -477,6 +495,10 @@ void gui::SetStyle(Style style) {
|
||||
}
|
||||
}
|
||||
|
||||
void gui::SetFPS(int fps) {
|
||||
gContext->fps = fps;
|
||||
}
|
||||
|
||||
void gui::SetClearColor(ImVec4 color) {
|
||||
gContext->clearColor = color;
|
||||
}
|
||||
@@ -539,6 +561,27 @@ void gui::EmitViewMenu() {
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("Frame Rate")) {
|
||||
bool selected;
|
||||
selected = gContext->fps == 0;
|
||||
if (ImGui::MenuItem("vsync", nullptr, &selected)) {
|
||||
gContext->fps = 0;
|
||||
}
|
||||
selected = gContext->fps == 30;
|
||||
if (ImGui::MenuItem("30 fps", nullptr, &selected)) {
|
||||
gContext->fps = 30;
|
||||
}
|
||||
selected = gContext->fps == 60;
|
||||
if (ImGui::MenuItem("60 fps", nullptr, &selected)) {
|
||||
gContext->fps = 60;
|
||||
}
|
||||
selected = gContext->fps == 120;
|
||||
if (ImGui::MenuItem("120 fps", nullptr, &selected)) {
|
||||
gContext->fps = 120;
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (!gContext->saveSettings) {
|
||||
ImGui::MenuItem("Reset UI on Exit?", nullptr, &gContext->resetOnExit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user