PDA

View Full Version : Question about GLUT callbacks



bcyde
10-31-2001, 08:10 AM
I have written a small C++ class to be used with GLUT which has some member functions that I am attempting to use as the keyboard callbacks. When I try to compile I get the error:

C2664: 'glutKeyboardFunc' : cannot convert parameter 1 from 'void (unsigned char,int,int)' to 'void (__cdecl *)(unsigned char,int,int)'

I was wondering what I can do to fix this. The program works if I specify a keyboardfunc within my main source file that sets up glut and then simply call the member function from there but that's just a superfluous function call I don't want to do. Any insight on this would be greatly appreciated. Thanks in advance,

tfpsly
10-31-2001, 09:07 AM
Your class methods must be static.
Otherwise how could glut call method referring to a class instence it dosn't know?

Advice: make everything in your class static, both variables and methods

bcyde
10-31-2001, 09:29 AM
Thanks for your reply, what you suggested works. Although if you can elaborate on your answer a bit I would appreciate it.
You stated:

"Your class methods must be static.
Otherwise how could glut call method referring to a class instence it dosn't know?"

If in my main I have a class menu and instantiate and pass that glut still will not be able to call the member?
Such as :

SimpleMenu menu;
glutKeyboardFunc(menu.KeyBoardFunc);
glutSpecialFunc(menu.SpecialKeyFunc);

Clearly it is not the case that this will work (since it doesn't) but if you had the time and were willing I'd appreciate a little more info. I thought that since I was passing menu.KeyBoardFunc and not SimpleMenu::KeyBoardFunc it would use the class instance of menu.

Thanks for your advice any additional input is greatly appreciated.

tfpsly
10-31-2001, 09:54 AM
Let's say you have :
class Menu {
...
void menu.KeyBoardFunc(...);
}

Menu menu;

A call to menu.KeyBoardFunc in C++ is almost the same as this in C:
typedef struct {
...
} Menu;

void KeyBoardFunc( Menu *menu,....) {
}

KeyBoardFunc( &menu,... );

So it is not the kind of function that glutKeyboardFunc is waiting.

The trick is to use this :

class Menu {
static MenuItem item;
...
static KeyBoardFunc(...);
};

MenuItem::item; //static variables must be declared outside of the class to get created, like static methods.

int main() {

glutKeyboardFunc(Menu::KeyBoardFunc);

}

bcyde
10-31-2001, 10:27 AM
Thanks again!.