从对象的freeglut中回调函数 [英] Callback function in freeglut from object

查看:368
本文介绍了从对象的freeglut中回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MSVC ++和freeglut为了使用openGL。现在,我有一个类 Camera ,这是很简单,但它也拥有重新塑造我的窗口的功能。



我的问题是:如何设置 glutReshapeFunc(void(* callback)(int,int))到我的相机的功能?

我有以下代码,由于编译器错误,它不工作:

  int main(int argc,char ** argv)
{
Camera * camera = new Camera();
glutReshapeFunc(camera-> ReshapeCamera);
}

并且我的Camera类在 Camera.h

  class Camera 
{
public:
Camera
〜Camera(void);
void ReshapeCamera(int width,int height);
};

也许这只是一个更普遍的回调问题,但是我在互联网上发现的唯一的东西是创建一些包装类围绕着回调。但它看起来不像这应该是那么难。
提前感谢。

解决方案

没有干净的方法。 C不知道对象,所以指针到成员不工作。 C不做模板,所以函子不工作。 API甚至不允许你传递一个任意的 void * userData 到你的回调,所以你也不能以这种方式传递对象。



事实上,你必须创建一个不是成员函数的包装函数,你必须让它访问 Camera 对象实例,通过一个静态和/或全局变量。



如果可以有多个对象侦听这个事件,你可以创建一个单例类允许您注册任意侦听器。


I'm using MSVC++ and freeglut in order to use openGL. Now, I have a class called Camera, which is pretty simple, but it also holds the function for the reshaping of my window.

My question is: how can I set glutReshapeFunc(void (*callback)(int,int)) to my function in my camera?

I have the following code, which won't work, because of a compiler error:

int main(int argc, char **argv)
{
    Camera *camera = new Camera();
    glutReshapeFunc(camera->ReshapeCamera);
}

and my Camera class looks like this in Camera.h:

class Camera
{
public:
    Camera(void);
    ~Camera(void);
    void ReshapeCamera(int width, int height);
};

Maybe this is just a more general callback question, but the only thing I found on the internet was creating some wrapper class around the callback. But it doesn't look like this should be so hard. Thanks in advance.

解决方案

There's no clean way to do this. C does not know about objects, so pointers-to-members don't work. C does not do templates, so functors don't work. The API does not even allow you to pass an arbitrary void *userData to your callback, so you can't pass the object around in that way either.

So, indeed, you'll have to create a wrapper function that is not a member function, and you'll have to give it access to the Camera object instance somehow, through a static and/or global variable.

If there can be multiple objects listening for this event, you could create a singleton class that allows you to register arbitrary listeners.

这篇关于从对象的freeglut中回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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