/* * pong.c * This is a simple double buffered program. * Use the left and right arrow keys to move the paddle. * Use the home key to re-center the paddle and return the ball "home." * The beginnings of the classic video game. */ #include #include GLdouble paddleX = 0.0; GLdouble paddleDelta = 1.0; GLdouble ballX = 0.0; GLdouble ballY = 0.0; GLdouble ballXDelta = 0.5; GLdouble ballYDelta = 0.5; GLuint ball; void display(void) { glClear(GL_COLOR_BUFFER_BIT); // Render paddle. glColor3f(1.0, 1.0, 1.0); glBegin(GL_QUADS); glVertex2d(paddleX - 10.0, -50.0); glVertex2d(paddleX - 10.0, -45.0); glVertex2d(paddleX + 10.0, -45.0); glVertex2d(paddleX + 10.0, -50.0); glEnd(); // Render ball. glColor3f(1.0, 0.0, 0.0); glPushMatrix(); glTranslatef(ballX, ballY, 0.0); glCallList(ball); glPopMatrix(); glutSwapBuffers(); } void init(void) { GLUquadricObj *qobj; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); // Probably unnecessary. // Create the ball. ball = glGenLists(1); qobj = gluNewQuadric(); glNewList(ball, GL_COMPILE); gluDisk(qobj, 0.0, 5.0, 72, 1); glEndList(); } void reshape(int w, int h) { // Probably needs to be fixed. glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(int key, int x, int y) { // Limited here by keyboard repeat rate. switch (key) { case GLUT_KEY_LEFT: paddleX -= paddleDelta; break; case GLUT_KEY_RIGHT: paddleX += paddleDelta; break; case GLUT_KEY_HOME: paddleX = 0.0; ballX = ballY = 0.0; break; } glutPostRedisplay(); } void idle(void) { ballX += ballXDelta; ballY += ballYDelta; glutPostRedisplay(); } /* * Request double buffer display mode. * Register "special" input callback functions for arrow and home keys. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutSpecialFunc(keyboard); glutIdleFunc(idle); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }