GLFW poll waits until window resize finished, how to fix? [duplicate]
Asked Answered
S

2

6

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?

Selway answered 25/5, 2014 at 18:15 Comment(1)
I am not really sure, but have you checked the Windows system settings? If not, asuming Windows 7, you might try this as suggested and see how it goes.Vague
K
8

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.

Kanzu answered 18/4, 2015 at 5:18 Comment(0)
L
1

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.

Lactase answered 31/5, 2014 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.