4

i'm having trouble using an object in a glut DisplayFunction.

class Modelisation
{
private:
    int hauteur, largeur, x, y;
    Camera *Cam;

    void DisplayFunction ();
    static void RedisplayFunction (int, int);

public:
    Modelisation (int argc, char **argv, char[]);
    ~Modelisation ();

    void StartMainLoop();
};

Modelisation.cpp

Modelisation::Modelisation (int argc, char **argv, char windowName [])
{
    Cam = new Camera;
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutCreateWindow (windowName);
};
void Modelisation::StartMainLoop()
{
    glutDisplayFunc(DisplayFunction);
    glutIdleFunc(DisplayFunction);
    glutReshapeFunc(RedisplayFunction);
    glutMainLoop(); 
}
void Modelisation::DisplayFunction()
{
    glClearDepth (1);
    glClearColor (0.0f, 0.0f, 0.0f, 0.0f); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    glLoadIdentity ();
    Cam->Render ();
    glFlush ();
    glutSwapBuffers ();
}

glutDisplayFunc(DisplayFunction); glutIdleFunc(DisplayFunction);

This doesn't work. I know that i can declare DisplayFunction as a static member, but this won't allow me to use the Cam Object, any idea ?

Thx !!!

Athanase
  • 933
  • 9
  • 25

3 Answers3

0

In C++, data members and methods that a static method uses must also be declared static. The easiest way out of this is to declare Cam to be static.

You'll also have to initialize it statically, that is, in your implementation file:

Modelisation::Camera* Cam = new Camera();

(Note that, depending on how else Cam is used, you might open yourself up to the static initialization fiasco.)

Adrian
  • 1,842
  • 13
  • 25
  • I'm sorry to ask that but how do i declare a static object Cam in a header file ? – Athanase Apr 18 '11 at 22:10
  • You declare it as `static Camera* Cam;` in your header file. – Adrian Apr 18 '11 at 22:24
  • Sorry, i made a mistake, this works perfectly. I always get perfect answer when i ask questions on this forum. Thank you !! – Athanase Apr 18 '11 at 22:30
  • Great! If you're happy with an answer, then you can mark it as "accepted" to let everyone know. The person who answered also gets reputation points. :) – Adrian Apr 18 '11 at 22:57
0

You can not do that, because your void DisplayFunction (); is not static, and glutDisplayFunc expects a function pointer. Change your Modelisation class to this :

class Modelisation
{
private:
    int hauteur, largeur, x, y;
    Camera *Cam;

    static void DisplayFunction ();
    static void RedisplayFunction (int, int);

public:
    Modelisation (int argc, char **argv, char[]);
    ~Modelisation ();

    void StartMainLoop();
};

and it will work

BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

What you are trying to do is essentially to use a non-static member function as a C callback, which is one of the ridiculous hard parts of C++.

A good overview why this doesn't work easily in C++03 is given in this StackOverflow question and examples to work around.

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156