Skip to content

Commit

Permalink
Merge pull request #328 from sphaero/idle_fps
Browse files Browse the repository at this point in the history
enable a lower idle fps when not receiving input
  • Loading branch information
ikbenmacje committed Apr 24, 2024
2 parents 1a2ac4a + d9a53f0 commit bd7b925
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/AboutWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions app/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace gzb {

struct App
{
// settings
unsigned int fps = 60;
unsigned int idle_fps = 10;
// windows
ImGuiTextBuffer log_buffer;
std::vector<TextEditorWindow*> text_editors;
AboutWindow about_win;
Expand Down
15 changes: 10 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit bd7b925

Please sign in to comment.