home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / boom.c < prev    next >
C/C++ Source or Header  |  1999-09-27  |  5KB  |  248 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. InitBooms()
  25. /*
  26.  *  Initiliaze explosions
  27.  */
  28. {
  29.     int i;
  30.     double v[3];
  31.  
  32.     /* Mark all booms unused */
  33.     for (i=0; i<NBOOMS; i++)
  34.     {
  35.         boom[i].age = 0.0;
  36.         boom[i].angle = 0.0;
  37.     }
  38.  
  39.     /* Tweak explosion data */
  40.     for (i=0; i<12; i++)
  41.     {
  42.         /* Tweak explosion shape */
  43.         Vmul (v, icos_data[i], 0.9+rnd (0.2));
  44.         Vmul (v, v, 0.2);
  45.         Vset (boom_data[i], v);
  46.  
  47.         /* Tweak explosion color */
  48.         boom_color[i][0] = 0.9+rnd (0.1);
  49.         boom_color[i][1] = rnd(1.5);
  50.     }
  51. }
  52.  
  53. int FindBoom()
  54. /*
  55.  *  Find unused or oldest boom
  56.  */
  57. {
  58.     int i, oldest;
  59.     double maxage;
  60.  
  61.     for (i=0; i<NBOOMS; i++)
  62.     {
  63.         if (boom[i].age == 0.0) return (i);
  64.  
  65.         if (i == 0)
  66.         {
  67.             maxage = boom[i].age;
  68.             oldest = i;
  69.         }
  70.         else
  71.         {
  72.             if (boom[i].age > maxage)
  73.             {
  74.                 maxage = boom[i].age;
  75.                 oldest = i;
  76.             }
  77.         }
  78.     }
  79.  
  80.     /* No unused booms, return oldest */
  81.     return (oldest);
  82. }
  83.  
  84. DestroyBoom (b)
  85. int b;
  86. /*
  87.  *  Mark boom as unused
  88.  */
  89. {
  90.     boom[b].age = 0.0;
  91.  
  92.     /* Destroy corresponding light */
  93.     DestroyLight (boom[b].light);
  94. }
  95.  
  96. Boom (v, size)
  97. double v[3], size;
  98. /*
  99.  *  There was an explosion at coordinates v[]
  100.  */
  101. {
  102.     int b, l;
  103.  
  104.     /* Find an unused boom */
  105.     b = FindBoom();
  106.  
  107.     /* Set palette_flash, so next screen clear flashes the screen */
  108.     palette_flash = 1;
  109.  
  110.     /* Set it up */
  111.     boom[b].age = deltaT;
  112.     Vset (boom[b].pos, v);
  113.     boom[b].size = size;
  114.  
  115.     l = FindLight();
  116.     boom[b].light = l;
  117.     light[l].pos[0] = (float) boom[b].pos[0];
  118.     light[l].pos[1] = (float) boom[b].pos[1];
  119.     light[l].pos[2] = (float) boom[b].pos[2];
  120.     light[l].age = deltaT;
  121.     light[l].color[0] = light[l].color[1] = light[l].color[2] = 1.0;
  122.  
  123.     boom[b].angle = rnd (360.0);
  124.  
  125.     /* Play sound */
  126.     if (sound) PlayAudio (SOUND_BOOM);
  127. }
  128.  
  129. DrawBooms()
  130. /*
  131.  *  Draw the explosions
  132.  */
  133. {
  134.     int b;
  135.  
  136.     glPointSize (1.0);
  137.     for (b=0; b<NBOOMS; b++)
  138.     {
  139.         if (boom[b].age > 0.0) DrawBoom (b);
  140.     }
  141. }
  142.  
  143. DrawBoom (b)
  144. int b;
  145. /*
  146.  *  Draw this explosion
  147.  */
  148. {
  149.     int i;
  150.     double v1[3], v2[3], v3[3], v[3];
  151.     double t, u;
  152.  
  153.     /* Bump boom time */
  154.     boom[b].age += deltaT;
  155.  
  156.     /* See if boom has expired */
  157.     if (boom[b].age > BOOM_TIME)
  158.     {
  159.         DestroyBoom (b);
  160.         return;
  161.     }
  162.  
  163.     /* Convert age to [0,1] */
  164.     t = boom[b].age / BOOM_TIME;
  165.  
  166.     /* And on [1,0] */
  167.     u = 1.0 - t;
  168.  
  169.     /* Set the intensity of this boom's light source */
  170.     i = boom[b].light;
  171.     light[i].color[0] = light[i].color[1] = light[i].color[2] = u;
  172.  
  173.     /* Draw the boom */
  174.     glPushMatrix();
  175.     glDisable (GL_LIGHTING);
  176.     glDepthMask (GL_FALSE);
  177.  
  178.     /* Translate to where the boom is */
  179.     Vsub (v, boom[b].pos, player.pos);
  180.     glTranslated (v[0], v[1], v[2]);
  181.  
  182.     /* Apply rotation so they all don't look the same */
  183.     glRotated (boom[b].angle, 1.0, 0.0, 1.0);
  184.  
  185.     /* Scale according to size of boom */
  186.     glScaled (boom[b].size, boom[b].size, boom[b].size);
  187.  
  188.     /* Draw a smaller yellow boom first */
  189.     glColor3d (u, u, 0.0);
  190.     if (t < 0.5)
  191.         /* Growing sphere */
  192.         glutSolidSphere (t/8.0, 5, 5);
  193.     else
  194.         /* Shrinking sphere */
  195.         glutSolidSphere (u/8.0, 5, 5);
  196.  
  197.     /* Now draw the fancier, alpha-blended gas cloud */
  198.     glEnable (GL_BLEND);
  199.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  200.  
  201.     /* Make a bunch of pretty sparkles */
  202.     glBegin (GL_POINTS);
  203.     for (i=0; i<20; i++)
  204.     {
  205.         Vmul (v1, boom_data[i], t);
  206.         glColor4d (boom_color[i][0], boom_color[i][1], boom_color[i][2], u);
  207.         glVertex3d (v1[0], v1[1], v1[2]);
  208.     }
  209.     glEnd();
  210.  
  211.     for (i=19; i>=0; i--)
  212.     {
  213.         /* Adjust size according to explosion age 
  214.            (should do this with glScale)*/
  215.         Vmul (v1, boom_data[icos_index[i][2]], t/2.0);
  216.         Vmul (v2, boom_data[icos_index[i][1]], t/2.0);
  217.         Vmul (v3, boom_data[icos_index[i][0]], t/2.0);
  218.  
  219.         glBegin (GL_POLYGON);
  220.  
  221.         glColor4d (boom_color[icos_index[i][2]][0],
  222.                    boom_color[icos_index[i][2]][1],
  223.                    boom_color[icos_index[i][2]][2],
  224.                    u);
  225.         glVertex3d (v1[0], v1[1], v1[2]);
  226.  
  227.         glColor4d (boom_color[icos_index[i][1]][0],
  228.                    boom_color[icos_index[i][1]][1],
  229.                    boom_color[icos_index[i][1]][2],
  230.                    u);
  231.         glVertex3d (v2[0], v2[1], v2[2]);
  232.  
  233.         glColor4d (boom_color[icos_index[i][0]][0],
  234.                    boom_color[icos_index[i][0]][1],
  235.                    boom_color[icos_index[i][0]][2],
  236.                    u);
  237.         glVertex3d (v3[0], v3[1], v3[2]);
  238.  
  239.         glEnd();
  240.     }
  241.  
  242.     glDisable (GL_BLEND);
  243.     glDepthMask (GL_TRUE);
  244.     glEnable (GL_LIGHTING);
  245.     glPopMatrix();
  246. }
  247.  
  248.