Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ member functions & glut events -- incompatible??

0 views
Skip to first unread message

Alec Maki

unread,
Nov 30, 1999, 3:00:00 AM11/30/99
to
Using VC++ 6.0, I cannot compile the following:

MEnvironment *spong = new MEnvironment;
glutDisplayFunc(spong->display);
glutReshapeFunc(spong->reshape);
glutKeyboardFunc(spong->keyboard);
glutPassiveMotionFunc(spong->mouseFunc);
glutMotionFunc(spong->mouseFunc);
glutMainLoop();

MEnvironment is a class which contains the program's event functions.
Originally, I wrote the program using straight C code, and it worked fine.
Now, I'm trying to OOP it with C++. GLUT doesn't seem to like this,
however. Any ideas on how to get around this problem -- or should I keep
OpenGL events chained to C?

Help is appreciated -- thanks.

Paul Miller

unread,
Nov 30, 1999, 3:00:00 AM11/30/99
to
> MEnvironment is a class which contains the program's event functions.
> Originally, I wrote the program using straight C code, and it worked fine.
> Now, I'm trying to OOP it with C++. GLUT doesn't seem to like this,
> however. Any ideas on how to get around this problem -- or should I keep
> OpenGL events chained to C?

Since GLUT is implemented in C it prefers functions with C-style
linkage. As you've discovered, class member functions have a different
type signature (and different linkage), and therefore don't work
(remember, member functions have an implicit class pointer passed as the
first parameter).

What you want to use are class static methods:

class MEnvironment
{
public:
static void display();
};

Unfortunately, GLUT doesn't provide a way to pass user-defined data to
your callback functions. However, you can store a static pointer to a
global MEnvironment in your object, and retrieve it in your static
functions, then use that to call a class method if you prefer:

class MEnvironment
{
public:
MEnvironment()
{
sEnv = this;
}

static void sDisplay()
{
// static, so no non-static member variables,
// but call the member function through static pointer
sEnv->display();
}

void display()
{
// do drawing (and access member variables)
}

private:
static MEnvironment *sEnv;
};

glutDisplayFunc(MEnvironment::sDisplay);

I hope this helps!

--
Paul Miller - st...@fxtech.com

0 new messages