且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

glfwSetCursorPosCallback在另一个类中起作用

更新时间:2023-01-08 14:49:38

将类的成员函数作为函数传递。 glfwSetCursorPosCallback 它期望一个函数并由于得到成员函数而引发错误。

You can't pass a class's member function as a function. glfwSetCursorPosCallback it's expecting a function and throwing the error because it gets a member function.

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

In other words your expected to provide a global function and pass that to glfwSetCursorPosCallback.

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

If you really want the controls object to get the cursor position callback you could store an instance of Controls in a global variable and pass on the callback to that instance. Something like this:

static Controls* g_controls;

void mousePosWrapper( double x, double y )
{
    if ( g_controls )
    {
        g_controls->handleMouse( x, y );
    }
}

然后在您调用 glfwSetCursorPosCallback时您可以通过 mousePosWrapper 函数:

Then when you call glfwSetCursorPosCallback you can pass the mousePosWrapper function:

glfwSetCursorPosCallback( window, mousePosWrapper );