Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Registering a GLUT Callback in C++

Reply
Thread Tools

Registering a GLUT Callback in C++

 
 
Michael Kipper
Guest
Posts: n/a
 
      01-04-2006
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
 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-04-2006

Michael Kipper wrote:
> 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&l...2B&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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-04-2006
Michael Kipper wrote:
> 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
 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      01-04-2006
Michael Kipper wrote:
> 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
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      01-04-2006

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

--
Bob R
POVrookie


 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      01-05-2006
Michael Kipper wrote:

> 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;
}
----------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
embedding and extending python C API registering callback handlerobjects Tim Spens Python 1 06-27-2008 04:03 PM
Re: registering/re-registering ASP.NET 1.1.4322 with IIS Juan T. Llibre ASP .Net 2 12-16-2006 12:29 AM
GLUT & QT Together Adam Hartshorne C++ 7 04-24-2005 09:45 PM
How do I get GLUT to work with GCC for Win32? Bushido Hacks C++ 0 02-20-2005 04:45 AM
GLUT callback registration (not quite OT) Martin Magnusson C++ 1 09-05-2003 11:33 PM



Advertisments