src/tron/gSparks.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "gSparks.h"
00029 #include "eTimer.h"
00030 #include "rRender.h"
00031 #include "tRandom.h"
00032 
00033 #ifdef USEPARTICLES
00034 #include "papi.h"
00035 #endif
00036 
00037 bool white_sparks=false;
00038 
00039 gSpark::gSpark(eGrid *grid, const eCoord &pos,const eCoord &dir,REAL time,REAL ocolor_r,REAL ocolor_g,REAL ocolor_b,REAL ecolor_r,REAL ecolor_g,REAL ecolor_b)
00040         :eReferencableGameObject(grid, pos, dir , NULL, true),
00041         //   sound(scrap),
00042 createTime(time){
00043     lastTime=createTime;
00044 
00045 #ifndef USEPARTICLES
00046     sparkowncolor_r=ocolor_r;
00047     sparkowncolor_g=ocolor_g;
00048     sparkowncolor_b=ocolor_b;
00049 
00050     sparkenemycolor_r=ecolor_r;
00051     sparkenemycolor_g=ecolor_g;
00052     sparkenemycolor_b=ecolor_b;
00053 
00054     for (int i=SPARKS-1;i>=0;i--){
00055         lastX[i]=preLastX[i]=x[i]=Vec3(pos.x,pos.y,.5);
00056 
00057         static const REAL fak=4;
00058 
00059         tRandomizer & randomizer = tRandomizer::GetInstance();
00060         REAL a=fak*( randomizer.Get() - .5f );
00061         REAL b=fak*( randomizer.Get() - .5f );
00062         //      REAL a=fak*(rand()/static_cast<REAL>(RAND_MAX)-.5f);
00063         //      REAL b=fak*(rand()/static_cast<REAL>(RAND_MAX)-.5f);
00064         REAL c=1;
00065 
00066         eCoord xy(eCoord(c,b).Turn(dir));
00067 
00068         xDot[i]=Vec3(xy.x,xy.y,a);
00069         xDot[i]=xDot[i]*(1/xDot[i].Norm());
00070         xDot[i].x[2]+=1;
00071 
00072         heat[i]=2+randomizer.Get();
00073         //      heat[i]=2+rand()/REAL(RAND_MAX);
00074         lastBreak[i]=createTime;
00075     }
00076 #else
00077 #ifndef DEDICATED
00078     particle_handle = pGenParticleGroups(1, SPARKS);
00079 
00080     pCurrentGroup(particle_handle);
00081     // Generate particles along a very small line in the nozzle.
00082     pSource(SPARKS, PDLine(pVec(Position().x, Position().y, 0.3), pVec(Position().x, Position().y, 0.8)));
00083 #endif
00084 #endif
00085     // add to game grid
00086     this->AddToList();
00087 }
00088 
00089 gSpark::~gSpark(){  }
00090 
00091 // virtual eGameObject_type type();
00092 
00093 bool gSpark::Timestep(REAL currentTime){
00094 #ifndef USEPARTICLES
00095     REAL ts=currentTime-lastTime;
00096 
00097     for (int i=SPARKS-1;i>=0;i--){
00098         x[i]+=xDot[i]*ts;
00099         xDot[i].x[2]-=5*ts;
00100         heat[i]-=ts;
00101 
00102         if (x[i].x[2]<0){
00103             x[i].x[2]*=-1;
00104             xDot[i].x[2]*=-.5;
00105             lastBreak[i]=currentTime;
00106         }
00107     }
00108 
00109     if (currentTime>createTime+4)
00110         return true;
00111     else
00112         return false;
00113 #else
00114 #ifndef DEDICATED
00115     pCurrentGroup(particle_handle);
00116     // Set up the state.
00117     pVelocityD(PDCylinder(pVec(0.0, 0.0, 0.0), pVec(0.0, 0.0, 0.01), 0.01, 0.007));
00118     pColorD(PDLine(pVec(1.0, 1.0, 1.0), pVec(1.0, 1.0, 1.0)));
00119     pSize(40.0);
00120     //pStartingAge(0);
00121 
00122     //pRandomAccel(PDSphere(pVec(Position().x, Position().y, 0.0), 100.0));
00123 
00124     // Gravity.
00125     pGravity(pVec(0.0, 0.0, -0.001));
00126 
00127     // Bounce particles off a disc of radius 5.
00128     pBounce(-0.05, 1.0, 0.1, PDDisc(pVec(0, 0, 0), pVec(0, 0, 1), 5000000));
00129 
00130     // Kill particles below Z=0.
00131     pSink(false, PDPlane(pVec(0,0,0), pVec(0,0,1)));
00132 
00133     pKillOld(40);
00134 
00135     // Move particles to their new positions.
00136     pMove();
00137 
00138     // Finished when there are less than 1/10 of the original particles
00139     if (pGetGroupCount() < SPARKS/10) {
00140         pDeleteParticleGroups(particle_handle, particle_handle);
00141         return true;
00142     }
00143 #endif // dedicated
00144 #endif // particles
00145 
00146     lastTime=currentTime;
00147 
00148     return false;
00149 }
00150 
00151 void gSpark::InteractWith(eGameObject *,REAL ,int){}
00152 void gSpark::PassEdge(const eWall *,REAL ,REAL ,int){}
00153 
00154 void gSpark::Kill(){createTime=lastTime-100000;}
00155 
00156 
00157 #ifndef DEDICATED
00158 void gSpark::Render(const eCamera *cam){
00159 #ifndef USEPARTICLES
00160     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
00161 
00162     //glMatrixMode(GL_MODELVIEW);
00163     //glPushMatrix();
00164     //glLoadIdentity();
00165 
00166     //glDisable(GL_TEXTURE);
00167     glDisable(GL_TEXTURE_2D);
00168 
00169     BeginLines();
00170     for (int i=SPARKS-1;i>=0;i--){
00171     #define rmax 1.2
00172     #define gmax 1.1
00173         REAL ago=.2;
00174         if (ago>se_GameTime()-lastBreak[i])
00175             ago=se_GameTime()-  lastBreak[i];
00176 
00177         REAL a=heat[i]+1.5;
00178         if (a>1) a=1;
00179         if (a<0) a=0;
00180 
00181         if(!white_sparks) {
00182             if(i%2)
00183                 glColor4f(sparkowncolor_r,sparkowncolor_g,sparkowncolor_b,a);
00184             else
00185                 glColor4f(sparkenemycolor_r,sparkenemycolor_g,sparkenemycolor_b,a);
00186         }
00187         else {
00188             REAL r=heat[i]+1;
00189             if (r>rmax) r=rmax;
00190             if (r>1) r=2-r;
00191             if (r<0) r=0;
00192             REAL g=heat[i]+.5;
00193             if (g>gmax) g=gmax;
00194             if (g>1) g=2-g;
00195             if (g<0) g=0;
00196             REAL b=heat[i];
00197             if (b>1) b=1;
00198             if (b<0) b=0;
00199 
00200             glColor4f(r,g,b,a);
00201         }
00202 
00203         x[i].RenderVertex();
00204         preLastX[i]=x[i];
00205         preLastX[i]+=xDot[i]*(-ago*.8);
00206 
00207         preLastX[i].RenderVertex();
00208         preLastX[i]=lastX[i];
00209         lastX[i]=x[i];
00210     }
00211     RenderEnd();
00212     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00213     //glPopMatrix();
00214 #else
00215     pCurrentGroup(particle_handle);
00216     int cnt = (int)pGetGroupCount();
00217     if(cnt < 1) return;
00218 
00219     float *ptr;
00220     size_t flstride, pos3Ofs, posB3Ofs, size3Ofs, vel3Ofs, velB3Ofs, color3Ofs, alpha1Ofs, age1Ofs;
00221 
00222     cnt = (int)pGetParticlePointer(ptr, flstride, pos3Ofs, posB3Ofs,
00223                                    size3Ofs, vel3Ofs, velB3Ofs, color3Ofs, alpha1Ofs, age1Ofs);
00224     if(cnt < 1) return;
00225 
00226     glEnableClientState(GL_COLOR_ARRAY);
00227     glColorPointer(4, GL_FLOAT, int(flstride) * sizeof(float), ptr + color3Ofs);
00228 
00229     glEnableClientState(GL_VERTEX_ARRAY);
00230     glVertexPointer(3, GL_FLOAT, int(flstride) * sizeof(float), ptr + pos3Ofs);
00231 
00232     glDrawArrays(GL_POINTS, 0, cnt);
00233     glDisableClientState(GL_VERTEX_ARRAY);
00234     glDisableClientState(GL_COLOR_ARRAY);
00235 #endif
00236 }
00237 
00238 /*
00239 void gSpark::SoundMix(Uint8 *dest,unsigned int len,
00240                       int viewer,REAL rvol,REAL lvol){
00241     //  sound.Mix(dest,len,viewer,rvol*.5,lvol*.5,4);
00242 }*/
00243 #endif

Generated on Sat Mar 15 22:56:09 2008 for Armagetron Advanced by  doxygen 1.5.4