From 1e7c6e3f492fd250d8d062ce9eb79447884d6125 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 27 Sep 2022 17:32:01 +0200 Subject: [PATCH] Handle iconification by waiting for events. --- profiler/src/BackendGlfw.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/profiler/src/BackendGlfw.cpp b/profiler/src/BackendGlfw.cpp index 8fa06fc8..a05baee1 100644 --- a/profiler/src/BackendGlfw.cpp +++ b/profiler/src/BackendGlfw.cpp @@ -16,6 +16,7 @@ static GLFWwindow* s_window; static std::function s_redraw; static RunQueue* s_mainThreadTasks; static WindowPosition* s_winPos; +static bool s_iconified; static void glfw_error_callback( int error, const char* description ) { @@ -45,6 +46,11 @@ static void glfw_window_maximize_callback( GLFWwindow*, int maximized ) s_winPos->maximize = maximized; } +static void glfw_window_iconify_callback( GLFWwindow*, int iconified ) +{ + s_iconified = iconified != 0; +} + Backend::Backend( const char* title, std::function redraw, RunQueue* mainThreadTasks ) { @@ -79,10 +85,12 @@ Backend::Backend( const char* title, std::function redraw, RunQueue* mai s_redraw = redraw; s_mainThreadTasks = mainThreadTasks; s_winPos = &m_winPos; + s_iconified = false; glfwSetWindowPosCallback( s_window, glfw_window_pos_callback ); glfwSetWindowSizeCallback( s_window, glfw_window_size_callback ); glfwSetWindowMaximizeCallback( s_window, glfw_window_maximize_callback ); + glfwSetWindowIconifyCallback( s_window, glfw_window_iconify_callback ); } Backend::~Backend() @@ -104,18 +112,20 @@ void Backend::Run() { while( !glfwWindowShouldClose( s_window ) ) { - glfwPollEvents(); - if( glfwGetWindowAttrib( s_window, GLFW_ICONIFIED ) ) + if( s_iconified ) { - std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); - continue; + glfwWaitEvents(); } - s_redraw(); - if( !glfwGetWindowAttrib( s_window, GLFW_FOCUSED ) ) + else { - std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); + glfwPollEvents(); + s_redraw(); + if( !glfwGetWindowAttrib( s_window, GLFW_FOCUSED ) ) + { + std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); + } + s_mainThreadTasks->Run(); } - s_mainThreadTasks->Run(); } }