Detect obscured window

Hi,
My problem is identical to the one here, partially solved:
–> stackoverflow.com/questions/23550423/high-cpu-usage-when-glfw-opengl-window-isnt-visible

GLFW uses 100% CPU when the gl-window is obscured, either by another window or minimized.
By using glfwSetWindowIconifyCallback, I am able to detect a minimized gl-window and reduce CPU usage, but I unable to do the same when another window covers the gl-window.
… and losing focus, as a trigger, is not an option for me either.

Is there any work-around for this?

Note that it is not GLFW using 100% CPU, but your application.

If you want to only run your game/render loop when an event occurs, you can use glfwWaitEvents() or glfwWaitEventsTimeout().

There isn’t a way to know when your application is obscured, but you can detect if you have input focus. So you could run your render loop freely when you have input focus, and then use glfwWaitEvents() when you don’t.

Here is the relevant issue. You can subscribe to that for progress updates.

https://github.com/glfw/glfw/issues/680

If the issue @elmindreda describes is the one affecting you, I’d implement a soft-vsync using sleep when windowed as a workaround, this works well for me.

@dougbinks

Thank you. Yes some laziness in the description there. In actuality it’s not using 100% either, more like 65%. Using input focus won’t work for the program I’m building, which is more like a “player” (ie VLC) than a video game. I want it to run continuously.

How might I implement soft-vsync? I’m not looking for hand-holding here, just something to start with for trial-and-error.

@elmindreda

Yes that is the issue exactly. I am also using OSX.
I will follow that thread, and the references to “application code” & “NSWindowDidChangeOcclusionStateNotification” are useful in the meantime. Thank you.

The soft approach simply uses usleep.

So you get the current frame time, and calculate (desired_frametime - frametime) then convert to microseconds (multiply by a million) and usleep for that amount of time if it’s a positive value.