Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLFW poll waits until window resize finished, how to fix?

Tags:

c++

window

glfw

I'm working on a GLFW application and I noticed a problem when resizing the window.

While the user is scaling the window, the glfwPollEvents() function waits until the user finishes. This means that, during the scaling of the window, the render function isn't called and horrible artifacts are created.

I've worked around this by calling the render function from the main loop as well as the window resize callback:

#include <GLFW/glfw3.h>

static void render(GLFWwindow* window) {
    glfwMakeContextCurrent(window);

    glClear(GL_COLOR_BUFFER_BIT);

    glfwSwapBuffers(window);
}

static void window_size_callback(GLFWwindow* window, int width, int height) {
    render(window);
    glViewport(0, 0, width, height);
}

int main(void) {
    if (!glfwInit()) {
        return -1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "Terrain chunk render", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwSetWindowSizeCallback(window, window_size_callback);

    while (!glfwWindowShouldClose(window)) {
        render(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

However, this means that the render function isn't called when the user is holding down the mouse button after having scaled the window without actually moving it. Is there an efficient way of working around this and having the render function called more consistently?

like image 234
cjfaure Avatar asked May 25 '14 18:05

cjfaure


2 Answers

Use two threads, one for polling events and one for rendering. You also might want to use glfwWaitEvents intstead of glfwPollEvents to block the event thread until events are available.

like image 187
Alex - GlassEditor.com Avatar answered Nov 20 '22 03:11

Alex - GlassEditor.com


You should move the glfwSetWindowSizeCallback(window, window_size_callback); into your while loop so that the render function is called while you are re-sizing the window. I had the same problem a long time ago.

Edit: It's not recommended to set callbacks in the loop. The frames just need to be redrawn while the window is being resized.

like image 20
FrostyZombi3 Avatar answered Nov 20 '22 05:11

FrostyZombi3