Handle iconification by waiting for events.

This commit is contained in:
Bartosz Taudul 2022-09-27 17:32:01 +02:00
parent ff8a85ea2d
commit 1e7c6e3f49
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -16,6 +16,7 @@ static GLFWwindow* s_window;
static std::function<void()> s_redraw; static std::function<void()> s_redraw;
static RunQueue* s_mainThreadTasks; static RunQueue* s_mainThreadTasks;
static WindowPosition* s_winPos; static WindowPosition* s_winPos;
static bool s_iconified;
static void glfw_error_callback( int error, const char* description ) 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; s_winPos->maximize = maximized;
} }
static void glfw_window_iconify_callback( GLFWwindow*, int iconified )
{
s_iconified = iconified != 0;
}
Backend::Backend( const char* title, std::function<void()> redraw, RunQueue* mainThreadTasks ) Backend::Backend( const char* title, std::function<void()> redraw, RunQueue* mainThreadTasks )
{ {
@ -79,10 +85,12 @@ Backend::Backend( const char* title, std::function<void()> redraw, RunQueue* mai
s_redraw = redraw; s_redraw = redraw;
s_mainThreadTasks = mainThreadTasks; s_mainThreadTasks = mainThreadTasks;
s_winPos = &m_winPos; s_winPos = &m_winPos;
s_iconified = false;
glfwSetWindowPosCallback( s_window, glfw_window_pos_callback ); glfwSetWindowPosCallback( s_window, glfw_window_pos_callback );
glfwSetWindowSizeCallback( s_window, glfw_window_size_callback ); glfwSetWindowSizeCallback( s_window, glfw_window_size_callback );
glfwSetWindowMaximizeCallback( s_window, glfw_window_maximize_callback ); glfwSetWindowMaximizeCallback( s_window, glfw_window_maximize_callback );
glfwSetWindowIconifyCallback( s_window, glfw_window_iconify_callback );
} }
Backend::~Backend() Backend::~Backend()
@ -104,18 +112,20 @@ void Backend::Run()
{ {
while( !glfwWindowShouldClose( s_window ) ) while( !glfwWindowShouldClose( s_window ) )
{ {
glfwPollEvents(); if( s_iconified )
if( glfwGetWindowAttrib( s_window, GLFW_ICONIFIED ) )
{ {
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); glfwWaitEvents();
continue;
} }
s_redraw(); else
if( !glfwGetWindowAttrib( s_window, GLFW_FOCUSED ) )
{ {
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();
} }
} }