TileStorm.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Das Tank - Copyright (C) 2006, 2007 Windsor Schmidt <windsor@windsorworld.net>
00004 //
00005 // This program is free software; you can redistribute it and/or modify it
00006 // under the terms of the GNU General Public License as published by the Free
00007 // Software Foundation; either version 2 of the License, or (at your option)
00008 // any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful, but WITHOUT
00011 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 // more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along with
00016 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017 // Place, Suite 330, Boston, MA 02111-1307 USA
00018 //
00020 
00021 
00022 #ifndef STDAFX
00023 #include "stdafx.h"
00024 #endif
00025 
00029 TileStorm::TileStorm(void)
00030 {
00031        int i;
00032        vPosition.x = 0;
00033        vPosition.y = 0;
00034        vPosition.z = 0;
00035 
00036        vForce.x = 0;
00037        vForce.y = 0;
00038        vForce.z = 0;
00039 
00040        fSpreadAngle = PI/2; // 90 degrees
00041        fFriction = 1;
00042        iNumParticles = 1;
00043 
00044        iTexture = TEXID_NULL;
00045 
00046        pParticles = (Particle *) malloc(sizeof(Particle) * iNumParticles);
00047 
00048        for(i=0; i<iNumParticles; i++) // init particles
00049        {
00050               pParticles[i].fPosX = 0;
00051               pParticles[i].fPosY = 0;
00052               pParticles[i].fVelX = 0;
00053               pParticles[i].fVelY = 0;
00054               pParticles[i].fRotVel = 0;
00055               pParticles[i].fSize = .01;
00056               pParticles[i].bAlive = true;
00057               pParticles[i].fAngle = 0;
00058               pParticles[i].fEnergy = 1;
00059        }
00060 
00061        fRandomness = 0;
00062        fDecayRate = 0;
00063 }
00064 
00065 
00069 TileStorm::TileStorm(Vector vPos, Vector vFce, float fSAngle, float fFric, float fRand, int iParticles, int tex)
00070 {
00071        int i;
00072        Vector vTemp;
00073        float angle1, angle2;
00074 
00075        vPosition = vPos;
00076        vForce = vFce;
00077 
00078        fSpreadAngle = fSAngle;
00079        fFriction = fFric;
00080        iNumParticles = iParticles;
00081 
00082        iTexture = tex;
00083 
00084        angle1 = -(fSAngle/2);
00085 
00086        if(iNumParticles > MAX_PARTICLES) { iNumParticles = MAX_PARTICLES; } // limit particles
00087 
00088        pParticles = (Particle *) malloc(sizeof(Particle) * iNumParticles); // reserve memory
00089 
00090        for(i=0; i<iNumParticles; i++) // init particles
00091        {
00092               fRandomness = fRand;
00093               fDecayRate = .001;
00094 
00095               vTemp = vForce;
00096               vTemp *= 1 + ((float)(rand()%100) -50) / 100 * fRandomness;
00097               angle2 = angle1 + ((float) i/ (float) iNumParticles) * fSpreadAngle;
00098               vTemp = VRotate2D(angle2 + ((float)(rand()%100) -50) / 100 * fRandomness, vTemp);
00099 
00100               pParticles[i].fPosX = vPos.x;
00101               pParticles[i].fPosY = vPos.y;
00102               pParticles[i].fVelX = vTemp.x;
00103               pParticles[i].fVelY = vTemp.y;
00104               pParticles[i].fSize = .1 + ((float)(rand()%100) - 50) / 100 * 6.0;
00105               pParticles[i].bAlive = true;
00106               pParticles[i].fAngle = 0;
00107               pParticles[i].fEnergy = 1;
00108               pParticles[i].fDecayRate = fDecayRate + (((float)(rand()%100)) / 100) * .0001;
00109 
00110               pParticles[i].fRotVel = ((float)(rand()%100) -50) / 100 * 1.0;
00111        }
00112 }
00113 
00114 
00117 TileStorm::~TileStorm(void)
00118 {
00119        free(pParticles); // free memory reserved for particles
00120 }
00121 
00122 
00128 bool TileStorm::Step(float timeStep)
00129 {
00130        int i;
00131        bool someAlive = false; // still some particles with energy
00132 
00133        for(i=0; i<iNumParticles; i++)
00134        {
00135               if(pParticles[i].fEnergy <= 0)
00136               {
00137                      pParticles[i].bAlive = false;
00138                      continue; // done with this particle
00139               }
00140 
00141               pParticles[i].fVelX *= 1 / fFriction;
00142               pParticles[i].fVelY *= 1 / fFriction;
00143 
00144               pParticles[i].fPosX += (pParticles[i].fVelX * timeStep);
00145               pParticles[i].fPosY += (pParticles[i].fVelY * timeStep);
00146 
00147               pParticles[i].fAngle += pParticles[i].fRotVel;
00148 
00149               pParticles[i].fEnergy -= fDecayRate;
00150 
00151               someAlive = true; // at least this particle is still alive
00152        }
00153 
00154        if(someAlive == false) // no particles left alive
00155        {
00156               return true; // expired
00157        }
00158        else
00159        {
00160               return false; // not expired yet
00161        }
00162 }
00163 
00165 void TileStorm::Draw(void)
00166 {
00167        int i;
00168 
00169        glEnable (GL_BLEND);
00170        glEnable(GL_TEXTURE_2D);
00171        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00172        glBindTexture( GL_TEXTURE_2D, GetTexture(iTexture));
00173        glColor4f(1, 1, 1, 1);
00174 
00175        glPushMatrix();
00176 
00177        for(i=0; i<iNumParticles; i++)
00178        {
00179               glPushMatrix();
00180               glTranslatef(pParticles[i].fPosX, pParticles[i].fPosY,.5);
00181               glRotatef(pParticles[i].fAngle,0,0,1);
00182 
00183               glBegin(GL_QUADS);                        // start drawing a polygon (4 sided)
00184               glTexCoord2d(0.0,1.0); glVertex3f(-1, 1, 0.1f);         // top left
00185               glTexCoord2d(1.0,1.0); glVertex3f(1, 1, 0.1f);          // top right
00186               glTexCoord2d(1.0,0.0); glVertex3f(1, -1, 0.1f);  // bottom right
00187               glTexCoord2d(0.0,0.0); glVertex3f(-1, -1, 0.1f); // bottom left       
00188               glEnd(); // done with the polygon
00189 
00190               glPopMatrix();
00191        }
00192        glPopMatrix();
00193 }

 

Copyright Windsor Schmidt 2006 - All rights reserved.