home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry2.iso / Cube Drop 2001 1.0 / Main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  7.7 KB  |  364 lines

  1. //Software Engineer:  T. Lee Young
  2. //Class:              CS 4280
  3. //Assignemnt:         Final Project
  4.  
  5. #ifdef WIN32
  6. #include "windows.h"
  7. #endif
  8.  
  9. #include <GL/gl.h>
  10. #include <GL/glu.h>
  11. #include <GL/glut.h>
  12. #include <GL/glaux.h>
  13. #include <stdio.h>
  14. #include "canvas.h"
  15. #include "matrix.h"
  16. #include "cammera.h"
  17.  
  18. Canvas cvs(0,0,640,480,"Cube Space               Score: 0");
  19. Camera cam(PI/2.0, 12.0f, 80.0f);
  20. Matrix mat;
  21. bool gameOver = false;
  22. bool pause = false;
  23. bool start = false;
  24. bool autoRotate = false;
  25. bool intro = true;
  26. bool timersSet = false;
  27. GLint delayTime;
  28.  
  29.  
  30. void drawCubes()
  31. {
  32.     int color;
  33.  
  34.     //position the cube in the matrix position given by x, y, and z
  35.     GLfloat w, h, d;
  36.     w = h = d = 5.0;  //cube is 5 x 5 x 5
  37.     for(int i = 0; i<MAT_WIDTH; ++i)
  38.         for(int j = 0; j<MAT_HEIGHT; ++j)
  39.             for(int k = 0; k<MAT_DEPTH; ++k)
  40.                 if(color = mat.isSet(i,j,k))
  41.                 {
  42.                     switch(color)
  43.                     {
  44.                         case 1: cvs.setAmbientMaterialColor(1,0,0,1);
  45.                             break;
  46.                         case 2: cvs.setAmbientMaterialColor(0,1,0,1);
  47.                             break;
  48.                         case 3: cvs.setAmbientMaterialColor(0,0,1,1);
  49.                             break;
  50.                         case 4: cvs.setAmbientMaterialColor(1,1,0,1);
  51.                             break;
  52.                         case 5: cvs.setAmbientMaterialColor(0,1,1,1);
  53.                             break;
  54.                         case 6: cvs.setAmbientMaterialColor(1,0,1,1);
  55.                             break;
  56.                     }
  57.                     glPushMatrix();
  58.                         glTranslatef(2.5+i*w, 2.5+j*h, -2.5 - k*h);
  59.                         cvs.Box(w,h,d);
  60.                     glPopMatrix();
  61.                 }
  62. }
  63.  
  64.  
  65. void DisplayScore()
  66. {
  67.     
  68.  
  69. }
  70.  
  71. void DrawText(char *text, void *font, int x, int y, int z)
  72. {
  73.     int i = 0;
  74.  
  75.     glRasterPos3d(x, y, z);
  76.     
  77.     while(text[i] != '\0')
  78.     {
  79.         glutBitmapCharacter(font,text[i]);
  80.         ++i;
  81.     }
  82.  
  83. }
  84.  
  85. void IntroScreen()
  86. {
  87.     int x, y, z;
  88.  
  89.     x = -14; //column
  90.     y = 60;
  91.     z = -19; //row
  92.  
  93.     //display game instructions
  94.     cvs.setAmbientMaterialColor(1.0, 0.0, 0.0, 1.0);
  95.     DrawText("Cube Space", GLUT_BITMAP_9_BY_15, x, y, z);
  96.  
  97.     x = -19;
  98.     ++z;
  99.     cvs.setAmbientMaterialColor(0.0, 0.0, 1.0, 1.0);
  100.     DrawText("Game Controls", GLUT_BITMAP_8_BY_13, x, y, ++z );
  101.     x = -14;
  102.     cvs.setAmbientMaterialColor(1.0, 1.0, 1.0, 1.0);
  103.     DrawText("Start:  s", GLUT_BITMAP_8_BY_13, x, y, ++z);
  104.     DrawText("Pause:  p", GLUT_BITMAP_8_BY_13, x, y, ++z);
  105.  
  106.     ++z;
  107.     x = -24;
  108.     cvs.setAmbientMaterialColor(0.0, 0.0, 1.0, 1.0);
  109.     DrawText("Cube Movement Controls", GLUT_BITMAP_8_BY_13, x, y, ++z);
  110.     x = -14;
  111.     cvs.setAmbientMaterialColor(1.0, 1.0, 1.0, 1.0);
  112.     DrawText(" Left:  Left Arrow", GLUT_BITMAP_8_BY_13, x, y, ++z);
  113.     DrawText("Right:  Right Arrow", GLUT_BITMAP_8_BY_13, x, y, ++z);
  114.     DrawText("  Out:  Down Arrow", GLUT_BITMAP_8_BY_13, x, y, ++z);
  115.     DrawText("   In:  Up Arrow", GLUT_BITMAP_8_BY_13, x, y, ++z);
  116.  
  117.     ++z;
  118.     x = -24;
  119.     cvs.setAmbientMaterialColor(0.0, 0.0, 1.0, 1.0);
  120.     DrawText("Change View Controls", GLUT_BITMAP_8_BY_13, x, y, ++z);
  121.     x = -21;
  122.     cvs.setAmbientMaterialColor(1.0, 1.0, 1.0, 1.0);
  123.     DrawText("Rotate Counter Clockwise:  4", GLUT_BITMAP_8_BY_13, x, y, ++z);
  124.     DrawText("        Rotate Clockwise:  6", GLUT_BITMAP_8_BY_13, x, y, ++z);
  125.     DrawText("          Move In Closer:  8", GLUT_BITMAP_8_BY_13, x, y, ++z);
  126.     DrawText("               Move Back:  2", GLUT_BITMAP_8_BY_13, x, y, ++z);
  127.     DrawText("  View From Higher Angle:  +", GLUT_BITMAP_8_BY_13, x, y, ++z);
  128.     DrawText("   View From Lower Angle:  -", GLUT_BITMAP_8_BY_13, x, y, ++z);
  129.     DrawText("             Auto Change:  A", GLUT_BITMAP_8_BY_13, x, y, ++z);
  130.     DrawText("      Toggle This Screen:  I", GLUT_BITMAP_8_BY_13, x, y, ++z);
  131. }
  132.  
  133.  
  134. void GameOver()
  135. {
  136.     char scorePhrase[50];
  137.     int i = 0;
  138.  
  139.     cvs.setAmbientMaterialColor(1.0, 1.0, 1.0, 1.0);
  140.     DrawText("Game Over", GLUT_BITMAP_8_BY_13, -3, 55, -13);
  141.  
  142.     sprintf(scorePhrase, "Score: %i", mat.GetScore());
  143.     DrawText(scorePhrase, GLUT_BITMAP_8_BY_13, -3, 50, -8);
  144.     timersSet = false;
  145. }
  146.  
  147. void AutoRotateDisplay()
  148. {
  149.     static int movement = 0;
  150.     static int count = 0;
  151.  
  152. //    srand( (unsigned)time(NULL) );//seed the rand num generator
  153.  
  154.     //only change the direction of change
  155.     //on even multiples of 125
  156.     if(count%125 == 0)
  157.     {
  158.         movement = rand()%6+1;
  159.     }
  160.  
  161.     //only change the view when count is an even multiple of 2
  162.     //if the graphics card is very slow, decrease this
  163.     if(count%1 == 0)
  164.     {
  165.         switch(movement)
  166.         {
  167.             case 1: cam.Up(); break;
  168.             case 2: cam.Down(); break;
  169.             case 3: cam.RotateCC(); break;
  170.             case 4: cam.RotateC(); break;
  171.             case 5: cam.In(); break;
  172.             case 6: cam.Out(); break;
  173.         }
  174.     }
  175.  
  176.     ++count;
  177.  
  178. //    printf("Movement: %i\n", movement);
  179. }
  180.  
  181.  
  182. void ProcessGeometry()
  183. {
  184.     glLoadIdentity();    
  185.  
  186.     if(start && !intro)
  187.     {
  188.         gluLookAt(cam.getX(), cam.getY(), cam.getZ(),
  189.               12.0f, 0.0f, -12.0f,
  190.               0.0f, 1.0f, 0.0f);
  191.  
  192.         drawCubes();
  193.         DisplayScore();
  194.  
  195.         if(gameOver)
  196.             GameOver();            
  197.  
  198.         if(autoRotate)
  199.             AutoRotateDisplay();
  200.     }
  201.     else
  202.     {
  203.         gluLookAt(0.0, 100.0, 0.1,
  204.                   0.0, 0.0, -15.0,
  205. //                   0.0, 0.0, 0.0,
  206.                   0.0, 1.0, 0.0);
  207.         IntroScreen();
  208.  
  209.         drawCubes();
  210.         DisplayScore();
  211.  
  212.         if(gameOver)
  213.         {
  214.             GameOver();
  215.             autoRotate = true;
  216.         }
  217.  
  218.         if(autoRotate)
  219.             AutoRotateDisplay();
  220.     }    
  221.  
  222.     //draw the game grid
  223.     cvs.setAmbientMaterialColor(0,1,0,1);
  224.     glCallList(cvs.FindDisplayListId("Grid"));
  225.  
  226.     glutPostRedisplay();
  227.     glutSwapBuffers();
  228. }
  229.  
  230. void init()
  231. {
  232.     cvs.setPerspectiveWindow(60,cvs.miWidth/cvs.miHeight,5,2000);
  233.     cvs.setViewport(0,0,cvs.miWidth,cvs.miHeight);
  234.     cvs.setBackgroundColor(0,0,0);
  235.     cvs.setLighting();
  236.     //cvs.setBlending();
  237.     cvs.setDepthBuffer();
  238.     glMatrixMode(GL_MODELVIEW);
  239.     glLoadIdentity();
  240.  
  241.     cvs.buildGrid("Grid");
  242.  
  243.     //seed the rand number generator
  244.     srand( (unsigned)time(NULL) );
  245. }
  246.  
  247.  
  248. void TimerFunction(int i)
  249. {
  250.     bool successful;
  251.  
  252.     if(pause)
  253.     {
  254.         glutTimerFunc(delayTime,TimerFunction, delayTime);
  255.     }
  256.     else
  257.     {
  258.         successful = mat.moveDown();
  259.         if(!successful)
  260.         {
  261.             gameOver = true;
  262.             autoRotate = true;
  263.         }
  264.         else
  265.             glutTimerFunc(delayTime, TimerFunction, delayTime);
  266.  
  267.         printf("Delay Time: %i\n", delayTime);
  268.     }
  269.  
  270.     glutPostRedisplay();
  271. }
  272.  
  273.  
  274. void IncreaseSpeed(int _time)
  275. {
  276.     if(delayTime >= 25)
  277.         delayTime -= 25;
  278.  
  279.     glutTimerFunc(_time, IncreaseSpeed, _time);
  280.  
  281. //    printf("2nd Timer's time has expired");
  282. }
  283.  
  284.  
  285. void StartGame()
  286. {
  287.     gameOver = false;
  288.     autoRotate = false;
  289.     intro = false;
  290.     cam.SetCamera(PI/2.0, 12.0f, 80.0f);
  291.     delayTime = 500;
  292.     mat.SetScore(0);
  293.     mat.SetTitleBar();
  294.     mat.ZeroMatrix();        
  295.     mat.set(3,10,3);
  296.     start = true;
  297.     
  298.     //prevent two sets of timers from running if the game is reset
  299.     if(!timersSet)
  300.     {
  301.         glutTimerFunc(delayTime,TimerFunction, delayTime);
  302.         glutTimerFunc(10000, IncreaseSpeed, 10000);
  303.         timersSet = true;
  304.     }    
  305. }
  306.  
  307.  
  308. static void key (unsigned char k, int x, int y)
  309. {
  310.     k = toupper(k);
  311.  
  312.     switch(k) {
  313.     case '8': cam.In();  break;
  314.     case '2': cam.Out(); break;
  315.     case '4': cam.RotateC();break;
  316.     case '6': cam.RotateCC(); break;
  317.     case '+': cam.Up(); break;
  318.     case '-': cam.Down(); break;
  319.     case 27:  exit(0); break;
  320.     case ' ': mat.drop(); break;
  321.     case 'S': StartGame(); break;
  322.     case 'P': if(start) pause = !pause;    break;
  323.     case 'A': autoRotate = !autoRotate; break;
  324.     case 'I': intro = !intro; break;
  325.     }
  326. }
  327.  
  328. static void special (int k, int x, int y)
  329. {
  330.   switch (k)
  331.   {
  332.       case GLUT_KEY_UP: if(!pause) mat.moveIn();  break;
  333.       case GLUT_KEY_DOWN: if(!pause) mat.moveOut();    break;
  334.       case GLUT_KEY_LEFT: if(!pause) mat.moveLeft(); break;
  335.       case GLUT_KEY_RIGHT: if(!pause) mat.moveRight(); break;
  336.   }
  337. }
  338.  
  339. void myReshape(GLsizei W, GLsizei H)
  340. {
  341.     cvs.setViewport(0,0,W,H);
  342. }
  343.  
  344. static void draw(GLvoid)
  345. {
  346.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  347.     ProcessGeometry();
  348.     glutSwapBuffers();
  349. }
  350.  
  351.  
  352. int main(int argc, char **argv) 
  353. {
  354.     init();
  355.     glutKeyboardFunc(key);
  356.     glutSpecialFunc(special);
  357.     glutDisplayFunc(draw);
  358.     glutReshapeFunc(myReshape);
  359.     //glutIdleFunc(draw);
  360.     glutMainLoop();
  361.  
  362.     return(0);
  363. }
  364.