Callback function in freeglut from object

守給你的承諾、 提交于 2019-12-05 04:07:07

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.

You can't connect it directly. ReshapeCamera is a member function, it needs a camera instance in order to run. The glut callback is a C function, which don't have an instance of camera to call your method. You need to create a function that call the camera reshape method.

It has nothing to do with a "C vs C++" thing. You just need to understand what a member function call entails and basically what it compiles down to.

myObject.memberFunction(); // This is what the programmer sees.
memberFunction(&myObject); // This is what the compiler sees.

A member function is a fancy description of a function that simply takes the object as a first parameter. It's just invisible in the actual parameter list.

void MyClass::memberFunction() // This is the declaration the programmer sees.
void memberFunction(MyClass* this) // This is what the compiler sees.

From there, C++ adds special semantics to make it easy to work in an object-oriented fashion. So, even though your object has a member function of the format void(int, int), it actually has the format void(Camera*, int, int), and that does not match the desired format! This is why static function members can be used for such function pointers. Ever notice that static member functions cannot access the members of "this"? That is because static member functions do not pass an instance of the object representing "this".

In effect, you could emulate much of object-oriented programming in plain C. If you make a set of functions that take a struct pointer as the first parameter, you gain many of the same advantages!

Scott Stensland

The following demonstrates how to register a c callback function from c++ Its generally useful, not specific to glut.

Here is your client c++ program

int main(int argc, char *argv[]) {

std::cout << "launching Camera ..." << std::endl;

Camera * camera = new Camera();

// ------ glut new window boilerplate ------- //

int WindowHandle = 0;
glutInit(&argc, argv);
WindowHandle = glutCreateWindow("hello there");
if(WindowHandle < 1) {
    std::cerr << "ERROR: Could not create a new rendering window" << std::endl;
    exit(EXIT_FAILURE);
}
// ------------------------------------------ //

camera->setup_callback();

glutMainLoop();

return 0;

}

Here is the Camera.cpp

Camera * ptr_global_instance = NULL;

extern "C" void ReshapeCamera_callback(int width, int height) {
    // c function call which calls your c++ class method
    ptr_global_instance->ReshapeCamera_cb(width, height);
}

void Camera::ReshapeCamera_cb(int width, int height) {
    std::cout << "width " << width << " height " << height << std::endl;
}

void Camera::setup_callback() {
    // c++ method which registers c function callback
    ::ptr_global_instance = this;
    ::glutReshapeFunc(::ReshapeCamera_callback);
}

and its header Camera.h

class Camera {
public:
    void ReshapeCamera_cb(int width, int height);
    void setup_callback();
};

Notice the use of a c++ global class pointer ptr_global_instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!