2017-07-23 44 views
-1

出於某種原因,我的Window::callback即使在鼠標離開窗口後仍被調用。我無法找到解決方案,甚至找不到有用的東西。 GLFW可能更新了鼠標光標回調的操作方式嗎?我想知道這是否是一個調用問題的順序?鼠標離開GLFWwindow後觸發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; 
} 
+0

我既不能重現這一點,也沒有在文檔[glfwSetCursorPosCallback](http://www.glfw.org/docs/latest/group__input.html#ga7dad39486f2c7591af7fb25134a2501d)中顯示。向我們展示[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 – Rabbid76

+0

@ Rabbid76我添加了所需材料來推斷可能是什麼問題。 – Matthew

回答

0

我想通了這個問題(或最好把)的問題是什麼。在Windows上,回調按預期執行,一旦鼠標離開窗口區域,回調停止觸發。對於OSX,窗口永遠不會失去焦點,因此總是調用遊標回調。要解決這個問題,您只需測試座標以確保鼠標實際上位於窗口內。

相關問題