landscapeig_manager.cpp File Reference


Detailed Description

Id
landscapeig_manager.cpp,v 1.14 2003/12/10 10:17:47 corvazier Exp

Definition in file landscapeig_manager.cpp.

#include "std3d.h"
#include "nel/3d/landscapeig_manager.h"
#include "3d/scene_user.h"
#include "3d/instance_group_user.h"
#include "3d/shape.h"
#include "nel/misc/common.h"
#include "nel/misc/debug.h"
#include "nel/misc/path.h"
#include "nel/misc/file.h"
#include "nel/misc/hierarchical_timer.h"
#include <fstream>

Go to the source code of this file.

Defines

#define NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG   H_AUTO_USE( NL3D_Load_Zone_IG )
#define NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG   H_AUTO_USE( NL3D_Unload_Zone_IG )

Functions

 H_AUTO_DECL (NL3D_Load_Zone_IG) H_AUTO_DECL(NL3D_Unload_Zone_IG) namespace NL3D


Define Documentation

#define NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG   H_AUTO_USE( NL3D_Load_Zone_IG )
 

Referenced by H_AUTO_DECL().

#define NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG   H_AUTO_USE( NL3D_Unload_Zone_IG )
 

Referenced by H_AUTO_DECL().


Function Documentation

H_AUTO_DECL NL3D_Load_Zone_IG   ) 
 

Definition at line 45 of file landscapeig_manager.cpp.

References file, NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG, NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG, NL3D_MEM_LANDSCAPE_IG, nlassert, nlwarning, NLMISC::strlwr(), NLMISC::strupr(), and uint.

00052 {
00053 
00054 
00055 // ***************************************************************************
00056 CLandscapeIGManager::CInstanceGroupElement::CInstanceGroupElement(UInstanceGroup *ig, const char *fileName)
00057 {
00058         NL3D_MEM_LANDSCAPE_IG
00059         Ig = ig;
00060         AddedToScene = false;
00061         if (fileName != NULL)
00062                 FileName = fileName;
00063 }
00064 
00065 // ***************************************************************************
00066 void    CLandscapeIGManager::CInstanceGroupElement::release()
00067 {
00068         NL3D_MEM_LANDSCAPE_IG
00069         delete Ig;
00070         Ig= NULL;
00071 }
00072 
00073 
00074 // ***************************************************************************
00075 CLandscapeIGManager::CLandscapeIGManager()
00076 {
00077         NL3D_MEM_LANDSCAPE_IG
00078         _Scene=NULL;
00079 }
00080 // ***************************************************************************
00081 CLandscapeIGManager::~CLandscapeIGManager()
00082 {
00083         NL3D_MEM_LANDSCAPE_IG
00084         // reset should have been called.
00085         if(_Scene!=NULL)
00086                 throw Exception("CLandscapeIGManager not reseted");
00087 }
00088 // ***************************************************************************
00089 void    CLandscapeIGManager::initIG(UScene *scene, const std::string &igDesc, UDriver *driver, uint selectedTexture, 
00090                                                                         NLMISC::IProgressCallback *callBack)
00091 {
00092         NL3D_MEM_LANDSCAPE_IG
00093         nlassert(scene);
00094         _Scene= scene;
00095 
00096         // Load the file.
00097         if(igDesc.empty())
00098                 return;
00099 
00100         string igFile = CPath::lookup(igDesc);
00101 
00102         //ifstream file(igFile.c_str(), ios::in);
00103 
00104         CIFile file;
00105 
00106         // Shape to add should be empty !
00107         nlassert(_ShapeAdded.empty ());
00108         
00109         // if loading ok.
00110         //if(file.is_open())
00111         if (file.open (igFile))
00112         {
00113                 char tmpBuff[260];
00114                 char delimiterBox[] = "\t";
00115                 // While the end of the file is not reached.
00116                 while(!file.eof())
00117                 {
00118                         // Get a line
00119                         file.getline(tmpBuff, 260);
00120                         char *token = strtok(tmpBuff, delimiterBox);
00121                         // create the instance group.
00122                         if(token != NULL)
00123                         {
00124                                 if( _ZoneInstanceGroupMap.find(token)!=_ZoneInstanceGroupMap.end() )
00125                                         throw Exception("CLandscapeIGManager::initIG() found 2 igs with same name in %s", igFile.c_str());
00126                                 else
00127                                 {
00128                                         // create the instanceGroup.
00129                                         UInstanceGroup  *ig = UInstanceGroup::createInstanceGroup(token);
00130                                         if (ig)
00131                                         {
00132                                                 // add it to the map.
00133                                                 string  tokId= token;
00134                                                 strupr(tokId);
00135                                                 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00136 
00137                                                 // Add a reference on the shapes
00138                                                 CInstanceGroup &_ig = static_cast<CInstanceGroupUser*>(ig)->getInternalIG();
00139                                                 CScene &_scene = static_cast<CSceneUser*>(scene)->getScene();
00140                                                 uint i;
00141                                                 for (i=0; i<_ig.getNumInstance(); i++)
00142                                                 {
00143                                                         // Get the instance name
00144                                                         string shapeName;
00145                                                         _ig.getShapeName(i, shapeName);
00146                                                         if (!shapeName.empty ())
00147                                                         {
00148                                                                 if (strlwr(CFile::getExtension(shapeName)) != "pacs_prim")
00149                                                                 {
00150                                                                         // Insert a new shape ?
00151                                                                         if (_ShapeAdded.find(shapeName) == _ShapeAdded.end())
00152                                                                         {
00153                                                                                 // Shape present ?
00154                                                                                 CShapeBank *shapeBank = _scene.getShapeBank();
00155                                                                                 IShape *shape = NULL;
00156                                                                                 if (shapeBank->isPresent (shapeName) == CShapeBank::NotPresent)
00157                                                                                         shapeBank->load (shapeName);
00158                                                                                 if (shapeBank->isPresent (shapeName) == CShapeBank::Present)
00159                                                                                         shape = shapeBank->addRef(shapeName);
00160 
00161                                                                                 // Shape loaded ?
00162                                                                                 if (shape)
00163                                                                                 {
00164                                                                                         // Insert the shape
00165                                                                                         CSmartPtr<IShape> *smartPtr = new CSmartPtr<IShape>;
00166                                                                                         *smartPtr = shape;
00167                                                                                         _ShapeAdded.insert (TShapeMap::value_type (shapeName, smartPtr));
00168 
00169                                                                                         // Flush the shape
00170                                                                                         IDriver *_driver = static_cast<CDriverUser*>(driver)->getDriver();
00171                                                                                         shape->flushTextures(*_driver, selectedTexture);
00172                                                                                 }
00173                                                                         }
00174                                                                 }
00175                                                         }
00176                                                 }
00177                                         }
00178                                         else
00179                                         {
00180                                                 nlwarning ("CLandscapeIGManager::initIG() Can't load instance group '%s' in '%s'", token, igFile.c_str());
00181                                         }
00182                                 }
00183                         }
00184                 }
00185                 file.close();
00186         }
00187         else
00188         {
00189                 nlwarning ("Couldn't load '%s'", igFile.c_str());
00190         }
00191 }
00192 // ***************************************************************************
00193 UInstanceGroup *CLandscapeIGManager::loadZoneIG(const std::string &name)
00194 {
00195         NL3D_MEM_LANDSCAPE_IG
00196         NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG
00197 
00198         if(name=="") 
00199                 return NULL;
00200 
00201         // try to find this InstanceGroup.
00202         ItZoneInstanceGroupMap  it;
00203         it= _ZoneInstanceGroupMap.find( translateName(name) );
00204 
00205         // if found.
00206         if( it!= _ZoneInstanceGroupMap.end() )
00207         {
00208                 // if not already added to the scene.
00209                 if( !it->second.AddedToScene )
00210                 {
00211                         // add to the scene.
00212                         if (it->second.Ig != NULL)
00213                         {
00214                                 it->second.Ig->addToScene(*_Scene);
00215                                 it->second.AddedToScene= true;
00216                         }
00217                 }
00218                 return it->second.Ig;
00219         }
00220         else
00221         {
00222                 return NULL;
00223         }
00224 }
00225 // ***************************************************************************
00226 void    CLandscapeIGManager::loadArrayZoneIG(const std::vector<std::string> &names, std::vector<UInstanceGroup *> *dest /*= NULL*/)
00227 {
00228         NL3D_MEM_LANDSCAPE_IG
00229         if (dest)
00230         {
00231                 dest->clear();
00232                 dest->reserve(names.size());
00233         }
00234         for(uint i=0; i<names.size(); i++)
00235         {
00236                 UInstanceGroup *ig = loadZoneIG(names[i]);
00237                 if (dest && ig)
00238                 {
00239                         dest->push_back(ig);
00240                 }
00241         }
00242 }
00243 
00244 // ***************************************************************************
00245 void    CLandscapeIGManager::unloadArrayZoneIG(const std::vector<std::string> &names)
00246 {
00247         NL3D_MEM_LANDSCAPE_IG
00248         for(uint i=0; i<names.size(); i++)
00249         {
00250                 unloadZoneIG(names[i]);
00251         }
00252 }
00253 
00254 // ***************************************************************************
00255 void    CLandscapeIGManager::unloadZoneIG(const std::string &name)
00256 {
00257         NL3D_MEM_LANDSCAPE_IG
00258         NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG
00259         if(name=="")
00260                 return;
00261 
00262         // try to find this InstanceGroup.
00263         ItZoneInstanceGroupMap  it;
00264         it= _ZoneInstanceGroupMap.find( translateName(name) );
00265 
00266         // if found.
00267         if( it!= _ZoneInstanceGroupMap.end() )
00268         {
00269                 // if really added to the scene.
00270                 if( it->second.AddedToScene )
00271                 {
00272                         // remove from the scene.
00273                         it->second.Ig->removeFromScene(*_Scene);
00274                         it->second.AddedToScene= false;
00275                 }
00276         }
00277 }
00278 
00279 // ***************************************************************************
00280 bool    CLandscapeIGManager::isIGAddedToScene(const std::string &name) const
00281 {
00282         NL3D_MEM_LANDSCAPE_IG
00283         if(name=="")
00284                 return false;
00285 
00286         // try to find this InstanceGroup.
00287         ConstItZoneInstanceGroupMap     it;
00288         it= _ZoneInstanceGroupMap.find( translateName(name) );
00289 
00290         // if found.
00291         if( it!= _ZoneInstanceGroupMap.end() )
00292                 return  it->second.AddedToScene;
00293         else
00294                 return false;
00295 }
00296 
00297 // ***************************************************************************
00298 UInstanceGroup  *CLandscapeIGManager::getIG(const std::string &name) const
00299 {
00300         NL3D_MEM_LANDSCAPE_IG
00301         if(name=="")
00302                 return NULL;
00303 
00304         // try to find this InstanceGroup.
00305         ConstItZoneInstanceGroupMap     it;
00306         it= _ZoneInstanceGroupMap.find( translateName(name) );
00307 
00308         // if found.
00309         if( it!= _ZoneInstanceGroupMap.end() )
00310                 return it->second.Ig;
00311         else
00312                 return NULL;
00313 }
00314 
00315 
00316 // ***************************************************************************
00317 std::string             CLandscapeIGManager::translateName(const std::string &name) const
00318 {
00319         NL3D_MEM_LANDSCAPE_IG
00320         std::string             ret;
00321         ret= name + ".ig";
00322         strupr(ret);
00323 
00324         return ret;
00325 }
00326 
00327 
00328 // ***************************************************************************
00329 void    CLandscapeIGManager::reset()
00330 {
00331         NL3D_MEM_LANDSCAPE_IG
00332         while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00333         {
00334                 string  name= _ZoneInstanceGroupMap.begin()->first;
00335                 // first remove from scene
00336                 unloadZoneIG( name.substr(0, name.find('.')) );
00337 
00338                 // then delete this entry.
00339                 _ZoneInstanceGroupMap.begin()->second.release();
00340                 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00341         }
00342 
00343         // For all shape reference
00344         TShapeMap::iterator ite = _ShapeAdded.begin ();
00345         while (ite != _ShapeAdded.end())
00346         {
00347                 // Unreference shape
00348                 CScene &_scene = static_cast<CSceneUser*>(_Scene)->getScene();
00349                 CSmartPtr<IShape> *smartPtr = (CSmartPtr<IShape> *)(ite->second);
00350                 IShape *shapeToRelease = *smartPtr;
00351                 *smartPtr = NULL;
00352                 _scene.getShapeBank()->release(shapeToRelease);
00353                 delete smartPtr;
00354         
00355                 // Next
00356                 ite++;
00357         }
00358         _ShapeAdded.clear ();
00359         
00360         _Scene=NULL;
00361 }
00362 
00363 
00364 // ***************************************************************************
00365 void    CLandscapeIGManager::reloadAllIgs()
00366 {
00367         NL3D_MEM_LANDSCAPE_IG
00368         vector<std::string>             bkupIgFileNameList;
00369         vector<bool>                    bkupIgAddedToScene;
00370 
00371         // First, erase all igs.
00372         while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00373         {
00374                 string  name= _ZoneInstanceGroupMap.begin()->first;
00375 
00376                 // bkup the state of this ig.
00377                 bkupIgFileNameList.push_back(_ZoneInstanceGroupMap.begin()->second.FileName);
00378                 bkupIgAddedToScene.push_back(_ZoneInstanceGroupMap.begin()->second.AddedToScene);
00379 
00380                 // first remove from scene
00381                 unloadZoneIG( name.substr(0, name.find('.')) );
00382 
00383                 // then delete this entry.
00384                 _ZoneInstanceGroupMap.begin()->second.release();
00385                 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00386         }
00387 
00388         // Then reload all Igs.
00389         for(uint i=0; i<bkupIgFileNameList.size(); i++)
00390         {
00391                 const   char    *token= bkupIgFileNameList[i].c_str();
00392                 UInstanceGroup  *ig = UInstanceGroup::createInstanceGroup(token);
00393                 // add it to the map.
00394                 string  tokId= token;
00395                 strupr(tokId);
00396                 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00397 
00398                 // If was addedToScene before, re-add to scene now.
00399                 if(bkupIgAddedToScene[i])
00400                 {
00401                         loadZoneIG( tokId.substr(0, tokId.find('.')) );
00402                 }
00403         }
00404 }
00405 
00406 
00407 // ***************************************************************************
00408 void CLandscapeIGManager::getAllIG(std::vector<UInstanceGroup *> &dest) const
00409 {       
00410         NL3D_MEM_LANDSCAPE_IG
00411         dest.clear();
00412         dest.reserve(_ZoneInstanceGroupMap.size());
00413         // add the instances
00414         for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00415         {                       
00416                 dest.push_back(it->second.Ig);                  
00417         }
00418 }
00419 
00420 // ***************************************************************************
00421 void CLandscapeIGManager::getAllIGWithNames(std::vector<std::pair<UInstanceGroup *, std::string> > &dest) const
00422 {
00423         NL3D_MEM_LANDSCAPE_IG
00424         dest.clear();
00425         dest.reserve(_ZoneInstanceGroupMap.size());
00426         // add the instances
00427         for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00428         {                       
00429                 dest.push_back(std::make_pair(it->second.Ig, it->second.FileName));                     
00430         }
00431 }
00432 
00433 
00434 } // NL3D


Generated on Tue Mar 16 06:42:51 2004 for NeL by doxygen 1.3.6