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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With