Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top