glfwSetCursorPosCallback在另一个类中起作用 [英] glfwSetCursorPosCallback to function in another class

查看:382
本文介绍了glfwSetCursorPosCallback在另一个类中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被卡住了:

我有mainWindow,在主游戏循环中,我这样做了:

I have the mainWindow and in the main game loop I do:

// poll for input
glfwPollEvents();

this->controls->handleInput(window, world->getPlayer());
glfwSetCursorPosCallback(window, controls->handleMouse);

我要做的是让一个类负责控件,并让这个类也处理

What I want to do is to have one class responsible for the controls and to have this class also handle the mouse.

我总是得到:

'Controls::handleMouse': function call missing argument list; use '&Controls::handleMouse' to create a pointer to member

的指针get:

'GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow *,GLFWcursorposfun)' : cannot convert argument 2 from 'void (__thiscall Controls::* )(GLFWwindow *,double,double)' to 'GLFWcursorposfun'

不确定我在做什么这是错误的,因为GLFWcursorposfun只是一个带有GLFWwindow和两个双精度的typedef。

Not sure what I'm doing wrong here, as GLFWcursorposfun is just a typedef with a GLFWwindow and two doubles.

由于该函数在另一个类中,因此我尝试为其创建原型,例如:

As the function is in another class I tried creating a prototype for it, like:

class Controls {
    void handleInput(GLFWwindow *window, object *gameObject);
    void handleMouse(GLFWwindow *window, double mouseXPos, double mouseYPos);
};

但无济于事。

编辑:当然,如果我将该功能设为静态,则可以将其设置为& Controls :: handleMouse,但我希望能够使用它们操作的不同相机和游戏对象创建多个Controls-Object。

edit: Of course I can set it to &Controls::handleMouse if I make the function static, but I'd rather be able to create multiple Controls-Objects with different cameras and gameObjects they manipulate.

另外,如何获取正确的相机/ gameObject数据呢?

Also, how to get in the correct camera/gameObject data then?

推荐答案

将类的成员函数作为函数传递。 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 );

这篇关于glfwSetCursorPosCallback在另一个类中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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