I have an an OSX OpenGL app I\'m trying to modify. When I create the app a whole bunch of initialisation functions are called -- including methods where I can specify my own
I ended up using paercebal's answer above, along with his previous attempt at using a try/catch block around glutMainLoop() as well. Why? Because I wanted to cleanup properly no matter how it was shutdown. The global Context object will get destroyed properly if the app exits cleanly, which is what happens if you close out the app by closing the window (or quitting the app on OS X). But if you hit ctrl-C in the terminal where it was launched (or send it a SIGINT), the cleanup does not happen. To handle this, I added the following to my code:
static bool exitFlag = false;
static void sighandler(int sig) {
exitFlag = true;
}
static void idleFunc() {
if(exitFlag) {
throw NULL;
}
}
And then in main():
signal(SIGINT, sighandler);
glutIdleFunc(idleFunc);
try {
glutMainLoop();
} catch(...) {}
This isn't the prettiest bit of code, but it does handle both cases of exiting the program correctly.
One catch (no pun intended) -- any code you place after the catch() block will not be called if you close the window/quit the app normally. You have to place your cleanup code in the global Context object as shown in paercebal's answer. All this code is doing is allow the SIGINT signal to be used to get out of the glutMainLoop().
I think the real lesson here is that for anything really complex, GLUT is just not going to cut it.