#include #include #include #include #include void *font = GLUT_BITMAP_TIMES_ROMAN_24; void *fonts[] = { GLUT_BITMAP_9_BY_15, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMAN_24 }; char defaultMessage[] = "LABYRINTH!!!"; char *message = defaultMessage; GLuint texture; float xpos = 0; float ypos = 0; float zpos = 10; float xpos2 = 50; float ypos2 = 50; float zpos2 = 10; float xpos3 = -50; float ypos3 = -50; float zpos3 = 10; float holex[30]; float holey[30]; float g = 9.8; float zlookat = 1; int NUMHOLES = 30; float radius = 5; int randomizeholes = 1; int leftcounter = 0; int rightcounter = 0; int deathhole = 0; int upcounter = 0; float thetax = 0; float thetay = 0; int downcounter = 0; int jump; int bounceball1= 0; int bounceball2= 0; int bounceball3= 0; int countert = 0; int KeyDown[256]; void FreeTexture( GLuint texture ) { glDeleteTextures( 1, &texture ); } void selectFont(int newfont) { font = fonts[newfont]; glutPostRedisplay(); } GLuint LoadTextureRAW( const char * filename, int wrap ) { GLuint texture; int width, height; void * data; FILE * file; // open texture data file = fopen( filename, "rb" ); if ( file == NULL ) return 0; // allocate buffer width = 256; height = 256; data = malloc( width * height * 3 ); // read texture data fread( data, width * height * 3, 1, file ); fclose( file ); // allocate a texture name glGenTextures( 1, &texture ); // select our current texture glBindTexture( GL_TEXTURE_2D, texture ); // select modulate to mix texture with color for shading glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // when texture area is small, bilinear filter the closest MIP map glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST ); // when texture area is large, bilinear filter the first MIP map glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // if wrap is true, the texture wraps over at the edges (repeat) // ... false, the texture ends at the edges (clamp) glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP ); // build our texture MIP maps gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data ); // free buffer free( data ); return texture; } void output(int x, int y, char *string) { int len, i; glRasterPos2f(x, y); len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); } } void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glEnable(GL_DEPTH_TEST); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf(GL_FRONT, GL_SHININESS, 25.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); texture = LoadTextureRAW( "wood.bmp", 1 ); } void display(void) { int i; float j; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,1,1); output(-150, 200, "LABYRINTH GAME!"); glPushMatrix(); glLoadIdentity(); //glEnable(GL_LIGHTING); //glEnable(GL_LIGHT0); glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, texture ); gluLookAt((leftcounter-rightcounter)/10.+0.2,(downcounter-upcounter)/10.-0.2,zlookat,0,0,0,0,1,0); glBegin(GL_QUADS); glColor3f(1,1,1); glNormal3f(0,0,1); glTexCoord2d(0.0,0.0); glVertex3f(-200,-150,0); glTexCoord2d(1.0,0.0); glVertex3f(-200, 150,0); glTexCoord2d(1.0,1.0); glVertex3f( 200, 150,0); glTexCoord2d(0.0,1.0); glVertex3f( 200,-150,0); glEnd(); glColor3f(1.0,0.0,0.0); glBegin(GL_QUADS); glNormal3f(0,-1,0); glTexCoord2d(1.0,0.0); glVertex3f(-200,-150,25); glTexCoord2d(1.0,1.0); glVertex3f(200,-150,25); glTexCoord2d(0.0,1.0); glVertex3f(200,-150,-25); glTexCoord2d(0.0,0.0); glVertex3f(-200,-150,-25); glEnd(); glBegin(GL_QUADS); glNormal3f(0,1,0); glTexCoord2d(1.0,0.0); glVertex3f(-200,150,25); glTexCoord2d(1.0,1.0); glVertex3f(200,150,25); glTexCoord2d(0.0,1.0); glVertex3f(200,150,-25); glTexCoord2d(0.0,0.0); glVertex3f(-200,150,-25); glEnd(); glBegin(GL_QUADS); glNormal3f(-1,0,0); glTexCoord2d(0.0,0.0); glVertex3f(-200,-150,25); glTexCoord2d(1.0,0.0); glVertex3f(-200,-150,-25); glTexCoord2d(1.0,1.0); glVertex3f(-200,150,-25); glTexCoord2d(0.0,1.0); glVertex3f(-200,150,25); glEnd(); glBegin(GL_QUADS); glNormal3f(1,0,0); glTexCoord2d(0.0,0.0); glVertex3f(200,-150,25); glTexCoord2d(1.0,0.0); glVertex3f(200,-150,-25); glTexCoord2d(1.0,1.0); glVertex3f(200,150,-25); glTexCoord2d(0.0,1.0); glVertex3f(200,150,25); glEnd(); glPopMatrix(); glColor3f(0,0,0); if (randomizeholes) { for (i = 0; i holex[i]-radius && ypos < holey[i]+radius && ypos > holey[i]-radius && zpos <=10) { deathhole = i; return 1; } } return 0; } int checkBallCollision(float xpos, float ypos, float zpos, float xpos2, float ypos2, int t) { if (xpos < xpos2+radius*4 && xpos > xpos2-radius*4 && ypos < ypos2+radius*4 && ypos > ypos2-radius*4 && zpos >=10) { bounceball1 = 1; countert = t; return 1; } return 0; } void timerFunc( int t) { int i, j, k; j = checkBallCollision(xpos,ypos,zpos2,xpos2,ypos2,t); k = checkBallCollision(xpos,ypos,zpos3,xpos3,ypos3,t); //printf("%f %f \n",thetax,thetay); i = checkBallInHole(xpos,ypos,zpos); if (i==1) { xpos = holex[deathhole]; ypos = holey[deathhole]; zpos-= t*9.8*.001; } else { if (j==1 || bounceball1 || k == 1) { if (xpos <200-radius && rightcounter > leftcounter) xpos-=(rightcounter-leftcounter)*.5; if (xpos >-200+radius && rightcounter < leftcounter) xpos+=(leftcounter-rightcounter)*.5; if (ypos <150-radius && downcounter < upcounter) ypos-=(upcounter-downcounter)*.5; if (ypos >-150+radius && downcounter > upcounter) ypos+=(downcounter-upcounter)*.5; } else { if (xpos <200-radius && rightcounter > leftcounter) xpos+= (rightcounter-leftcounter); if (xpos >-200+radius && rightcounter < leftcounter) xpos-=(leftcounter-rightcounter); if (ypos <150-radius && downcounter < upcounter) ypos+=(upcounter-downcounter); if (ypos >-150+radius && downcounter > upcounter) ypos-=(downcounter-upcounter); } if (t-countert==10) { countert = 0; bounceball1 = 0; } } i = checkBallInHole(xpos2,ypos2,zpos2); j = checkBallCollision(xpos2,ypos2,zpos,xpos,ypos,t); k = checkBallCollision(xpos2,ypos2,zpos3,xpos3,ypos3,t); if (j==1 || k==1) bounceball2 == 1; if (i==1) { xpos2 = holex[deathhole]; ypos2 = holey[deathhole]; zpos2-= t*9.8*.001; } else { if (j==1 || k==1 || bounceball2 == 1) { if (xpos2 <200-radius && rightcounter > leftcounter) xpos2-=(rightcounter-leftcounter); if (xpos2 >-200+radius && rightcounter < leftcounter) xpos2+=(leftcounter-rightcounter); if (ypos2 <150-radius && downcounter < upcounter) ypos2-=(upcounter-downcounter); if (ypos2 >-150+radius && downcounter > upcounter) ypos2+=(downcounter-upcounter); } else { if (xpos2 <200-radius && rightcounter > leftcounter) xpos2+=(rightcounter-leftcounter); if (xpos2 >-200+radius && rightcounter < leftcounter) xpos2-=(leftcounter-rightcounter); if (ypos2 <150-radius && downcounter < upcounter) ypos2+=(upcounter-downcounter); if (ypos2 >-150+radius && downcounter > upcounter) ypos2-=(downcounter-upcounter); } } i = checkBallInHole(xpos3,ypos3,zpos2); j = checkBallCollision(xpos3,ypos3,zpos,xpos,ypos,t); k = checkBallCollision(xpos3,ypos3,zpos2,xpos2,ypos2,t); if (j==1 || k==1) bounceball3 == 1; if (i==1) { xpos3 = holex[deathhole]; ypos3 = holey[deathhole]; zpos3-= t*9.8*.001; } else { if (j==1 || k==1 || bounceball3 == 1) { if (xpos3 <200-radius && rightcounter > leftcounter) xpos3-=(rightcounter-leftcounter); if (xpos3 >-200+radius && rightcounter < leftcounter) xpos3+=(leftcounter-rightcounter); if (ypos3 <150-radius && downcounter < upcounter) ypos3-=(upcounter-downcounter); if (ypos3 >-150+radius && downcounter > upcounter) ypos3+=(downcounter-upcounter); } else { if (xpos3 <200-radius && rightcounter > leftcounter) xpos3+=(rightcounter-leftcounter); if (xpos3 >-200+radius && rightcounter < leftcounter) xpos3-=(leftcounter-rightcounter); if (ypos3 <150-radius && downcounter < upcounter) ypos3+=(upcounter-downcounter); if (ypos3 >-150+radius && downcounter > upcounter) ypos3-=(downcounter-upcounter); } } if (jump == 1) { if (zpos > 30 || zpos2 > 30 || zpos3 > 30) jump = 0; else { zpos++; zpos2++; zpos3++; } } else { if (zpos > 10) { zpos--; } if (zpos2 > 10) { zpos2--; } if (zpos3 > 10) { zpos3--; } } //printf("%d",i); t++; glutPostRedisplay(); glutTimerFunc(20,timerFunc,t); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); //glFrustum(-400,400,-300,300,1,200); glOrtho(-400,400,-300,300,-300,300); /* if (w <= h) glOrtho (-400, 400, -300*(GLfloat)h/(GLfloat)w, 300*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-400*(GLfloat)w/(GLfloat)h, 400*(GLfloat)w/(GLfloat)h, -300, 300, -10.0, 10.0);*/ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void mouse(int button, int state, int x, int y) { } void keyboard(unsigned char key, int x, int y) { KeyDown[key]= 1; switch (key) { case 27: exit(0); break; //case 'a': leftcounter++; thetax--;break; //case 'd': rightcounter++; thetax++;break; //case 'w': upcounter++; thetay++;break; //case 's': downcounter++; thetay--;break; //case ' ': jump = 1; break; case 'r': leftcounter = rightcounter = upcounter = downcounter = 0; randomizeholes = 1; xpos = ypos = zpos = 0; xpos2 = 50; ypos2 = 50; zpos2 = 10; xpos3 = -50; ypos3 = -50; zpos3 = 10; countert = 0; bounceball1 = 0; break; } if (KeyDown['a']==1) { leftcounter++; } if (KeyDown['d']==1) { rightcounter++; } if (KeyDown['w']==1) { upcounter++; } if (KeyDown['s']==1) { downcounter++; } if (KeyDown[' ']==1) { jump = 1; } glutPostRedisplay(); } void keyboardUp(unsigned char key, int x, int y) { KeyDown[key] = 0; } int main(int argc, char** argv) { int i; glutInit(&argc, argv); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-mono")) { font = GLUT_BITMAP_9_BY_15; } } glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (800, 600); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutKeyboardUpFunc(keyboardUp); glutTimerFunc(20,timerFunc,0); glutMainLoop(); return 0; }