I\'m writing a game for school in OpenGL. Since there will be several more similar assignments I want to make a small framework for doing common things in OpenGL. I have mad
Before I was using SDL, so my question is, is this the right way of going about this in OpenGL?
I'd rather bypass GLUT completely (if you want to learn the inner guts of OpenGL and your OS windowing system interface). Plus GLUT is not supported on the iPhone (if you want to target this platform in the future).
So the idea would be to use WGL
on Windows, GLX
on Linux and CGL
on OS X.
Two points,
First, glutMainLoop never returns, and starts dispatching messages. Because you called it from your constructor your class is potentially not properly constructed yet. Additionally glut does support more than one window, and glutModalLoop only needs to be called once.
Second, as you have noted, the method passed to glutDisplayFunc must be static. You will need a static array associating glut window id's with IO pointers. When glutCreateWindow is called by an IO instance, store the returned id and IO instance in the array. Whenever the static drawScene is called you would call glutGetWindow to get the id of the current window, and look up the associated IO pointer and call the non static drawScene implementation.
Unfortunately the glutDisplayFunc()
doesn't take a void*
pointer so you could've fake an object context. You will have to make a static function that can call into the correct IO instance using a static variable.
I see some slight trouble with your pattern also, though. As far as I know, glutMainLoop()
never returns until you terminate the GLUT context, therefore you have a constructor that practically never returns, so your program flow is unreasonable. You should move that call into a separate run()
method in your class.
(Personally I would use GLFW, which avoids the entire callback mess with GLUT, although you have to write your mainloop.)