C++ GLFWSETCURSOR回调到另一个类中的函数

C++ GLFWSETCURSOR回调到另一个类中的函数,c++,callback,glfw,C++,Callback,Glfw,我真的被卡住了: 我有主窗口,在主游戏循环中我做: // poll for input glfwPollEvents(); this->controls->handleInput(window, world->getPlayer()); glfwSetCursorPosCallback(window, controls->handleMouse); 我想做的是让一个类负责控件,让这个类也处理鼠标 我总是得到: 'Controls::handleMouse': func

我真的被卡住了:

我有主窗口,在主游戏循环中我做:

// poll for input
glfwPollEvents();

this->controls->handleInput(window, world->getPlayer());
glfwSetCursorPosCallback(window, controls->handleMouse);
我想做的是让一个类负责控件,让这个类也处理鼠标

我总是得到:

'Controls::handleMouse': function call missing argument list; use '&Controls::handleMouse' to create a pointer to member
现在,当我尝试这个时,我得到:

'GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow *,GLFWcursorposfun)' : cannot convert argument 2 from 'void (__thiscall Controls::* )(GLFWwindow *,double,double)' to 'GLFWcursorposfun'
不确定我在这里做错了什么,因为GLFWcursorposfun只是一个带有GLFWwindow和两个double的typedef

由于函数在另一个类中,我尝试为它创建一个原型,如:

class Controls {
    void handleInput(GLFWwindow *window, object *gameObject);
    void handleMouse(GLFWwindow *window, double mouseXPos, double mouseYPos);
};
但是没有用

编辑:当然,如果我将函数设置为静态,我可以将其设置为&Controls::handleMouse,但我更希望能够使用不同的相机和游戏对象创建多个控件对象


另外,如何获取正确的相机/游戏对象数据?

不能将类的成员函数作为函数传递。glfwsetcursorpcallback它需要一个函数并抛出错误,因为它得到了一个成员函数

换句话说,您希望提供一个全局函数并将其传递给glfwSetCursorPosCallback

如果确实希望控件对象获得光标位置回调,可以将控件实例存储在全局变量中,并将回调传递给该实例。大概是这样的:

static Controls* g_controls;

void mousePosWrapper( double x, double y )
{
    if ( g_controls )
    {
        g_controls->handleMouse( x, y );
    }
}
然后,当您调用GLFWSETCURSOR或OSCALLBACK时,可以传递mousepowrapper函数:

glfwSetCursorPosCallback( window, mousePosWrapper );

不能将类的成员函数作为函数传递。glfwsetcursorpcallback它需要一个函数并抛出错误,因为它得到了一个成员函数

换句话说,您希望提供一个全局函数并将其传递给glfwSetCursorPosCallback

如果确实希望控件对象获得光标位置回调,可以将控件实例存储在全局变量中,并将回调传递给该实例。大概是这样的:

static Controls* g_controls;

void mousePosWrapper( double x, double y )
{
    if ( g_controls )
    {
        g_controls->handleMouse( x, y );
    }
}
然后,当您调用GLFWSETCURSOR或OSCALLBACK时,可以传递mousepowrapper函数:

glfwSetCursorPosCallback( window, mousePosWrapper );

我的解决方案:不要使用回调设置器。相反,我做了以下工作:

glfwPollEvents();

this->controls->handleInput(window, mainObj);
this->controls->handleMouse(window, mainObj);
在扶手屋里,我会:

GLdouble xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);

我的解决方案:不要使用回调设置器。相反,我做了以下工作:

glfwPollEvents();

this->controls->handleInput(window, mainObj);
this->controls->handleMouse(window, mainObj);
在扶手屋里,我会:

GLdouble xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);

另一种解决方案是将指向控件的指针与GLFWindow关联。看

指针可以通过从GLFWWindow对象中检索。当然,返回类型是void*,必须转换为Controls*

可以使用全局函数或静态方法来代替。e、 g:

glfwSetWindowUserPointerwindow,此->控件; GLFWSetCursorPostCallback窗口,[]GLFWwindow*窗口,双x,双y { 如果Controls*Controls=static\u castglfwGetWindowUserPointerwindow 控件->扶手鼠标窗口,x,y; } ;
另一种解决方案是将指向控件的指针与GLFWindow关联。看

指针可以通过从GLFWWindow对象中检索。当然,返回类型是void*,必须转换为Controls*

可以使用全局函数或静态方法来代替。e、 g:

glfwSetWindowUserPointerwindow,此->控件; GLFWSetCursorPostCallback窗口,[]GLFWwindow*窗口,双x,双y { 如果Controls*Controls=static\u castglfwGetWindowUserPointerwindow 控件->扶手鼠标窗口,x,y; } ;
你当然是对的。这是相当不灵活的,所以我避免使用回调函数,并决定最好实际地主动地抓住光标位置。当然你是对的。这是相当不灵活的,所以我避免使用回调函数,并决定最好实际主动地抓住光标位置。您可以看到我对这个问题的回答,简而言之,它使用了glfwSetWindowUserPointer:您可以看到我对这个问题的回答,简而言之,它使用glfwSetWindowUserPointer: