Jump to content
  • Advertisement
Sign in to follow this  
discostu

OpenGL GLUT callbacks as Class member functions

This topic is 4974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have a class I'm creating called OpenGLApp. In it there are member functions like OpenGLApp::display(), OpenGLApp::keyb(...), OpenGLApp::reshape(...), and so on. I have another function that runs glutDisplayFunc( display ), and so on. But it has a problem that it can't convert void (OpenGLApp::*display) () to void(*)(). I don't want to make these static, cause then I have to make everything i use in them static. OpenGLApp is a singleton by the way. Here is the header file: #ifndef _OPENGLAPP_H #define _OPENGLAPP_H #include "Ball.hh" #include "Brick.hh" #include "Paddle.hh" #include <vector> class OpenGLApp { public : static OpenGLApp& instance( ); void setWindow( int width = 640, int height = 480, int left = 100, int top = 100, std::string winName = "OpenGL Application" ); void start( int argc, char** argv ); void stop( ); void createBall( int x, int y ); void createPaddle( int x, int y ); private : static OpenGLApp* mInstance; protected : OpenGLApp( ) { mWinSet = mMainLoop = false; mPtrDraw = &OpenGLApp::draw; mPtrReshape = &OpenGLApp::reshape; mPtrKeyb = &OpenGLApp::keyb; }; int mHeight, mWidth; int mLeft, mTop; std::string mWinName; bool mWinSet; bool mMainLoop; void mainLoop( ); Ball mBall; Paddle mPaddle; std::vector <Brick> mBricks; void ( OpenGLApp::*mPtrDraw )( void ); void draw( ); void ( OpenGLApp::*mPtrReshape )( int, int ); void reshape( int width, int height ); void ( OpenGLApp::*mPtrKeyb )( unsigned char, int, int ); void keyb( unsigned char key, int x, int y ); void drawBall( Ball b ); void drawPaddle( Paddle p ); void drawBrick( Brick b ); }; #endif // _OPENGLAPP_H

Share this post


Link to post
Share on other sites
Advertisement
You'll have to write static wrapper functions that calls the appropriate singleton methods.
It's impossible (well, unsafe) to cast a member function pointer into a regular one since they use different calling conventions, and pass the object pointer as an additional hidden argument.

Share this post


Link to post
Share on other sites
Thanks, that worked great. I just made static functions which are of the form:

static void sDisplay( ) { instance().display(); }

They compile and run!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!