Skip to content

Commit 871054d

Browse files
committed
Skip rendering when window is minimized
Avoid rendering and buffer swaps while the main GLFW window is iconified or has zero-size framebuffer. The main loop now checks GLFW_ICONIFIED and sleeps briefly to reduce CPU usage, obtains ImGui draw data and tests DisplaySize, and only performs glViewport/clear/ImGui_ImplOpenGL3_RenderDrawData and glfwSwapBuffers when the window and framebuffer sizes are valid. This prevents rendering with zero-sized viewports and reduces wasted work when the window is minimized.
1 parent 699ba5a commit 871054d

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

Solidify/src/main.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,30 @@ main(int argc, char* argv[])
213213
while (!glfwWindowShouldClose(window)) {
214214
glfwPollEvents();
215215

216+
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0) {
217+
ImGui_ImplGlfw_Sleep(10);
218+
continue;
219+
}
220+
216221
ImGui_ImplOpenGL3_NewFrame();
217222
ImGui_ImplGlfw_NewFrame();
218223
ImGui::NewFrame();
219224

220225
RenderUI();
221226

222227
ImGui::Render();
228+
ImDrawData* drawData = ImGui::GetDrawData();
229+
const bool mainMinimized = drawData->DisplaySize.x <= 0.0f || drawData->DisplaySize.y <= 0.0f;
230+
223231
int display_w = 0;
224232
int display_h = 0;
225233
glfwGetFramebufferSize(window, &display_w, &display_h);
226-
glViewport(0, 0, display_w, display_h);
227-
glClearColor(0.45f, 0.55f, 0.60f, 1.0f);
228-
glClear(GL_COLOR_BUFFER_BIT);
229-
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
234+
if (!mainMinimized && display_w > 0 && display_h > 0) {
235+
glViewport(0, 0, display_w, display_h);
236+
glClearColor(0.45f, 0.55f, 0.60f, 1.0f);
237+
glClear(GL_COLOR_BUFFER_BIT);
238+
ImGui_ImplOpenGL3_RenderDrawData(drawData);
239+
}
230240

231241
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != 0) {
232242
GLFWwindow* backup_current_context = glfwGetCurrentContext();
@@ -235,7 +245,9 @@ main(int argc, char* argv[])
235245
glfwMakeContextCurrent(backup_current_context);
236246
}
237247

238-
glfwSwapBuffers(window);
248+
if (!mainMinimized && display_w > 0 && display_h > 0) {
249+
glfwSwapBuffers(window);
250+
}
239251
}
240252

241253
dnd_glfw::shutdown(window);

0 commit comments

Comments
 (0)