AaronCameron.net
I care not for your petty politics.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 01:56am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cPParticleSpec.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #include "particles/cPParticleSpec.h"
00027 
00028 #include "n2l/vfs.h"
00029 #include "n2l/dynVars.h"
00030 #include "n2l/resourceManagement.h"
00031 
00032 #include "particles/cParticle.h"
00033 
00034 //#include <iostream>
00035 //using namespace std;
00036 
00037 namespace n2l
00038 {
00039 
00040     /******************************************************************/
00041     cPParticleSpec::cPParticleSpec()
00042     {
00043         init();
00044     }
00045 
00046     /******************************************************************/
00047     cPParticleSpec::cPParticleSpec( const cPParticleSpec &iSpec )
00048     {
00049         init();
00050         *this = iSpec;
00051     }
00052 
00053     /******************************************************************/
00054     cPParticleSpec::cPParticleSpec( const cDynVar &iDef )
00055     {
00056         init();
00057         load(iDef);
00058     }
00059 
00060     /******************************************************************/
00061     cPParticleSpec::cPParticleSpec( const cVfsNodeInterface &iNode )
00062     {
00063         init();
00064         load(iNode);
00065     }
00066 
00067     /******************************************************************/
00068     cPParticleSpec::~cPParticleSpec()
00069     {
00070         free();
00071     }
00072 
00073     /******************************************************************/
00074     void cPParticleSpec::load( const cDynVar &iDef )
00075     {
00076         free();
00077         // Do the load here
00078         try
00079         {
00080             // Textures
00081             if (iDef.keyExists("textures"))
00082                 for (cDynVar::tConstIterator i=iDef["textures"].begin();
00083                     i!=iDef["textures"].end(); ++i)
00084                     mTextures.push_back( cResourceManager::get<cGLTexture>(
00085                         i->second) );
00086 
00087             // Colours
00088             if (iDef.keyExists("colourStartToEnd")) {
00089                 mColourSelect = ColourSelect_C1ToC2;
00090                 cDynVar c;
00091                 c = iDef["colourStartToEnd"]["start"];
00092                 mColours.push_back( cColour(c[0],c[1],c[2],c[3]) );
00093                 c = iDef["colourStartToEnd"]["end"];
00094                 mColours.push_back( cColour(c[0],c[1],c[2],c[3]) );
00095 
00096             } else {} // Put other colour selection types here          
00097 
00098             // Growth Ranges
00099             if (iDef.keyExists("growth")) {
00100                 mGrowthSelect = GrowthSelect_Uniform;
00101                 mGrowths.push_back( tVector2f(iDef["growth"][0],iDef["growth"][1]) );
00102 
00103             } else {} // Put other growth selection types here
00104 
00105             // Size Ranges
00106             if (iDef.keyExists("size")) {
00107                 mSizeSelect = SizeSelect_Uniform;
00108                 mSizes.push_back( tVector2f(iDef["size"][0],iDef["size"][1]) );
00109 
00110             } else {} // Put other growth selection types here
00111 
00112             // Others
00113             mLifespan = tSint(iDef.keyValueOr( "lifespan", tSint(mLifespan) ));
00114 
00115         }
00116         catch ( const cException &iException )
00117         {
00118             throw cBadDataUseException( "cPParticleSpec::load",
00119                 tString("Not a valid particle spec: ")+
00120                 tString(iException) );
00121         }
00122     }
00123 
00124     /******************************************************************/
00125     void cPParticleSpec::load( const cVfsNodeInterface &iNode )
00126     {
00127         tVfsFileBuffer buffer;
00128         tUint DataOffset;
00129         try
00130         {
00131             DataOffset = vfsNodeFileWithHeader( iNode,
00132                                                 "n2l::cPParticleSpec",
00133                                                 buffer );
00134         }
00135         catch ( const cBadDataUseException & iException )
00136         {
00137             throw cBadDataUseException( "cPParticleSpec::load",
00138                                         tString("Failed to load: \"")+
00139                                         iNode.name() + "\": " +
00140                                         tString(iException) );
00141         }
00142         cDynVar def;
00143         def.unserialize( buffer, DataOffset );
00144         load( def );
00145     }
00146 
00147     /******************************************************************/
00148 /*  cPParticleSpec &cPParticleSpec::operator =( const cPParticleSpec &iSpec )
00149     {
00150         // Do a copy here.
00151 
00152         
00153         return *this;
00154     }
00155 */
00156     /******************************************************************/
00157     void cPParticleSpec::clear()
00158     {
00159         free();
00160         init();
00161     }
00162 
00163     /******************************************************************/
00164     cParticle *const cPParticleSpec::newParticle(
00165         const tVector3f &iPos, const tVector3f &iVel ) const
00166     {
00167         static const cColour White( 1.0f,1.0f,1.0f,1.0f );
00168         static const cColour Black( 0.0f,0.0f,0.0f,1.0f );
00169         static const tVector2f NoGrowth( 0.0f,0.0f );
00170         static const tVector2f OneSize( 1.0f,1.0f );
00171 
00172         cParticle *p( new cParticle() );
00173 
00174         // ------------------------------
00175         if (!mTextures.empty())
00176             p->mTexture = mTextures[ n2lRandom(mTextures.size()) ];
00177 
00178         // ------------------------------
00179         switch (mColourSelect)
00180         {
00181             case ColourSelect_C1ToC2:
00182                 p->mColour = mColours[0];
00183                 p->mStartColour = mColours[0];
00184                 p->mEndColour = mColours[1];
00185                 break;
00186 
00187             default:
00188             case ColourSelect_WhiteToBlack:
00189                 p->mColour = White;
00190                 p->mStartColour = White;
00191                 p->mEndColour = Black;
00192                 break;
00193         }
00194 
00195 
00196         // ------------------------------
00197         switch (mGrowthSelect)
00198         {
00199             case GrowthSelect_Uniform:
00200                 p->mGrowth = mGrowths[0];
00201                 break;
00202 
00203             default:
00204             case GrowthSelect_Static:
00205                 p->mGrowth = NoGrowth;
00206                 break;
00207         }
00208 
00209         // ------------------------------
00210         switch (mSizeSelect)
00211         {
00212 
00213             case SizeSelect_Uniform:
00214 //              cout << "Created new p with size: " << mSizes[0].dump() << endl;
00215                 p->mSize = mSizes[0];
00216                 break;
00217 
00218             default:
00219             case SizeSelect_One:
00220                 p->mSize = OneSize;
00221                 break;
00222         }
00223 
00224         // ------------------------------
00225         // Other stuff
00226         p->mTimeToLive = mLifespan;
00227 
00228         p->mPos = iPos;
00229         p->mVelocity = iVel;
00230 
00231         return p;
00232     }
00233 
00234     /******************************************************************/
00235     void cPParticleSpec::init()
00236     {
00237         mColourSelect = ColourSelect_WhiteToBlack;
00238         mGrowthSelect = GrowthSelect_Static;
00239         mSizeSelect = SizeSelect_One;
00240         mLifespan = 1000;
00241     }
00242 
00243     /******************************************************************/
00244     void cPParticleSpec::free()
00245     {
00246         mColours.clear();
00247         mTextures.clear();
00248         mGrowths.clear();
00249         mSizes.clear();
00250     }
00251     
00252 
00253 }
©2012 Aaron Cameron