473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Registering a GLUT Callback in C++

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
Jan 4 '06 #1
5 14279

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.

Jan 4 '06 #2
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
Jan 4 '06 #3
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
Jan 4 '06 #4

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

--
Bob R
POVrookie
Jan 4 '06 #5
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;
}
----------------------------------------------------------
Jan 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fulko van Westrenen | last post by:
Hello, I'm very new to Python and rather unexperienced in OpenGL, and now I try to learn both at the same time. I can find my way around in Perl with Perl/Tk. What I try to do is combining Tk...
5
by: Steven Gutstein | last post by:
I've just managed to get PyOpenGL installed on a Windows NT machine. I've tried testing it using one of the programs supplied with the PyOpenGL download - molehill.py. On my machine, it was placed...
1
by: Martin Magnusson | last post by:
Hi all. I'm having trouble registering a GLUT callback function. glutIdleFunc wants a void(*)() function as argument, and I believe that's what I've given it, but gcc complains with the following...
0
by: Bushido Hacks | last post by:
What's that? I've go GCC working for Windows? Well, sort of. But I'm not using a Unix Emulator. The trick is to install Dev-C++, but do not use the Dev-C++ IDE unless you feel like it. I...
1
by: Scott Chang | last post by:
Hi All I tried to use the attached OpenGL code to draw a Star-shaped figure on my Microsoft Visual C++ .NET 2002-Windows XP Pro PC. When I did 'Build' in Debug Mode, I got an error C2065:...
13
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
1
Airslash
by: Airslash | last post by:
Hello, I'm trying to make a simpel renderengine that allows me to draw objects and 3D models. To make it cross-platform i'm working with the glut library supplied by OpenGL. This library...
0
by: shortyzms | last post by:
I have been trying to write a small glut program that uses popup context menus on right clicks to draw objects. However, every time I do this, the area underneath the submenu item I clicked does not...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.