views:

446

answers:

3

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 made a few simple games before and I usually break it down into an IO class to handle input and drawing to the screen, Game class for the main game loop/logic, and classes for whatever objects there are in the game.

Before I was using SDL, so my question is, is this the right way of going about this in OpenGL? I've already ran into some trouble. I want my IO class to handle initializing the window, drawing the scene, and mouse clicks. So the constructor looked like this:

IO::IO()
{
    currWindowSize[0] = DEF_WIDTH;
    currWindowSize[1] = DEF_HEIGHT;

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( INIT_WINDOW_POSITION[0], INIT_WINDOW_POSITION[1] );
    glutInitWindowSize( currWindowSize[0], currWindowSize[1] );
    glutCreateWindow( "TEST" );

    setUp();

    glutDisplayFunc(drawScene);
    glutMainLoop();
}

However, drawScene is a class method. Is there a way to pass a class method to glutDisplayFunc() without making it static?

+2  A: 

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.)

Cecil Has a Name
Yep ctor as coded is effectively a setup and main function combined. So I agree on the 'run' suggestion but I'd also put an explicit 'setup' ( and corresponding 'shutdown') function
zebrabox
Whoops, I didn't see that. Thanks for the suggestion.
jluebbert
A: 

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.

Stringer Bell
Thank you for the suggestion. This is for a class so I'm stuck using GLUT.
jluebbert
I doubt iPhone is the target anyway. I believe using an interface that is pretty much platform agnostic, like GLUT and GLFW, is better to use than any platform-specific FXs like the ones you mention (if I stand correct). Last autumns programming project recommended GLUT, however I chose GLFW and easily dodged some trouble that kept haunting everyone else using GLUT (erratic rendering behaviour).
Cecil Has a Name
+1  A: 

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.

Chris Becke