AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 02:49am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cRSkyMobile2.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  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 #include "renderObjects/cRSkyMobile2.h"
00026 
00027 #include "n2l/vfs.h"
00028 #include "n2l/dynVars.h"
00029 #include "n2l/materials.h"
00030 #include "n2l/resourceManagement.h"
00031 
00032 #include "renderObjects/cRFrustum.h"
00033 
00034 #include "renderObjects/cRSkyMobileSprite.h"
00035 
00036 using namespace std;
00037 
00038 namespace n2l
00039 {
00040 
00041     /**************************************************************************/
00042     cRSkyMobile2::cRSkyMobile2()
00043     {
00044         init();
00045     }
00046 
00047     /**************************************************************************/
00048     cRSkyMobile2::cRSkyMobile2( const cRSkyMobile2 &iMobile )
00049     {
00050         const tUint Size = iMobile.mSprites.size();
00051         for (tUint i=0; i!=Size; ++i)
00052             mSprites.push_back( new cRSkyMobileSprite(*iMobile.mSprites[i]) );
00053 
00054         mViewpoint = iMobile.mViewpoint;
00055 
00056         sort();     
00057         mSkipSort = false;
00058     }
00059 
00060     /**************************************************************************/
00061     cRSkyMobile2::cRSkyMobile2( const cVfsNodeInterface &iNode )
00062     {
00063         init();
00064         load( iNode );
00065     }
00066 
00067     /**************************************************************************/
00068     cRSkyMobile2::cRSkyMobile2( const cDynVar &iDef )
00069     {
00070         init();
00071         load( iDef );
00072     }
00073 
00074     /**************************************************************************/
00075     cRSkyMobile2::~cRSkyMobile2()
00076     {
00077     }
00078 
00079     /**************************************************************************/
00080     void cRSkyMobile2::load( const cDynVar &iDef )
00081     {
00082         clear();
00083 
00084         // Load mobiles, last (and only, for now)
00085         if (!iDef["mobiles"].isArray()) return;
00086 
00087         try
00088         {
00089             mSkipSort = true;
00090             const cDynVar::tConstIterator Last( iDef["mobiles"].end() );
00091             for (cDynVar::tConstIterator i=iDef["mobiles"].begin();
00092                 i!=Last; ++i)
00093             {
00094                 try {
00095                     addSprite( i->first, asVector3f( i->second["pos"] ),
00096                         asVector3f( i->second["size"] ),
00097                         i->second.keyValueOr("zRot", .0f),
00098                         cResourceManager::get<cGLTexture>(
00099                             i->second["texture"] ));
00100                 }
00101                 catch ( const cNoResultException &iNR )
00102                 {
00103                     throw cBadDataUseException( "cRSkyMobile2::load",
00104                         "Failed to load requested sprite \"" + i->first +
00105                         "\" because: " + tString(iNR) );
00106                 }
00107             }
00108             mSkipSort = false;
00109         }
00110         catch ( const cException &iException )
00111         {
00112             clear();
00113             mSkipSort = false;
00114             iException.rethrow();
00115         }
00116         sort();
00117     }
00118 
00119     /**************************************************************************/
00120     void cRSkyMobile2::load( const cVfsNodeInterface &iNode )
00121     {
00122         tVfsFileBuffer buffer;
00123         tUint DataOffset;
00124         try
00125         {
00126             DataOffset = vfsNodeFileWithHeader( iNode, "n2l::cRSkyMobile2",
00127                 buffer );
00128         }
00129         catch ( const cBadDataUseException &iE )
00130         {
00131             throw cBadDataUseException( "cRSkyMobile2::load",
00132                 "Failed to load: \"" + iNode.name() + "\": " + tString(iE) );
00133         }
00134         cDynVar def;
00135         def.unserialize( buffer, DataOffset );
00136         load( def );
00137     }
00138 
00139     /**************************************************************************/
00140     void cRSkyMobile2::draw( const cRFrustum &iFrustum,
00141         const tVector3f &iPosAdjust, const tUint iOptions ) const
00142     {
00143         glPushAttrib( GL_ALL_ATTRIB_BITS );
00144 
00145         glEnable( GL_TEXTURE_2D );
00146         glDisable( GL_DEPTH_TEST );
00147         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00148         glEnable( GL_BLEND );
00149         glDepthMask( false );
00150         glCullFace( GL_BACK );
00151         glDisable( GL_CULL_FACE );
00152         glColor4f(1,1,1,1);
00153 
00154         const tUint NumSprites( mSprites.size() );
00155 
00156         for (tUint i=0; i<NumSprites; ++i)
00157             mSprites[i]->draw( iFrustum, iPosAdjust );
00158 
00159         glPopAttrib();
00160     }
00161 
00162     /**************************************************************************/
00163     const cRSkyMobileSprite &cRSkyMobile2::mustFind(
00164         const tString &iName ) const
00165     {
00166         const tSpriteList::const_iterator Last = mSprites.end();
00167         for (tSpriteList::const_iterator i=mSprites.begin(); i!=Last; ++i)
00168             if ((*i)->name()==iName) return *(*i);
00169 
00170         throw cOutOfBoundsException( "cRSkyMobile2::mustFind",
00171             "No such sprite.", iName );
00172     }
00173 
00174     /**************************************************************************/
00175     void cRSkyMobile2::addSprite( const tString &iName,
00176         const tVector3f &iPos, const tVector3f &iSize, const tFloat &iZRot, 
00177         const cAutoPtr<const cGLTexture> &iTexture )
00178     {
00179         mSprites.push_back( new cRSkyMobileSprite(iName,iTexture) );
00180         mSprites[ mSprites.size()-1 ]->locate( iPos, iSize, iZRot, 100.0f );
00181 
00182         // Stupid to sort the whole list every time.  But hey, I suck.
00183         if (!mSkipSort) sort();
00184     }
00185 
00186     /**************************************************************************/
00187     void cRSkyMobile2::reposFromViewpoint( const tVector3f &iViewpoint )
00188     {
00189         const tUint Size = mSprites.size();
00190         for (tUint i=0; i!=Size; ++i)
00191             mSprites[i]->relocate( iViewpoint );
00192         mViewpoint = iViewpoint;
00193         if (!mSkipSort) sort();
00194     }
00195 
00196     /**************************************************************************/
00197     void cRSkyMobile2::clear()
00198     {
00199         mSprites.clear();
00200         mSkipSort = false;
00201     }
00202 
00203     /**************************************************************************/
00204     void cRSkyMobile2::init()
00205     {
00206         mSkipSort = false;
00207         mViewpoint.set( .0f, .0f, .0f );
00208     }
00209 
00210     /**************************************************************************/
00211     void cRSkyMobile2::sort()
00212     {
00213         const tUint NumSprites( mSprites.size() );
00214         if (NumSprites<2) return;
00215 
00216         for (tUint i1=0; i1<NumSprites-1; ++i1)
00217             for (tUint i2=0; i2<NumSprites-1-i1; ++i2)
00218                 // I should be shot, I know.
00219                 if ( (mSprites[i2+1]->pos()-mViewpoint).magnitude() >
00220                     (mSprites[i2]->pos()-mViewpoint).magnitude())
00221                 {
00222                     cAutoPtr<cRSkyMobileSprite> tmp = mSprites[i2+1];
00223                     mSprites[i2+1] = mSprites[i2];
00224                     mSprites[i2] = tmp;
00225                 }
00226     }
00227 
00228 } // namespace
©2012 Aaron Cameron