Jump to content
  • Advertisement
Sign in to follow this  
Togakangaroo

question about passing functions in glut

This topic is 4943 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

Ok, I hope I'm not being an asshole by skipping over the FAQ and just posting my question, but I figure its pretty specific and just merits a quick answer. I am also pretty new to graphis programming so bear with me etc. etc. Now then. I am making a terrain map generator/display. In order to avoid having to declare a bunch of global variables and just keep overall neatness at a maximum I tried putting as much as I can in an general GLInterface class (that I wrote). So very basically my program looks like this. class myclass { ///stuff }; class GLInterface { myclass c1; //otherstuff void RenderScene() { //stuff } void setupinterface() { //change window size and other stuff glutDisplayFunc(GLInterface::RenderScene); } startLoop(){ glutMainLoop(); } }; int main() { GLInterface GLI; GLI.setupinterface(); GLI.startLoop(); } However the line glutDisplayFunc(RenderScene); will not complile. It will not compile if I do not scope it with GLInterface:: and it will not compile if I put it in the main glutDispalyFunc(GLI.RenderScene); Please tell me that there is some way short of making RenderScene a global function to get this to work! Thanks a lot. George

Share this post


Link to post
Share on other sites
Advertisement
The glut callbacks are suppose to be plain non-method, functions. Methods(class-functions) and plain functions do not have the same signature so therefore you can't pass a class method as a function pointer. Try it out without glut. You'll get the same errors.

Share this post


Link to post
Share on other sites
The only way to bypass your little problem is if the function inside the class were static. Since static methods have the same signature as non-class functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Khaosifix
The glut callbacks are suppose to be plain non-method, functions. Methods(class-functions) and plain functions do not have the same signature so therefore you can't pass a class method as a function pointer. Try it out without glut. You'll get the same errors.

True and not true[smile].
It would work perfectly if the member functions are static. Since they aren't one can use std::mem_fn together with std::bind1st to achieve this (or boost.bind).

Regards,
Pat.

[edit]
Oh, you already noticed - just ignore the first part [smile]
[/edit]

Share this post


Link to post
Share on other sites
Since IMHO the STL is a bit clumsy on the bind1st and mem_fn part, I decided to just post an example:


class GLInterface {
myclass c1;
//otherstuff
void RenderScene() {
//stuff
}
void setupinterface() {
//change window size and other stuff
glutDisplayFunc(std::bind1st(std::mem_fun(&GLInterface::RenderScene), this));
}

void startLoop() {
glutMainLoop();
}
};



Hope this helps,
Pat.

Share this post


Link to post
Share on other sites
thanks a lot, I'll try it right now and see what happens.
I'm afraid I don't know what it would mean to have a static method (I mean I know I could put the word static there but I don't know waht it would do) could someone explain that to me so I could see if that method would be applicable?

Share this post


Link to post
Share on other sites
darookie, what you recomended std::bind1st(std::mem_fun(&GLInterface::RenderScene), this)
didn't work problem with second argument type as best as I can decipher from the compiler error

Share this post


Link to post
Share on other sites
Hope this helps :


class CClass
{

public :

static GLvoid RenderScene( ) ; // declare it as static in the class

} ;

GLvoid CClass::RenderScene( )
{ // Important step : define the function outside of the class. Can't be inside.
// Draw stuff.
}

void main( )
{
// Function calls are a little odd. But this is how you call a static method.
CClass::RenderScene( ) ;
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Togakangaroo
thanks a lot, I'll try it right now and see what happens.
I'm afraid I don't know what it would mean to have a static method (I mean I know I could put the word static there but I don't know waht it would do) could someone explain that to me so I could see if that method would be applicable?

You could use the PIMPL idiom to achieve that:


class ActualImplementation {
private:
// declare data
public:
// methods
void doSomething();
};

class PublicInterface {
private:
static ActualImplementation *pimpl;
public:
PublicInterface() {
if (0 == pimpl) {
pimpl = new ActualImplementation;
}
}
~PublicInterface() {
delete pimpl;
pimpl = 0;
}

static void doSomething() {
// dispatch to actual implementation
assert (pimpl);
pimpl->doSomething();
}
};




You can use PublicInterface::doSomething like a free function now and pass it as a callback to GLUT for example.

Regards,
Pat.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!