鼠标离开GLFWwindow后触发了GLFW鼠标回调? [英] GLFW mouse callback fired after mouse leave GLFWwindow?

查看:264
本文介绍了鼠标离开GLFWwindow后触发了GLFW鼠标回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For some reason my Window::callback is being called even after the mouse has left the window. I am unable to find a solution or even something that could help. Is it possible that GLFW updated how the mouse cursor callback operates? I wonder if it is an order of invocation problem?

Window

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;
}


推荐答案

我已经弄清楚了问题(或更合适地说)是。在Windows上,回调将按预期执行,一旦鼠标离开窗口区域,回调就会停止触发。对于OSX,窗口永远不会失去焦点,因此总是调用游标回调。要解决此问题,您只需测试坐标以确保鼠标实际上位于窗口内。

I have figured out what the problem (or better put) issue is. On Windows the callback performs as expected where once the mouse leaves the windows area the callback stops firing. For OSX the window never loses focus and therefore the cursor callback is always being called. To fix the issue you simply have to test the coordinates to ensure that the mouse is in fact inside the window.

这篇关于鼠标离开GLFWwindow后触发了GLFW鼠标回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆