• Advertisement

Archived

This topic is now archived and is closed to further replies.

getting glut to accept class members as callbacks

This topic is 6213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a game, and I want to have the whole thing inside a class called TheGame. I was planning on putting the gl and glut init stuff inside of the constructor, and using member functions as drawing, resizing, and input callbacks. However, glut does not seem to allow that.
    TheGame::TheGame()
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(400, 50);					
	glutCreateWindow("The Window");
	
	glutDisplayFunc(drawFrame);	//this is TheGame::drawFrame()

	glutReshapeFunc(resizeDisplay); //and TheGame::resizeDisplay()

}     
When I try to compile this this is the error I get: error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)' I get it for each of the callback setting functions. When I change drawFrame and resizeDisplay to _not_ be class members, it works perfectly. But that means that I'd have to have a set of functions that kind of crutch TheGame along instead of having it run all by itself. Is there anyway to change this? And what does __cdecl mean? I looked it up on MSDN and it said something about being MS specific.. I've never seen it before. Thanks for any help /riley Edited by - rileyriley on February 19, 2001 10:18:03 AM Edited by - rileyriley on February 19, 2001 10:19:21 AM

Share this post


Link to post
Share on other sites
Advertisement
__cdecl is a calling convention.

A calling convention is a way how the parameters of a function are pushed to the stack and all that other assembly stuff so I won't explain that here because I don't like to type that much.
Look into the MSDN for more information about it.

__cdecl and/or __stdcall are normally used for most functions but when using classes there's another convention used: thiscall.
That's because classes have their functions attached to the this pointer.

    
eg.

void CExample::FirstFunction()
{
// When you're calling a function from this class...

SecondFunction();

// ...you're actually doing this:

this->SecondFunction();

// You can also use the last one if you like but it's just

// more typing.

// Sometimes it can be handy to call it with the this pointer

// when there are 2 functions with the same name: 1

// in the class and 1 global function.

// Then just call this->TheFunction() and the function in this

// class will be executed instead of the global function.

}



However, when using callbacks there's one little tiny small problem: you can't attach a function pointer meant for __cdecl to a pointer declared with thiscall.
So we need a way to get around this problem.

The solution is simple: make the callbacks in your class static!
But now there's another problem.. when a member function is static you can't acces the member variables.
There are various ways to get around this problem but that depends on what you like most.


// this...

void TheGame::drawFrame();

// ...should be like this when using callbacks in classes

static void TheGame::drawFrame();

// NOTE: You only have to do this in front of the declaration.

// (header)




Hope I've explained it well enough...


Edited by - richardve on February 19, 2001 12:13:56 PM

Share this post


Link to post
Share on other sites
ahhh, thanks a lot. The sense of "I shoulda known that" is so strong right now that I really can''t decide if I remember thinking of trying the static thing before. It definitely feels like it but I can''t think of any reason I would have changed it back so it must be my ego telling me I really _am_ smart.

Thanks again
/riley

Share this post


Link to post
Share on other sites

  • Advertisement