Skip to main content
gamedev.net
🔒 Locked

Object Oriented GLUT

Started by farofeiro Nov 6, 2008 at 2:49 PM 8 replies 3.6k views
Original Post
farofeiro
farofeiro
Hello. I'm trying to create a class GLclass to easily open a GLUT window and work with OpenGL. But I can't compile it anyway. G++ always gives me this error:
Quote:
main.cpp: In member function ‘void GLclass::InitializeGL(int, char**)’: main.cpp:20: erro: argument of type ‘void (GLclass::)()’ does not match ‘void (*)()’
Line 20 is the bold one.
Quote:
#include #include #include #include class GLclass { public: void InitializeGL(int argc, char *argv[]); void DrawScene(); }; void GLclass::InitializeGL(int argc, char *argv[]) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA ); glutInitWindowPosition (400,300); glutInitWindowSize(640,480); glutCreateWindow("Window"); glutDisplayFunc(DrawScene); glutMainLoop(); } void GLclass::DrawScene(void) { } int main(int argc, char *argv[]) { GLclass GLviewer; GLviewer.InitializeGL(argc,argv); }
Can someone see what I'm doing wrong?
CableGuy
CableGuy
As the error said glutDisplayFunc expects a void (*)() and you supplied void (GLclass::)() which is not the same. AFAIK you need to either put DrawScene in the global scope or make it static but I'm not sure, perhaps someone more knowledgeable in c++ can help...
HTH.

EDIT: Ok. After thinking about it I'm quite sure this is the problem. DrawScene is not marked as static and therefore can change local member of GLclass but calling glutDisplayFunc won't pass the "this" pointer to the DrawScene function. Hope this makes sense.
Servant of the Lord
Servant of the Lord
As CableGuy said, you can't pass a class member function as a parameter, unless it's static. Either make DrawScene a static function, or make it not belong to any class.
farofeiro
farofeiro
Ok, so how can I make DrawScene static?


I have tried making
static void DrawScene();
but it didn't work.

compiler give me this error:
Quote:
main.cpp:24: erro: cannot declare member function ‘static void GLclass::DrawScene()’ to have static linkage
areddevil
areddevil
I think you get the error because you are using a non-static type of parameter.

first declare the display function as static(which you have done)

static void DrawScene();


then call that static function by prefixing it with the class name
glutDisplayFunc(&GLclass::DrawScene);

rip-off
rip-off
I believe that the problem is unsolvable.

You cannot associate any state with the callbacks through GLUT, so you cannot make the interface you want. At best, you can have a global GLclass instance (or global pointer to an instance) and access it in the callbacks.

You could try write a set of wrapper functions that simplify the operations but they are already pretty simplified.

Alternatively, you could get yourself a better way to do non-GL related operations. For example, SDL is a very flexible library that you could potentially use for this.
dmatter
dmatter
Quote:
Original post by rip-off
I believe that the problem is unsolvable.

You cannot associate any state with the callbacks through GLUT, so you cannot make the interface you want.
QFE
Essentially GLUT is about as object oriented as it's going to get I'm afraid.

If you want to do anything more sophisticated then you'll need to use a more flexible library than GLUT.
ma_hty
ma_hty
Using OOP (or not using OOP) will only affect the management of source code if you are not using virtual member function or inheritance. In this sense, there is nothing you can implement with C but not C++. And, even if you are using virtual member function or inheritance, the performance of the program will still stay the same if you are doing things correctly.

And, glutDisplayFunc takes a function pointer. So, the best you can get to wrap a display function is a static member function. However, being a static function member doesn't mean it is impossible to access a specific class object. What you have to do, is to keep a list of class object pointers and to search for a specific class pointer with by other means. In case of glut window, that would be the glut window id.
daviddoria
daviddoria
I tried doing this for SO long and then I found SDL http://www.libsdl.org/ and it does things exactly how I want - ie. there is no "main loop" so you get to control everything however you want.

Dave

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.