From d9a53f0a3d5234920d5fd8a6fe64ec8622aa7a0d Mon Sep 17 00:00:00 2001 From: Arnaud Loonstra Date: Tue, 16 Apr 2024 18:18:01 +0200 Subject: [PATCH] enable a lower idle fps when not receiving input, add fps to about window --- app/AboutWindow.cpp | 4 +++- app/App.hpp | 4 ++++ main.cpp | 15 ++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/AboutWindow.cpp b/app/AboutWindow.cpp index d5813fa..b08b5af 100644 --- a/app/AboutWindow.cpp +++ b/app/AboutWindow.cpp @@ -80,7 +80,7 @@ void AboutWindow::OnImGui() mouse_data.w = io.MouseDownDuration[1]; float time = (float)ImGui::GetTime(); FX(draw_list, p0, p1, size, mouse_data, time); - draw_list->AddText(ImGui::GetFont(), ImGui::GetFontSize()*4.f, p0 + ImVec2(16,120), ImColor::HSV(mouse_data.x,mouse_data.x,0.9), "GazebOsc"); + draw_list->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.f, p0 + ImVec2(16,120), ImColor::HSV(mouse_data.x,mouse_data.x,0.9), "GazebOsc"); //draw_list->AddText(p0 + size/2, IM_COL32(255,0,255,255), "GazebOsc"); //draw_list->AddCircleFilled( p0 + size/2, 10.f, IM_COL32_WHITE, 8); draw_list->PopClipRect(); @@ -142,6 +142,8 @@ void AboutWindow::OnImGui() } } + //printf("fps %.2f %i, Application average %.3f ms/frame (%.1f FPS)\n", 1000./(SDL_GetTicks() - oldTime), deltaTime, 1000.0f / io.Framerate, io.Framerate); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)\n", 1000.f/io.Framerate, io.Framerate); ImGui::End(); } diff --git a/app/App.hpp b/app/App.hpp index 36e83c9..c2ce920 100644 --- a/app/App.hpp +++ b/app/App.hpp @@ -20,6 +20,10 @@ namespace gzb { struct App { + // settings + unsigned int fps = 60; + unsigned int idle_fps = 10; + // windows ImGuiTextBuffer log_buffer; std::vector text_editors; AboutWindow about_win; diff --git a/main.cpp b/main.cpp index ab62d09..1059ba3 100644 --- a/main.cpp +++ b/main.cpp @@ -498,7 +498,8 @@ int SDLInit( SDL_Window** window, SDL_GLContext* gl_context, const char** glsl_v *window = SDL_CreateWindow("GazebOSC", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); *gl_context = SDL_GL_CreateContext(*window); SDL_GL_MakeCurrent(*window, *gl_context); - SDL_GL_SetSwapInterval(1); // Enable vsync + if ( SDL_GL_SetSwapInterval(-1) == -1) // Enable adaptive vsync + SDL_GL_SetSwapInterval(1); // Enable vsync return 0; } @@ -602,26 +603,30 @@ void UILoop( SDL_Window* window, ImGuiIO& io ) { // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. + bool had_events = false; + unsigned int timeout = 1000/app.idle_fps; // 10 fps while idle SDL_Event event; while (SDL_PollEvent(&event)) { + timeout = 1000/app.fps; // 60 fps while input events ImGui_ImplSDL2_ProcessEvent(&event); if (event.type == SDL_QUIT) stop = 1; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) stop = 1; } + // Get time since last frame + deltaTime = SDL_GetTicks() - oldTime; + // Start the Dear ImGui frame ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); - // Get time since last frame - deltaTime = SDL_GetTicks() - oldTime; // In here we can poll sockets // For when we send msgs to the main thread - if (deltaTime < 1000/30) - SDL_Delay((1000/30)-deltaTime); + if (deltaTime < timeout) + SDL_Delay(timeout-deltaTime); //printf("fps %.2f %i, Application average %.3f ms/frame (%.1f FPS)\n", 1000./(SDL_GetTicks() - oldTime), deltaTime, 1000.0f / io.Framerate, io.Framerate); oldTime = SDL_GetTicks();