home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / oglgold.zip / SAMPLES / TK / STARGAME.C < prev    next >
C/C++ Source or Header  |  1997-09-30  |  14KB  |  565 lines

  1. #include "gl.h"
  2. #include "glu.h" 
  3. #include<math.h>
  4. #include<stdio.h>
  5. #include "tk.h"
  6.  
  7. #define PI        3.141592654
  8. #define PINFRAME    4
  9. #define PSPACE        60    
  10. #define SPEED        1.0    
  11. #define CRASHFRAMES    15
  12. #define FRACTIONINC    10.0
  13. #define abs(a)          ((a<0.0)? -(a) : (a))
  14.  
  15. unsigned char RTF[3] = { 0xff, 0x55, 0x55};  /* Orange */
  16. unsigned char RBF[3] = { 0x88, 0x11, 0x11};
  17.  
  18. unsigned char BTF[3] = { 0x55, 0x55, 0xff};  /* Blue */
  19. unsigned char BBF[3] = { 0x22, 0x22, 0x88};
  20.  
  21. unsigned char GTF[3] = { 0x77, 0xff, 0x77};  /* Green */
  22. unsigned char GBF[3] = { 0x00, 0x99, 0x00};
  23.  
  24. unsigned char PTF[3] = { 0xff, 0x55, 0xcc};  /* Pink */
  25. unsigned char PBF[3] = { 0x99, 0x05, 0x22};
  26.  
  27. unsigned char YTF[3] = { 0xff, 0xff, 0x33};  /* Yellow */
  28. unsigned char YBF[3] = { 0x88, 0x88, 0x11};
  29.  
  30. unsigned char OTF[3] = { 0xdd, 0x33, 0xff};  /* Purple */
  31. unsigned char OBF[3] = { 0x66, 0x11, 0x88};
  32.  
  33. #define BLUE    {{BTF,BTF,BTF,BTF},{BBF,BBF,BBF,BBF}}
  34. #define GREEN    {{GTF,GTF,GTF,GTF},{GBF,GBF,GBF,GBF}}
  35. #define RED    {{RTF,RTF,RTF,RTF},{RBF,RBF,RBF,RBF}}
  36. #define YELLOW  {{YTF,YTF,YTF,YTF},{YBF,YBF,YBF,YBF}}
  37. #define PURPLE    {{PTF,PTF,PTF,PTF},{PBF,PBF,PBF,PBF}}
  38. #define ORANGE    {{OTF,OTF,OTF,OTF},{OBF,OBF,OBF,OBF}}
  39.  
  40. int random[PINFRAME];
  41. float ztrans=0.0;
  42. float slowdown=0.0;
  43. int mask;
  44. int i,j,k;
  45. int ii;
  46. double out;
  47. int pleft, pright;
  48. int shift;
  49. int pastpannel=0, oldpastpannel=0;
  50. float xship, yship;
  51. int angle;
  52. int hit=0;
  53. int fbhit=0;
  54. int sign;
  55. int hitangle=0;
  56. float xlook=0.0, ylook=0.0;
  57. int cycle=0;
  58. float xfb1=0.0, yfb1=20.0;
  59. float xfb2=0.0, yfb2=20.0;
  60. float distin, fdistx, fdisty;
  61. float startpl[3], startpr[3], endpoint[3];
  62. char wallcount[30];
  63. int hitcount=0;
  64. int wallpast=0;
  65. int winWidth, winHeight, winX, winY;
  66. int screenHeight, screenWidth;
  67. int mousex, mousey;
  68. int type;
  69. int unit;
  70.  
  71. float identy[4][4] = { { 1.0, 0.0, 0.0, 0.0 },
  72.                { 0.0, 1.0, 0.0, 0.0 },
  73.                { 0.0, 0.0, 1.0, 0.0 },
  74.                { 0.0, 0.0, 0.0, 1.0 } };
  75.  
  76. int* pcolor[16][2][4] ={ BLUE,
  77.             GREEN,
  78.             RED,
  79.             YELLOW,
  80.             PURPLE,
  81.             ORANGE,
  82.             BLUE,
  83.             GREEN,
  84.             RED,
  85.             YELLOW,
  86.             PURPLE,
  87.             ORANGE,
  88.             BLUE,
  89.             PURPLE,
  90.             ORANGE,
  91.             BLUE};
  92.  
  93.             
  94. float starverts[25][3] = { { -1.0,  1.0,  1.0 },
  95.                { -1.0,  1.0,  0.0 }, 
  96.                { -1.0,  1.0, -1.0 }, 
  97.                {  0.0,  1.0, -1.0 }, 
  98.                {  1.0,  1.0, -1.0 }, 
  99.                {  1.0,  1.0,  0.0 }, 
  100.                {  1.0,  1.0,  1.0 }, 
  101.                {  0.0,  1.0,  1.0 }, 
  102.  
  103.                { -1.0,  0.0,  1.0 }, 
  104.                { -1.0,  0.0,  0.0 }, 
  105.                { -1.0,  0.0, -1.0 }, 
  106.                {  0.0,  0.0, -1.0 }, 
  107.                {  1.0,  0.0, -1.0 }, 
  108.                {  1.0,  0.0,  0.0 }, 
  109.                {  1.0,  0.0,  1.0 }, 
  110.                {  0.0,  0.0,  1.0 }, 
  111.  
  112.                { -1.0, -1.0,  1.0 }, 
  113.                { -1.0, -1.0,  0.0 }, 
  114.                { -1.0, -1.0, -1.0 }, 
  115.                {  0.0, -1.0, -1.0 }, 
  116.                {  1.0, -1.0, -1.0 }, 
  117.                {  1.0, -1.0,  0.0 }, 
  118.                {  1.0, -1.0,  1.0 },
  119.                {  0.0, -1.0,  1.0 },
  120.  
  121.                {  0.0,  0.0,  0.0 } };
  122.  
  123.  
  124.  
  125.  
  126. float verts[40][3] = { {  0.0, 40.0,  0.0},
  127.                        { 15.0, 40.0,  0.0},
  128.                        { 30.0, 40.0,  0.0},
  129.                        {  0.0, 30.0,  0.0},
  130.                        { 15.0, 30.0,  0.0},
  131.                        { 30.0, 30.0,  0.0},
  132.                        {  0.0, 20.0,  0.0},
  133.                        { 15.0, 20.0,  0.0},
  134.                        { 30.0, 20.0,  0.0},
  135.                        {  0.0, 10.0,  0.0},
  136.                        { 15.0, 10.0,  0.0},
  137.                        { 30.0, 10.0,  0.0},
  138.                        {  0.0,  0.0,  0.0},
  139.                        { 15.0,  0.0,  0.0},
  140.                        { 30.0,  0.0,  0.0},
  141.                        {  0.0, 35.0,  5.0},
  142.                        {  0.0, 35.0, -5.0},
  143.                        { 15.0, 35.0,  5.0},
  144.                        { 15.0, 35.0, -5.0},
  145.                        { 30.0, 35.0,  5.0},
  146.                        { 30.0, 35.0, -5.0},
  147.                        {  0.0, 25.0,  5.0},
  148.                        {  0.0, 25.0, -5.0},
  149.                        { 15.0, 25.0,  5.0},
  150.                        { 15.0, 25.0, -5.0},
  151.                        { 30.0, 25.0,  5.0},
  152.                        { 30.0, 25.0, -5.0},
  153.                        {  0.0, 15.0,  5.0},
  154.                        {  0.0, 15.0, -5.0},
  155.                        { 15.0, 15.0,  5.0},
  156.                        { 15.0, 15.0, -5.0},
  157.                        { 30.0, 15.0,  5.0},
  158.                        { 30.0, 15.0, -5.0},
  159.                        {  0.0,  5.0,  5.0},
  160.                        {  0.0,  5.0, -5.0},
  161.                        { 15.0,  5.0,  5.0},
  162.                        { 15.0,  5.0, -5.0},
  163.                        { 30.0,  5.0,  5.0},
  164.                        { 30.0,  5.0, -5.0} };
  165.  
  166. int panel[8][2][4] = { { {1, 16, 18, 2},
  167.                          {16, 4, 5, 18} },
  168.  
  169.                        { {2, 18, 20, 3},
  170.                          {18, 5, 6, 20} },
  171.  
  172.                        { {4, 22, 24, 5},
  173.                          {22, 7, 8, 24} },
  174.  
  175.                        { {5, 24, 26, 6},
  176.                          {24, 8, 9, 26} },
  177.  
  178.                        { {7, 28, 30, 8},
  179.                          {28, 10, 11, 30} },
  180.  
  181.                        { {8, 30, 32, 9},
  182.                          {30, 11, 12, 32} },
  183.  
  184.                        { {10, 34, 36, 11},
  185.                          {34, 13, 14, 36} },
  186.  
  187.                        { {11, 36, 38, 12},
  188.                          {36, 14, 15, 38} } };
  189.  
  190. int ends[4][4] =  { {2, 18, 5, 19},
  191.                     {5, 24, 8, 25},
  192.                     {8, 30, 11, 31},
  193.                     {11, 36, 14, 37} };
  194.  
  195.  
  196. int panconfig[16] = { 0x3C,
  197.               0xC3,
  198.              0x33,
  199.              0xCC,
  200.              0x55,
  201.              0xAA,
  202.              0xFC,
  203.              0x3F, 
  204.              0x7F,
  205.              0xBF,
  206.              0xDF,
  207.              0xEF,
  208.              0xF7,
  209.              0xFB,
  210.              0xFD,
  211.              0xFE };
  212.  
  213. float tverts[8][3] = { { -15.0,  0.0, 0.0 },
  214.                        {  15.0,  0.0, 0.0 },
  215.                        { -15.0, 40.0, 0.0 },
  216.                        {  15.0, 40.0, 0.0 },
  217.                        { -15.0,  0.0, -PINFRAME*PSPACE },
  218.                        {  15.0,  0.0, -PINFRAME*PSPACE },
  219.                        { -15.0, 40.0, -PINFRAME*PSPACE },
  220.                        {  15.0, 40.0, -PINFRAME*PSPACE } };
  221.  
  222. int             tunnel[4][2] = {{0, 4},
  223.         {1, 5},
  224.         {2, 6},
  225.         {3, 7}};
  226.  
  227. /* For solids 
  228. int tunnel[3][4] = { { 0, 2, 6, 4 },
  229.              { 0, 4, 5, 1 },
  230.              { 1, 5, 7, 3 } }; 
  231. */
  232.  
  233. void
  234. blookat(float vx, float vy, float vz, float px, float py, float pz, int twist)
  235. {
  236.   float           sino, hyp, hyp1, dx, dy, dz, ftwist;
  237.  
  238.   ftwist = -twist / 10.0;
  239.   sino = sin(ftwist * PI / 180.0);
  240.   glRotatef(ftwist, 0.0, 0.0, 1.0);
  241.  
  242.   dx = px - vx;
  243.   dy = py - vy;
  244.   dz = pz - vz;
  245.  
  246.   hyp = dx * dx + dz * dz;/* hyp squared  */
  247.   hyp1 = sqrt((double) (dy * dy + hyp));
  248.   hyp = sqrt((double) hyp);  /* the real hyp */
  249.  
  250.   if (hyp1 != 0.0) {  /* rotate X     */
  251.     sino = -dy / hyp1;
  252.   } else {
  253.     sino = 0.0;
  254.   }
  255.  
  256.   glRotatef(asin(sino) * 180 / PI, 1.0, 0.0, 0.0);
  257.  
  258.   if (hyp != 0.0) {  /* rotate Y     */
  259.     sino = dx / hyp;
  260.   } else {
  261.     sino = 0.0;
  262.   }
  263.  
  264.   glRotatef(asin(sino) * 180.0 / PI, 0.0, 1.0, 0.0);
  265.   glTranslatef(-vx, -vy, -vz);
  266. }
  267.  
  268.  
  269. void main(int argc, char **argv)
  270. {
  271.     GLenum type;
  272.  
  273.     screenHeight = 1024;
  274.     screenWidth  = 1280;
  275.     winWidth = 500;
  276.     winHeight = 400;
  277.     winX = 100;
  278.     winY = 100;
  279.     tkInitPosition(winX, winY, winWidth, winHeight);
  280.  
  281.     type = TK_DEPTH;
  282.     type |= TK_RGB;
  283.     type |= TK_DOUBLE;
  284.     type |= TK_DIRECT;
  285.     tkInitDisplayMode(type);
  286.  
  287.     if (tkInitWindow("Wave Demo") == GL_FALSE) {
  288.     tkQuit();
  289.     }
  290.  
  291.     Init();
  292.  
  293.     tkExposeFunc(DrawIt);
  294.     tkIdleFunc(DrawIt);
  295.     tkExec();
  296. }
  297.  
  298. Init()
  299. {
  300.   glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_FALSE);
  301.   glClearColor(0.0, 0.0, 0.0, 1.0);
  302.   glDisable(GL_DITHER);
  303.   glShadeModel(GL_FLAT);
  304.  
  305.   glMatrixMode(GL_PROJECTION);
  306.   glLoadIdentity();
  307.   gluPerspective(60.0, 1.25, 0.1, 1500.0);
  308.  
  309.   for(i=0;i<PINFRAME;++i)
  310.     random[i] = 16*rand()/((int) pow(2,15)-1);    
  311. }
  312.  
  313. /*
  314. while(!getbutton(MIDDLEMOUSE))
  315. */
  316. /* START of DRAW */
  317. DrawIt()
  318. {
  319.     glPushMatrix();
  320.     oldpastpannel = pastpannel;
  321.     pastpannel = -((int) ztrans)/PSPACE;
  322.     if(oldpastpannel != pastpannel)
  323.        {
  324.        hit = (panconfig[random[0]]&(1<<((3-((int) yship)/10)*2+(xship>0.0))))>0;
  325.        if(!hit)
  326.         {
  327.             for(i=0;i<PINFRAME-1;++i)
  328.            random[i] = random[i+1];
  329.                 random[PINFRAME-1] = 16*rand()/((int) pow(2,15)-1);    
  330.         ++wallpast;
  331.         }
  332.        else
  333.            hitcount += 2;
  334.        xfb2 = xfb1;
  335.        yfb2 = yfb1;
  336.        if((int) (xship/abs(xship))+1)
  337.            xfb1 = -15.0;
  338.        else
  339.            xfb1 =  15.0;
  340.        yfb1 = 20.0;
  341.        }
  342.     distin = -ztrans - ((float) pastpannel)*PSPACE;
  343.  
  344.     if((distin > PSPACE/2.0 - 1.0) && (distin < PSPACE/2.0 + 1.0))
  345.         {
  346.         fdistx = abs(xship - xfb2);
  347.         fdisty = abs(yship - yfb2);
  348.         if((fdistx < 2.0) && (fdisty < 2.0)) 
  349.             {
  350.             fbhit = 1;
  351.                  hitcount += 1;
  352.             }
  353.         }    
  354.  
  355.     if(hit || fbhit)
  356.        {
  357.            unit = winWidth/100;
  358. /*
  359.        glRecti(unit*10, unit, unit*10+hitcount*unit, unit*3);
  360.        if((unit*10+hitcount*unit) > winWidth-unit) 
  361. */
  362.            if (hitcount > CRASHFRAMES)
  363.         {
  364.         glClearColor(1.0, 0.0, 0.0, 1.0);
  365.         glClear(GL_COLOR_BUFFER_BIT);
  366.         tkSwapBuffers();
  367.         glClearColor(1.0, 1.0, 0.0, 1.0);
  368.         glClear(GL_COLOR_BUFFER_BIT);
  369.         for(i=0;i<50;++i) tkSwapBuffers();
  370.         printf("Total walls past = %d\n", wallpast);
  371.         exit(0);
  372.         }
  373.            glClearColor(0.0, 0.0, 0.0, 1.0);
  374.        glPopMatrix();
  375.        }
  376.     if(hit)
  377.        {
  378.        sign = ((int) (xship/abs(xship)));
  379.        hitangle = sign*random[(++cycle)%4]*150;        
  380.        xlook = (float) sign*random[(++cycle)%4]*7;
  381.        ylook = (float) sign*random[(++cycle)%4]*7;
  382.        ztrans += 0.75*PSPACE;
  383.        glClearColor(1.0, 0.0, 0.0, 1.0);
  384.            glClear(GL_COLOR_BUFFER_BIT);
  385.        tkSwapBuffers();
  386.            glClear(GL_COLOR_BUFFER_BIT);
  387.        tkSwapBuffers();
  388.        glClearColor(0.0, 0.0, 0.0, 1.0);
  389.        hit=0;
  390.        fbhit=0;
  391.        slowdown = (((float) pastpannel)/FRACTIONINC)/1.2;
  392.        }
  393.     else
  394.        {
  395.        if(fbhit)
  396.          {
  397.          sign = ((int) (xship/abs(xship)));
  398.          hitangle = sign*random[(++cycle)%4]*100;        
  399.          xlook = (float) sign*random[(++cycle)%4]*5;
  400.          ylook = (float) sign*random[(++cycle)%4]*5;
  401.          fbhit=0;
  402.          slowdown = (((float) pastpannel)/FRACTIONINC)/2.2;
  403.          ztrans -= (SPEED+(((float) pastpannel)/FRACTIONINC)-slowdown);
  404.          }
  405.        else
  406.          {
  407. /* MOUSE stuff here */
  408.             tkGetMouseLoc(&mousex, &mousey);
  409.             xship = (float)mousex/36.8-14.0;
  410.             yship = (float)mousey/20.25+1.0;
  411. /*
  412.          xship = ((float) getvaluator(MOUSEX))/46.0-14.0;
  413.          yship = ((float) getvaluator(MOUSEY))/27.0+1.0;
  414. */
  415.          angle = ((int) xship)*10;
  416.          slowdown /= 1.005;
  417.          ztrans -= (SPEED+(((float) pastpannel)/FRACTIONINC)-slowdown);
  418.          }
  419.        }
  420.  
  421.     hitangle*=0.95;
  422.     xlook*=0.95;
  423.     ylook*=0.95;
  424.  
  425.         glMatrixMode(GL_MODELVIEW);
  426.         glLoadIdentity();
  427.     blookat(xship,yship,ztrans,xlook,20.0+ylook,-PSPACE+ztrans,angle+hitangle);
  428.  
  429.         glPushMatrix();
  430.     glClearColor(0.0,0.0,0.0,1.0);
  431.     glClear(GL_COLOR_BUFFER_BIT);
  432.  
  433.     glTranslatef(0.0, 0.0, ztrans);
  434.     
  435.     /****** Render Tunnel ***********************************/
  436.  
  437. /*
  438.     glBegin(GL_QUADS);
  439.     for(i=0;i<3;++i)
  440.        { 
  441.        glColor3ub(i*80+50,i*80+50,i*80+50);
  442.         for(j=0;j<4;++j)
  443.            glVertex3fv(tverts[tunnel[i][j]]);
  444.            }
  445.     glEnd();
  446.     glPopMatrix();
  447. */
  448.         glColor3ub(255, 255, 255);
  449.         glBegin(GL_LINES);
  450.         for (i = 0; i < 4; ++i) {
  451.             for (j = 0; j < 2; ++j)
  452.                 glVertex3fv(tverts[tunnel[i][j]]);
  453.          }
  454.          glEnd();
  455.     glPopMatrix();
  456.  
  457.  
  458.     /****** Render Walls ***********************************/
  459.  
  460.     for(shift=PINFRAME-1; shift>=0; --shift)
  461.       {
  462.           glPushMatrix();
  463.       glTranslatef(-15.0, 0.0, -((float) shift)*PSPACE-((float)pastpannel)*PSPACE-PSPACE);
  464.  
  465.       /***** Render Ice Ball **********************************/
  466.  
  467.       if(shift==0)
  468.         {
  469.         glShadeModel(GL_SMOOTH);
  470.           glPushMatrix();
  471. /*
  472.             glLineWidth(5.0);
  473. */
  474.             glLineWidth(3.0);
  475.           xfb1 += (xship - xfb1)/8.0;
  476.         yfb1 += (yship - yfb1)/8.0;
  477.           glTranslatef(xfb1+15.0, yfb1, -PSPACE/2.0);
  478.                 glRotatef(ztrans * 2.0, 0.0, 1.0, 0.0);
  479.         glScalef(2.0, 2.0, 2.0);
  480.         glBegin(GL_LINES);
  481.         for(i=0;i<23;++i)
  482.             {
  483.             glColor3ub(0, 255, 255);
  484.             glVertex3fv(starverts[24]);        
  485.             glColor3ub(255, 255, 255);
  486.             glVertex3fv(starverts[i]);        
  487.             }
  488.         glEnd();
  489.         glLineWidth(1.0);
  490.         glPopMatrix();
  491.         glShadeModel(GL_FLAT);
  492.         }
  493.           /****** Render Ends ***********************************/
  494.  
  495.       mask = 1;
  496.       glBegin(GL_QUADS); 
  497.           for(i=0;i<4;++i)
  498.         {
  499.         pleft = mask & panconfig[random[shift]];
  500.         pright = (mask<<1) & panconfig[random[shift]];
  501.         if((pleft && !pright)||(!pleft && pright))
  502.             {
  503.             glColor3ubv(pcolor[random[shift]][1][1]);
  504.             for(k=0;k<4;++k)
  505.                   glVertex3fv(verts[ends[i][k]-1]);
  506.             }
  507.         mask = mask << 2;
  508.         }
  509.  
  510.           /****** Render Pannels ***********************************/
  511.  
  512.       mask = 1;
  513.           for(i=0;i<8;++i)
  514.         {
  515.         if(mask & panconfig[random[shift]])
  516.             {
  517.             for(j=0;j<2;++j)
  518.                 {
  519.                 for(k=0;k<4;++k)
  520.                   {
  521.                     glColor3ubv(pcolor[random[shift]][j][k]);
  522.                       glVertex3fv(verts[panel[i][j][k]-1]);
  523.                   }
  524.                 }
  525.             }
  526.         mask = mask << 1;
  527.         }
  528.           glEnd();
  529.  
  530.       /***** Render Fire Ball **********************************/
  531.  
  532.       if(shift==0)
  533.         {
  534.         glShadeModel(GL_SMOOTH);
  535.         glPushMatrix();
  536. /*
  537.         glLineWidth(15.0);
  538. */
  539.         glLineWidth(7.0);
  540.         xfb2 += (xship - xfb2)/7.0;
  541.         yfb2 += (yship - yfb2)/7.0;
  542.         glTranslatef(xfb2+15.0, yfb2, PSPACE/2.0);
  543.                 glRotatef(ztrans * 10.0, 0.0, 1.0, 0.0);
  544.                 glRotatef(ztrans * 10.0, 1.0, 0.0, 0.0);
  545.                 glRotatef(ztrans * 10.0, 0.0, 0.0, 1.0);
  546.                 glScalef(2.0, 2.0, 2.0);
  547.         glBegin(GL_LINES);
  548.         for(i=0;i<23;++i)
  549.           {
  550.           glColor3ub(255, 50, 10);
  551.           glVertex3fv(starverts[24]);        
  552.           glColor3ub(255, 255, 50);
  553.           glVertex3fv(starverts[i]);        
  554.           }
  555.         glEnd();
  556.         glLineWidth(1.0);
  557.         glPopMatrix();
  558.         glShadeModel(GL_FLAT);
  559.         }
  560.           glPopMatrix();
  561.           }
  562.     tkSwapBuffers();
  563.         glPopMatrix();
  564. }
  565.