Real-time rendering, especially with games, can have a rather high CPU and GPU usage. Thereby if the user minimizes (iconifies) the window, or otherwise stops interacting with the application. Then it would be beneficial to halt all rendering, until the user resumes interaction.

This post will focus on iconified windows. So how can this be done? The short answer is, listen for the related window events and block the game loop when the window is iconified.

The concepts of implementing this is similar across frameworks. Conceptually it looks like this (Rust):

loop {
    // Update timing

    if window_iconified {
        wait_events(); // Blocking
    } else {
        poll_events(); // Non-blocking
    }

    // Iterate all pending events
    for event in events() {
        // If event is an iconify event
        // then set window_iconified accordingly
    }

    if window_iconified {
        // Update logic that should update while the window is iconified
    } else {
        // Update all logic
    }

    if window_iconified {
        // The window is iconified, so no need to render
        continue;
    }

    // Render
    // Swap buffers
}

The following example is implemented with GLFW in Rust. The snippet is tested against the following version of the glfw crate:

glfw = "0.29.0"

The following example assumes window is an instance of glfw::Window.

window.set_iconify_polling(true);

let mut window_iconified = window.is_iconified();

loop {
    // Update timing

    if window_iconified {
        glfw.wait_events();
    } else {
        glfw.poll_events();
    }

    for (_, event) in glfw::flush_messages(&events) {
        match event {
            glfw::WindowEvent::Iconify(iconified) => {
                window_iconified = iconified;
            },
            _ => (),
        }
    }

    if window_iconified {
        // Update logic that should update while the window is iconified
    } else {
        // Update all logic
    }

    if window_iconified {
        continue;
    }

    // Render

    window.swap_buffers();
}