PDA

View Full Version : Glut callback functions & Wrappers



malikcpp
06-13-2012, 03:39 AM
Hello everyone, i'm really disturbed with a issue that i'm working on since two or three days.
Here is the context :



class MyClass {
...
};

MyClass::display(void) {
...
}


I'v a class MyClass, with a method display(). Then i'm declaring everything for my object that i call object. And what i want to do is some glutDisplayFunc(). So i'm using some wrappers to avoid errors of kind


void (*) () expected instead of void (MyClass::) ()

.. here is a solution for people who were stucked there :



struct MyWrapper {
static void display(void) {
u->display();
}
static MyClass * u;
};

MyClass MyWrapper::u = 0;

...

int main ( .. ) {

...

MyWrapper::u = &object;
glutDisplayFunc(MyWrapper::display);

...

}


Fortunately i'v passed this issue, but now what i want is implementing multiple MyClass objects in multiple windows..
But modifying the structure by simply trying to make arrays of object or something like that will stuck because of we need a reference to the concerned object. (here is this version (http://hastebin.com/lajevofexo.coffee))

I'v thought of functions pointers but i'v telled that it will not work.

Thank you for your help.

For the moment i'm using a limited amount of object wich should work until the amount of objects is not dynamically set ..

thokra
06-13-2012, 05:05 AM
Why do you want to use a member-function pointer of a variable number of objects when GLUT only allows one function being the active callback at a time anyway?

Why not implement a single update function which selects thew object dynamically based on some criterion(criteria)?

Dark Photon
06-13-2012, 06:00 AM
...now what i want is implementing multiple MyClass objects in multiple windows..
As thokra said, you can do what you want with a shared "display" function. At the top of this function, just lookup your class instance pointer from something unique to the window you're rendering to, such as the window ID (e.g. glutGetWindow). In fact, what you're trying to do is a specific use case listed in the glutGetWindow man page:

* http://linux.die.net/man/3/glutgetwindow

malikcpp
06-13-2012, 06:31 AM
What i know is that each created window should have it's own glut callback functions, so i do not see how your solutions should be implemented.
Or maybe i did not understand how Glut is managing multiple windows.

But thank you for your answers and i will try to implement these tonight, and i will post a feedback.

Alfonse Reinheart
06-13-2012, 08:03 AM
What i know is that each created window should have it's own glut callback functions, so i do not see how your solutions should be implemented.

The callback setting functions (generally) work with the current window. If you have a GLUT window, and you want to set its display callback, you must first call `glutSetWindow`with that window, then call `glutDisplayFunc`. A lot like regular OpenGL.

You still can't use member pointers though. So you'll have to give each window a global function, each of which calls a specific window instance's display func.

Dark Photon
06-14-2012, 05:45 AM
...i do not see how your solutions should be implemented. Or maybe i did not understand how Glut is managing multiple windows.



#include <map>

std::map< int, Obj * > Window_obj;

void display()
{
Obj *obj = Window_obj[ glutGetWindow() ];
...
}

void createWindows()
{
Window_obj[ glutCreateWindow() ] = obj1;
Window_obj[ glutCreateWindow() ] = obj2;
...
}


Tweak to taste.