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:
rawGLScene(){
// --- 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()
// =========================================