glutDisplayFunc() - Objective C

Started by
0 comments, last by DaedalusOwnsYou 14 years, 5 months ago
I'm trying to convert some Opengl code from C to Objective-c . So far syntax wise things appear to be OK but I'm running into an error I cant figure out. "invalid use of void expression"

//--------------------------
// @implementation
//--------------------------
@interface myOGL : NSObject  //Possible culprit? OGL Object?
-(void) DrawGLScene {
   glClear( GL_COLOR_BUFFER | GL_DEPTH_BUFFER_BIT);

   ..

   glFlush();
}

//---------------------
// Main Code
//---------------------
int main(int argc, char * argv[]) {
..

myOGL *OGL = [[myOGL alloc] init];

..
glutDisplayFunc([OGL DrawGLScene]); //Error here *Invalid use of void expression

..
return 0;
}


I'm sure it's my lack of knowledge in Obj C at this stage and not knowing how to do proper C Callbacks most likely...still learning. Regardless, I was hoping that some of the more savvy verterans my be able to help me out. FWIW, the implementation of the glutDisplayFunc is glutDisplayFunc(void (*func) (void)); With that in mind, I'm supecting that this allocation "myOGL *OGL = [[myOGL alloc] init];" maybe causing an issue since the OGL func is looking to receive a func pointer and/or that my Obj-C func (DrawGLScene) signature does not have (void) as a parameter of which I dont think is possible in Obj-C? Anyway, any help or suggestions are most welcome and for the sake of space I only listed the most relative parts of the code. If you need to see more I can post the whole thing but I figured initially it would be more noise than anything else. Thanks

----------

Advertisement
glutDisplayFunc expects it's argument to be a C-style function pointer. The code fragment
[OGL DrawGLScene]

isn't talking the pointer to your objective-c function, it's actually calling the function. The error message says that, because it complains that glutDisplayFunc needs an actual argument, not a "void expression". In fact, given the way objective-c handles method calling I'm not sure what you're doing is even directly possible. Consider how this looks internally, glutDisplayFunc wants to call a method that takes no arguments, and returns nothing (void), but you want to give it a function that takes 1 argument (the pointer to the myOGL object).

Figure out how you would pass data to the original c function that you are converting from. How would you pass application data to a GLUT display function? Do that, but pass a pointer to your myOGL object, and then have the c function just call [theObject DrawGLScene].

Edit: In an effort to be more helpful, and with fair warning that this hasn't been tested, compiled, or looked at for more than a few seconds, here's an example of how I would do what you're doing.

@interface CustomOpenGLDrawer: NSObject- (void)doTheDrawing;@end@implementation CustomOpenGLDrawer- (void)doTheDrawing {    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    //whole bunch of more garbage...    glFlush();}@endstatic CustomOpenGLDrawer* staticDrawer = NULL;void customOpenGLDrawerStaticCallback() {    [staticDrawer doTheDrawing];}int main(int argc,char** argv) {    //init garbage...        staticDrawer = [[CustomOpenGLDrawer alloc] init];    glutDisplayFunc(customOpenGLDrawerStaticCallback);        //main loop garbage...        [staticDrawer release];        //cleanup garbage...    return 0;}


[Edited by - DaedalusOwnsYou on November 8, 2009 1:58:37 PM]

This topic is closed to new replies.

Advertisement