/* * This is a GLUT (OpenGL Utility Toolkit) implementation of a Go interface, * and is used for all the Go examples. * * The purpose of this file is to hide all the GUI (Graphical User Interface) * details. * * The GLUT library is available on any system capable of running OpenGL. */ #include #include #include #include "Go.h" #include "GoGlutInterface.h" GlutContext GoGlutInterface::glutContext[MAX_GLUT_CONTEXTS]; int GoGlutInterface::contextCount = 0; char *GoGlutInterface::name = NULL; int GoGlutInterface::width = 300; // default window width int GoGlutInterface::height = 300; // default window width int GoGlutInterface::x = 10; // default window x position int GoGlutInterface::y = 10; // default window y position GoGlutInterface::GoGlutInterface(void) { // // Make sure MAX_GLUT_CONTEXT has not been exceeded. // if(contextCount == MAX_GLUT_CONTEXTS) { fprintf(stderr, "ERROR: MAX_GLUT_CONTEXT exceeded\n"); } // // Setup GLUT. // glutInitWindowSize(width, height); glutInitWindowPosition(x, y); glutContext[contextCount].winID = glutCreateWindow(name != NULL ? name : "Go Window"); glutDisplayFunc(GoGlutInterface::glutDisplayFunction); glutReshapeFunc(GoGlutInterface::glutReshapeFunction); glutKeyboardFunc(GoGlutInterface::glutKeyboardFunction); glutMouseFunc(GoGlutInterface::glutMouseFunction); glutMotionFunc(GoGlutInterface::glutMotionFunction); glutContext[contextCount].interface = this; // // Create the Go context. // go = new Go(width, height); contextCount++; } void GoGlutInterface::init(int *argc, char *argv[]) { glutInit(argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); } void GoGlutInterface::windowName(char *name) { GoGlutInterface::name = name; } void GoGlutInterface::windowSize(int width, int height) { GoGlutInterface::width = width; GoGlutInterface::height = height; } void GoGlutInterface::windowPosition(int x, int y) { GoGlutInterface::x = x; GoGlutInterface::y = y; } void GoGlutInterface::mainLoop(void) { glutMainLoop(); } void GoGlutInterface::swap(void) { glutSwapBuffers(); } void GoGlutInterface::rerender(void) { glutPostRedisplay(); } void GoGlutInterface::render(void) { // override } void GoGlutInterface::resized(int width, int height) { // override } void GoGlutInterface::keyTyped(char key) { // override } void GoGlutInterface::mousePressed(int x, int y) { // override } void GoGlutInterface::mouseReleased(int x, int y) { // override } void GoGlutInterface::mouseDragged(int x, int y) { // override } GoGlutInterface *GoGlutInterface::getInterface(void) { int winID = glutGetWindow(); for(int i = 0; i < MAX_GLUT_CONTEXTS; i++) { if(glutContext[i].winID == winID) { return glutContext[i].interface; } } return NULL; } void GoGlutInterface::glutDisplayFunction(void) { getInterface()->render(); } void GoGlutInterface::glutReshapeFunction(int width, int height) { GoGlutInterface *interface = getInterface(); interface->go->size(width, height); interface->resized(width, height); } void GoGlutInterface::glutKeyboardFunction(unsigned char key, int x, int y) { getInterface()->keyTyped(key); } void GoGlutInterface::glutMouseFunction(int button, int state, int x, int y) { switch(state) { case GLUT_DOWN: getInterface()->mousePressed(x, y); break; case GLUT_UP: getInterface()->mouseReleased(x, y); break; } } void GoGlutInterface::glutMotionFunction(int x, int y) { getInterface()->mouseDragged(x, y); }