C++ 鼠标离开GLFW窗口后启动GLFW鼠标回调?
由于某些原因,甚至在鼠标离开窗口后,仍会调用myC++ 鼠标离开GLFW窗口后启动GLFW鼠标回调?,c++,callback,glfw,C++,Callback,Glfw,由于某些原因,甚至在鼠标离开窗口后,仍会调用myWindow::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:
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);
}
Window::Window(标准::字符串标题、int32\t宽度、int32\t高度){
//TODO:添加对GLFW监视器和共享的支持
m_window=std::unique_ptr(glfwCreateWindow(宽度、高度、title.c_str()、nullptr、nullptr));
glfwMakeContextCurrent(m_window.get());
glfwSetWindowUserPointer(m_window.get(),this);
glfwsetcursorpcallback(m_window.get(),window::callback);
}
void Window::鼠标回调(双xpos,双YPO){
std::cout我已经找出了问题所在(或者更好地说)问题是。在Windows上,回调按预期执行,一旦鼠标离开窗口区域,回调将停止触发。对于OSX,窗口永远不会失去焦点,因此总是调用光标回调。要解决此问题,您只需测试坐标,以确保鼠标实际上在窗口内。@rabbidi添加了必要的材料来推断可能存在的问题。
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();
}
}
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;
}