Registering a GLUT Callback in C++

M

Michael Kipper

Hi,

I'm having a problem moving from C to C++.
In C, I have a function:

void display(void) {}

that I can register with OpenGL as a callback using:

GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )

called like so:

glutDisplayFunc( display );

My problem is when I move to C++ objects, I have a class with a member
function 'void display(void)', which I want to register like:

glutDisplayFunc( this->display );

But I get the error (in MSVC++):

error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
'void (void)' to 'void (__cdecl *)(void)'

What is the correct way to do this?

Thanks,
Michael
 
R

roberts.noah

Michael said:
Hi,

I'm having a problem moving from C to C++.
In C, I have a function:

void display(void) {}

that I can register with OpenGL as a callback using:

GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )

called like so:

glutDisplayFunc( display );

My problem is when I move to C++ objects, I have a class with a member
function 'void display(void)', which I want to register like:

glutDisplayFunc( this->display );

But I get the error (in MSVC++):

error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
'void (void)' to 'void (__cdecl *)(void)'

What is the correct way to do this?

"correct" is subjective.

http://www.google.com/search?hl=en&lr=&q=c+callbacks+in+c++&btnG=Search

You can't actually do what you are trying to do. You'll need to use
some hack. See above for some ideas.
 
V

Victor Bazarov

Michael said:
I'm having a problem moving from C to C++.
In C, I have a function:

void display(void) {}

that I can register with OpenGL as a callback using:

GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )

called like so:

glutDisplayFunc( display );

My problem is when I move to C++ objects, I have a class with a member
function 'void display(void)', which I want to register like:

glutDisplayFunc( this->display );

But I get the error (in MSVC++):

error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
'void (void)' to 'void (__cdecl *)(void)'

What is the correct way to do this?

There is none.

Read the FAQ, it explains why non-static members cannot be used as
callbacks.

V
 
J

James Juno

Michael said:
Hi,

I'm having a problem moving from C to C++.
In C, I have a function:

void display(void) {}

that I can register with OpenGL as a callback using:

GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )

called like so:

glutDisplayFunc( display );

My problem is when I move to C++ objects, I have a class with a member
function 'void display(void)', which I want to register like:

glutDisplayFunc( this->display );

But I get the error (in MSVC++):

error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
'void (void)' to 'void (__cdecl *)(void)'

What is the correct way to do this?

Thanks,
Michael

Maybe try something like GLUT for C++

http://www.cs.unc.edu/~rademach/glui/

-JJ
 
B

BobR

Michael Kipper wrote in message ...
Hi,
I'm having a problem moving from C to C++.
In C, I have a function:
void display(void) {}
that I can register with OpenGL as a callback using:
GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )
called like so:
glutDisplayFunc( display );
My problem is when I move to C++ objects, I have a class with a member
function 'void display(void)', which I want to register like:
glutDisplayFunc( this->display );
But I get the error (in MSVC++):
error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
'void (void)' to 'void (__cdecl *)(void)'
What is the correct way to do this?
Thanks,
Michael


You need to use 'static'. Much removed from the example below, but, should be
enough for you to get the idea.

// ---------------------------------------------------------
// GLutBase.cpp - Minimal GLut window
// made with (C) GLUTwriter by BobR <07dec01>
// ---------------------------------------------------------
#include <windows.h> // Standard Header For MSWindows Applications
#include "glut.h" // GL Utility Toolkit (GLUT 3.7) Header. LOCAL dir.

class GLRock{
public: //--------------------------------------- public
GLRock(){ } //GLRock() Ctor
~GLRock(){} //GLRock() Dtor
//--------------------------------------------------------------
bool InitGL(); // All setup for OpenGL goes here
// ======= InitCallBacks =======
static void InitCallBacks(); // Match functions to their counterparts
static void DrawGLScene(); // Drawing done here
// ---------------------------------------------
protected: //--------------------------------------- protected
private: //--------------------------------------- private
// ---------------------------------------------
}; //GLRock
// ---------------------------------------------

// ======= InitGL =======
bool GLRock::InitGL(){
// --- All setup for OpenGL goes here ---
return TRUE;
} //InitGL()
// ---------------------------------------------

// ======= DrawGLScene =======
void GLRock::DrawGLScene(){
// --- Drawing done here ---
} //DrawGLScene()
// ---------------------------------------------

// ======= InitCallBacks =======
void GLRock::InitCallBacks(){ // Match functions to their counterparts
glutDisplayFunc( DrawGLScene ); // Register Display Function
// --- etc. ---
} //InitCallBacks()
// ---------------------------------------------

// ======= MAIN =======
int main(int argc, char** argv) {
glutInit(&argc, argv); // GLUT Initializtion
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA );
if( g_gamemode ){
glutGameModeString("640x480:24"); // Select The 640x480 in 24bpp
Mode
if(glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
glutEnterGameMode(); // Enter Full Screen
else g_gamemode = false; // No Game Mode, switch to Windowed
}
if( not g_gamemode){
glutInitWindowSize(640, 480); // Window size if Windowed Mode
glutCreateWindow("GLut OpenGL Rock Demo"); // Window Title
}

GLRock GLR;
if( not GLR.InitGL() ){ // GL Initialization
/* "InitGL FAILED */
return EXIT_FAILURE;
}

GLR.InitCallBacks();

glutMainLoop(); // Go To GLUT Main Loop
// --- Never reaches this line ---
return 0;
} //main()
// =========================================
 
M

Manuel

Michael said:
What is the correct way to do this?

I've solved wrapping the callbacks.
Example (I use a reshape method from a custom obj called MainWindow):

----------------------------------------------------------
//Wrapping the glut reshape callback
void reshape(int w, int h){MainWindow.reshape (w,h);}

int main ( int argc, char** argv )
{

glutInit ( &argc, argv );
glutReshapeFunc ( reshape );
glutDisplayFunc ( display );
glutMotionFunc(motion);
glutMainLoop ( );
system("PAUSE");
return 0;
}
----------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top