鼠标离开GLFWwindow后GLFW鼠标回调被触发?

时间:2017-07-23 13:20:50

标签: c++ callback glfw

由于某些原因,即使鼠标离开窗口,我的Window::callback也会被调用。我无法找到解决方案,甚至无法找到解决方案。 GLFW可能更新了鼠标光标回调的操作方式吗?我想知道这是否是一个调用问题的顺序?

窗口

Window::Window(std::string title, int32_t width, int32_t height) {
    // TODO: add support for monitor and share for GLFW
    m_window = std::unique_ptr<GLFWwindow, GLFWdeleter>(glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr));
    glfwMakeContextCurrent(m_window.get());
    glfwSetWindowUserPointer(m_window.get(), this);
    glfwSetCursorPosCallback(m_window.get(), Window::callback);
}

void Window::mouse_callback(double xpos, double ypos) {
    std::cout << "x: " << xpos << " y: " << ypos << std::endl;
}

void Window::callback(GLFWwindow* window, double xpos, double ypos)
{
    auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
    win->mouse_callback(xpos, ypos);
}

引擎

void startup() const
{
    if (glfwInit() == 0)
    {
        LOG(kError, "GLFW init failed!");
        exit(-1);
    }
}

void Engine::run() {
    if (m_main_window_registered)
    {
        glewExperimental = static_cast<GLboolean>(true);
        if (glewInit() != GLEW_OK)
        {
            std::cout << "Failed to initialize glew" << std::endl;
            return;
        }
    }

    while(glfwWindowShouldClose(m_main_window->window()) == 0) {
       glClear(GL_COLOR_BUFFER_BIT);
       glfwSwapBuffers(m_main_window->window());
       glfwPollEvents();
    }
}

的main.cpp

int main()
{
    g_engine.startup();

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    auto window = std::make_unique<Window>("Hello World!", 640, 480);
    //window->make_current();
    g_engine.registerWindow(std::move(window));
    g_engine.run();

    glfwTerminate();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我已经弄清楚问题(或更好的问题)是什么问题。在Windows上,回调按预期执行,一旦鼠标离开窗口区域,回调就会停止触发。对于OSX,窗口永远不会失去焦点,因此始终会调用光标回调。要解决这个问题,您只需测试坐标以确保鼠标实际上在窗口内。