c++ glut callbacks

Feb 13, 2010 at 6:37am
I'm working on a c++Shell, and I'm using OpenGL as the interface for creating the window and drawing text to screen...yes I know this is circuitous, but I'm forced to work with it as it's the only API I know. Now, my problem is in in the glutDisplayFunc()...This is a callback function, and I've got it encapsulated in a class. Now I know the trick to get it to work with a member function is using a static wrapper member function. Here's where the problem comes in. I've got another class for IO, which is being synced with the graphics class...The variables synced are needed for output...which is the whole point of the glutDisplayFunc()...So. I ask of you all, how do I pass these values to the new object created in the wrapper function?

Here's the code that's necessary for answering my questions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	class Graphics{
		private:
			GLFONT *glFont;
			char   *user;                                                                   //user out string
			char   *system;                                                                 //system out string
			char   *error;                                                                  //error string
	        char   *buffer;
			float   x;                                                                      //x Coordinate of text
			float   y;                                                                      //y coordinate of text
			int     runLevel;                                                               //0=NULL, 1=User, 2=System, 3=Error
			int     errLevel;                                                               //Levels are listed in '#ifdef ERROR' tree
			int     sysLevel;                                                               //Levels are listed in '#ifdef SYSTEM' tree
		public:
			int     glFontCreate(GLFONT *Font, char *FileName, int Tex);                    //Intial Creation of Font
			void    glFontDestroy (GLFONT *Font);                                           //Destruction of Font
			void    glFontBegin (GLFONT *Font);                                             //Initializer for Font output
			void    glFontEnd (void);                                                       //Destructor for Font Output
			void    glFontTextOut (char *String, float x, float y,float z);                 //Font output function
			void    initScreen(int argc, char **argv);                                      //Initialize gl screen for ooutput
		    void    updateScreen(void);                                                     //Updates screen with text typed by user, or output by console.
	static  void    wrapUpdate(void* ptr2Obj);
			void    passString(char *inBuffer);
        	void    setRunLevel(int level);                                                 //Sets runlevel to value indicated by level
			void    setErrLevel(int level);                                                 //Assigns Level to errLevel
			void    setSysLevel(int level);
					Graphics();
			       ~Graphics();
	};

1
2
3
4
	void Graphics::wrapUpdate(void* ptr2Obj){
	    Graphics *pGraph  = (Graphics*) ptr2Obj;
		pGraph->updateScreen();
	}

1
2
3
4
5
6
7
8
9
void Graphics::initScreen(int argc, char **argv){
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
		glutInitWindowPosition(100,100);
		glutInitWindowSize(500,250);
		glutCreateWindow("bi0Shell");
		glutDisplayFunc(wrapUpdate); //Gives me error converting void(*)(void*) to void(*)()
		glutMainLoop();
	}

Any help would be GREATLY appreciated.


Regards,
Alex E.
Feb 13, 2010 at 7:07am
This is one of the most common techniques used for callbacks:
1
2
3
4
5
6
7
8
9
10
11
struct callback_params{
    Graphics *ptr2Obj;
    //Anything else you need
};

//...

void Graphics::wrapUpdate(void *p){
    callback_params *params=(callback_params *)p;
    p->ptr2Obj->updateScreen();
}

One thing you need to be careful with: the parameter object has to exist whenever the callback may be called, so sometimes you'll have to create it dynamically.
Feb 13, 2010 at 7:20am
using this technique I've got a new error: 'error: `void*' is not a pointer-to-object type' as well as the old one. Thank you for your help though, I'm sure I'm doing something wrong...
Feb 13, 2010 at 7:55am
I meant to say params->ptr2obj. I shouldn't even need to say this.
Feb 13, 2010 at 8:01am
heh, I understood what you meant :P, and I fixed it immediately after posting. Sorry it's late and I'm a little tired, I'm sure you are too. Even so, I'm getting the same error. I know why I am, glutDisplayFunc() expects a pointer to a function with no arguments. and I have no way arround this, save for scrapping glut, and working with another toolkit :\ As I don't know any others...this may take some time. Care to reccomend one for me? It doesn't have to be OpenGL, as I'm learning something new anyways.

Nevermind. Going with X11, I always have liked it. Thank you again for your help Helios, as always, you've been great :P
Last edited on Feb 13, 2010 at 8:19am
Topic archived. No new replies allowed.