glfw / glfw

A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input
https://www.glfw.org
zlib License
12.82k stars 5.15k forks source link

How can GLFW response to window events(keyboard, mouse move,...) quickly? #2158

Open aorandexiaohai opened 2 years ago

aorandexiaohai commented 2 years ago

I am doubting about the underlying mechanism of GLFW. The common pattern that use GLFW:

       while !glfwWindowShouldClose(window)
           glfwPollEvents();
           render();
           swapbuffer();

This is an endless loop. I think that it is difficult to response to window events quickly.

elmindreda commented 2 years ago

Have you measured and found this to be a problem?

You can render in a separate thread and have the main thread be solely for event processing, but the effects of any input would still need to be rendered after the input was received.

aorandexiaohai commented 2 years ago

@elmindreda

Are you sure that this method is adopted by glfw?

aorandexiaohai commented 2 years ago

It seems that the render thread and GUI thread are the same thread.

elmindreda commented 2 years ago

The example you gave is single-threaded. The common multi-threaded approach is to process events on the main thread and, while rendering and buffer swapping is done in a separate thread.

See the threads test program for an example of this.

aorandexiaohai commented 2 years ago

@elmindreda Maybe my description is not clear or you misunderstand me.

I think that glfw can response to keyboard/mouse events quickly when running the above code. But I think that it is impossible. So, I want to know about the implementation mechanism.