Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High CPU usage when GLFW / OpenGL Window isn't visible

Tags:

c

opengl

3d

glfw

I'm using GLFW so set up a Window with OpenGL. As i'm just started learning OpenGL and all the stuff around it, this might sound like a silly question, but why is the example program of GLFW using nearly 100% CPU when the Window is not actively displayed (minimized or hidden by another Window)?

Here is the GLFW exmaple, i'm running it on Mac OS with Xcode:

#include <GLFW/glfw3.h>
int main(void)
{
    GLFWwindow* window;

    if (!glfwInit())  /* Initialize the library */
        return -1;

/* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
       glfwTerminate();
        return -1;
    }

/* Make the window's context current */
    glfwMakeContextCurrent(window);

/* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
like image 204
Bastian Seeleib Avatar asked May 08 '14 19:05

Bastian Seeleib


People also ask

Do I need OpenGL for GLFW?

By default, the OpenGL context GLFW creates may have any version. You can require a minimum OpenGL version by setting the GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR hints before creation. If the required minimum version is not supported on the machine, context (and window) creation fails.

What version of OpenGL does GLFW use?

Explicit creation of OpenGL contexts of version 3.0 and above on Windows and X11, including profiles and flags, is supported by GLFW 2.7 and later.

What is hint in GLFW?

Window creation hints. There are a number of hints that can be set before the creation of a window and context. Some affect the window itself, others affect the framebuffer or context. These hints are set to their default values each time the library is initialized with glfwInit.


2 Answers

Your render loop is executed no matter what mimimization state of your window is.

If you want to stop rendering, you have to enhace the application logic a bit to track the state your window is in. GLFW supports user-defined a callback for such things with glfwSetWindowIconifyCallback() so your application can get noticed when the window is minimized or restored. You can the decide to stop the render loop, and can use glfwWaitEvents() to wait for something to happen (like the window being restored) without using all available CPU time.

like image 66
derhass Avatar answered Sep 22 '22 08:09

derhass


Maybe start doing something?

Or use glfwWaitEvents(); instead of glfwPollEvents(); to block when there isn't new events.

The documentation explains it on the first step : http://www.glfw.org/docs/latest/quick.html

like image 22
Kabe Avatar answered Sep 22 '22 08:09

Kabe