question about passing functions in glut

Started by
17 comments, last by darookie 19 years, 4 months ago
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
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.
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
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.
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
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]
^ [cool]
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
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.
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?
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
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( ) ;   }
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
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 datapublic:   // 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.

This topic is closed to new replies.

Advertisement