GLUT Celestia C++ challengeing trouble

The place to discuss creating, porting and modifying Celestia's source code.
Topic author
Paolo
Posts: 502
Joined: 23.09.2002
With us: 22 years 2 months
Location: Pordenone/Italy

GLUT Celestia C++ challengeing trouble

Post #1by Paolo » 05.03.2006, 22:47

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!
Remember: Time always flows, it is the most precious thing that we have.
My Celestia - Celui

Christophe
Developer
Posts: 944
Joined: 18.07.2002
With us: 22 years 4 months
Location: Lyon (France)

Post #2by Christophe » 05.03.2006, 23:36

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.
Christophe

Topic author
Paolo
Posts: 502
Joined: 23.09.2002
With us: 22 years 2 months
Location: Pordenone/Italy

Post #3by Paolo » 05.03.2006, 23:45

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
Remember: Time always flows, it is the most precious thing that we have.

My Celestia - Celui

Christophe
Developer
Posts: 944
Joined: 18.07.2002
With us: 22 years 4 months
Location: Lyon (France)

Post #4by Christophe » 06.03.2006, 00:06

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.
Christophe

Topic author
Paolo
Posts: 502
Joined: 23.09.2002
With us: 22 years 2 months
Location: Pordenone/Italy

Post #5by Paolo » 06.03.2006, 09:47

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
Remember: Time always flows, it is the most precious thing that we have.

My Celestia - Celui

Christophe
Developer
Posts: 944
Joined: 18.07.2002
With us: 22 years 4 months
Location: Lyon (France)

Post #6by Christophe » 06.03.2006, 11:36

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.
Christophe


Return to “Development”