jkuhlmann / gainput

Cross-platform C++ input library supporting gamepads, keyboard, mouse, touch
http://gainput.johanneskuhlmann.de/
MIT License
858 stars 103 forks source link

OpenGL SwapBuffers interferes with input detection. #45

Closed FeikoJoosten closed 7 years ago

FeikoJoosten commented 7 years ago

Hi,

So I have created a window using GLFW, I'm using the glfw win32 window handle to get the required HWND value. Whenever I setup my OpenGL renderer to contain a swap buffer (with a swap interval of 1) the input of the mouse is barely detected, and the input of the keys are pretty much non-existent

Here's an example of my OpenGL window creation

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwSetErrorCallback(error_callback);

if (!glfwInit())
{
    glfwTerminate();
    exit(EXIT_FAILURE);
}

window = glfwCreateWindow(width, height, title, nullptr, nullptr);

int major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
int minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
int revision = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
std::cout << "OpenGL Version " << major << "." << minor << "." << revision << std::endl;

glfwSetWindowUserPointer(window, this);

if (!window)
{
    glfwTerminate();
    exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(window);
glfwSwapInterval(1);

glfwGetFramebufferSize(window, &width, &height);

And here's an example of my render function

glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//Main render loop

glfwSwapBuffers(window);
glfwPollEvents();

If I remove the buffer swap it works as intended. Is there someone that could help me out with this? (I'm probably doing something wrong).

FeikoJoosten commented 7 years ago

Figured out my problem. Apparently the glfwPollEvents() is a heavy call. Commenting that one out fixed the issue!