Page 1 of 1

GLUT Celestia C++ challengeing trouble

Posted: 05.03.2006, 22:47
by Paolo
Hi all, I?€™m asking for help.
After a long time I'm dealing with GLUT - Celestia.
I would like to use freeglut and I would like to create a C++ wrapper for its initialization.

I?€™ve created the following objects:

Code: Select all

Class App
{
 public:
    App();

    virtual void __cdecl displayFn(void);
    virtual void __cdecl keyFn(unsighed char, int, int);
    ...

 private:

    virtual void init();
    ...

}

Class GLUTApp: public App
{
    ...

 private:
    void init();  //method override
    ...

}

 
// implementation
void GLUTApp::init()
{
    // inherited
    App::init();
    ...

    // Create the window
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(screenW, screenH);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
    glutInit(&argc, argv);

    hWnd = glutCreateWindow("FreeGLUT Celestia");

    // callbacks
    glutDisplayFunc(&uiGLUTApp::displayFn);      // * Error !!!!!!
    glutKeyboardFunc(&uiGLUTApp::keyFn);         // * Error !!!!!!
    glutMouseFunc(&uiGLUTApp::mouseFn);          // * Error !!!!!!
    glutMotionFunc(&uiGLUTApp::dragFn);          // * Error !!!!!!
    glutPassiveMotionFunc(&uiGLUTApp::motionFn);  // * Error !!!!!!
    ...

}


I?€™m Using Visual Studio and the error messages that the compiler returns are similar to the following:

c:\CVS_sandbox\celestia_dev\src\glutapp.cpp(196): error C2664: "glutDisplayFunc": impossible to convert parameter 1 from "void (__cdecl GLUTApp::* )(void)" to "void (__cdecl *)(void)"

Browsing the internet I've found the reason. A direct assignment to a function pointer using a pointer to a method is impossible. However since the problem is very complex I did not found understandable suggestions for a workaround.

Can please someone give a suggestion?

Kind regards!

Posted: 05.03.2006, 23:36
by Christophe
You have to make your methods static, function pointers on non static methods don't have any meaning since they may reference member variables which don't exist outside the context of an instance of the class.

Posted: 05.03.2006, 23:45
by Paolo
Hi Christophe

This is the solution that I've figured but that I wouldn't like to do.
Perhaps there isn't any other more "elegant" workaround.

Kind regards

Posted: 06.03.2006, 00:06
by Christophe
Hi Paolo,

Nop, I don't think there are any elegant solution here. C++ and function pointers just don't mix very well.

C++ offers other more elegant alternatives, function pointers are the old C way of doing things. If your API is C based and requires function pointers, you're stuck using ugly hacks. See the C++ FAQ.

Posted: 06.03.2006, 09:47
by Paolo
Hi Christophe

Perhaps you'll think that I'm a bit crazy, but the best hack that I'm figuring out should be converting freeglut in C++ and use "Functionoids" or "Functors" for the event callback system.
Of course the conversion should disregard any unuseful function (and IMHO there are many).
Do you think it should be a real mess?

Perhaps you'll think that there are a lot of better things to do! :lol: :lol: :lol:

Kind regards

Posted: 06.03.2006, 11:36
by Christophe
Of course derivation is the way the glut API would be implemented if it were in C++. Functionoids are just a special case, I'm not sure they are the best approach in this particular case. The way C++ toolkits handle this is rather through a base class:

Code: Select all

class freeGLUT {
...
protected:
virtual void display();
virtual void onKeypress(...);
virtual void onMouse(...);
...
}

class App:public freeGLUT {
...
}


See QGLWidget from Qt for example.

In any case, devising an object oriented API for GLUT is a major task, have you looked at GLOW? There's a list of GLUT alternatives here.