# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

landscapeig_manager.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "nel/3d/landscapeig_manager.h"
00029 #include "nel/3d/u_scene.h"
00030 #include "nel/3d/u_instance_group.h"
00031 #include "nel/misc/common.h"
00032 #include "nel/misc/debug.h"
00033 #include "nel/misc/path.h"
00034 #include "nel/misc/file.h"
00035 
00036 // std.
00037 #include <fstream>
00038 
00039 
00040 using namespace NLMISC;
00041 using namespace std;
00042 
00043 
00044 namespace NL3D 
00045 {
00046 
00047 
00048 // ***************************************************************************
00049 CLandscapeIGManager::CInstanceGroupElement::CInstanceGroupElement(UInstanceGroup *ig, const char *fileName)
00050 {
00051         NL3D_MEM_LANDSCAPE_IG
00052         Ig = ig;
00053         AddedToScene = false;
00054         if (fileName != NULL)
00055                 FileName = fileName;
00056 }
00057 
00058 // ***************************************************************************
00059 void    CLandscapeIGManager::CInstanceGroupElement::release()
00060 {
00061         NL3D_MEM_LANDSCAPE_IG
00062         delete Ig;
00063         Ig= NULL;
00064 }
00065 
00066 
00067 // ***************************************************************************
00068 CLandscapeIGManager::CLandscapeIGManager()
00069 {
00070         NL3D_MEM_LANDSCAPE_IG
00071         _Scene=NULL;
00072 }
00073 // ***************************************************************************
00074 CLandscapeIGManager::~CLandscapeIGManager()
00075 {
00076         NL3D_MEM_LANDSCAPE_IG
00077         // reset should have been called.
00078         if(_Scene!=NULL)
00079                 throw Exception("CLandscapeIGManager not reseted");
00080 }
00081 // ***************************************************************************
00082 void    CLandscapeIGManager::initIG(UScene *scene, const std::string &igDesc)
00083 {
00084         NL3D_MEM_LANDSCAPE_IG
00085         nlassert(scene);
00086         _Scene= scene;
00087 
00088         // Load the file.
00089         if(igDesc.empty())
00090                 return;
00091 
00092         string igFile = CPath::lookup(igDesc);
00093 
00094         //ifstream file(igFile.c_str(), ios::in);
00095 
00096         CIFile file;
00097 
00098         // if loading ok.
00099         //if(file.is_open())
00100         if (file.open (igFile))
00101         {
00102                 char tmpBuff[260];
00103                 char delimiterBox[] = "\t";
00104                 // While the end of the file is not reached.
00105                 while(!file.eof())
00106                 {
00107                         // Get a line
00108                         file.getline(tmpBuff, 260);
00109                         char *token = strtok(tmpBuff, delimiterBox);
00110                         // create the instance group.
00111                         if(token != NULL)
00112                         {
00113                                 if( _ZoneInstanceGroupMap.find(token)!=_ZoneInstanceGroupMap.end() )
00114                                         throw Exception("CLandscapeIGManager::initIG() found 2 igs with same name in %s", igFile.c_str());
00115                                 else
00116                                 {
00117                                         // create the instanceGroup.
00118                                         UInstanceGroup  *ig = UInstanceGroup::createInstanceGroup(token);
00119                                         if (ig)
00120                                         {
00121                                                 // add it to the map.
00122                                                 string  tokId= token;
00123                                                 strupr(tokId);
00124                                                 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00125                                         }
00126                                         else
00127                                         {
00128                                                 nlwarning ("CLandscapeIGManager::initIG() Can't load instance group '%s' in '%s'", token, igFile.c_str());
00129                                         }
00130                                 }
00131                         }
00132                 }
00133                 file.close();
00134         }
00135         else
00136         {
00137                 nlwarning ("Couldn't load '%s'", igFile.c_str());
00138         }
00139 }
00140 // ***************************************************************************
00141 UInstanceGroup *CLandscapeIGManager::loadZoneIG(const std::string &name)
00142 {
00143         NL3D_MEM_LANDSCAPE_IG
00144         if(name=="") 
00145                 return NULL;
00146 
00147         // try to find this InstanceGroup.
00148         ItZoneInstanceGroupMap  it;
00149         it= _ZoneInstanceGroupMap.find( translateName(name) );
00150 
00151         // if found.
00152         if( it!= _ZoneInstanceGroupMap.end() )
00153         {
00154                 // if not already added to the scene.
00155                 if( !it->second.AddedToScene )
00156                 {
00157                         // add to the scene.
00158                         if (it->second.Ig != NULL)
00159                         {
00160                                 it->second.Ig->addToScene(*_Scene);
00161                                 it->second.AddedToScene= true;
00162                         }
00163                 }
00164                 return it->second.Ig;
00165         }
00166         else
00167         {
00168                 return NULL;
00169         }
00170 }
00171 // ***************************************************************************
00172 void    CLandscapeIGManager::loadArrayZoneIG(const std::vector<std::string> &names, std::vector<UInstanceGroup *> *dest /*= NULL*/)
00173 {
00174         NL3D_MEM_LANDSCAPE_IG
00175         if (dest)
00176         {
00177                 dest->clear();
00178                 dest->reserve(names.size());
00179         }
00180         for(uint i=0; i<names.size(); i++)
00181         {
00182                 UInstanceGroup *ig = loadZoneIG(names[i]);
00183                 if (dest && ig)
00184                 {
00185                         dest->push_back(ig);
00186                 }
00187         }
00188 }
00189 
00190 // ***************************************************************************
00191 void    CLandscapeIGManager::unloadArrayZoneIG(const std::vector<std::string> &names)
00192 {
00193         NL3D_MEM_LANDSCAPE_IG
00194         for(uint i=0; i<names.size(); i++)
00195         {
00196                 unloadZoneIG(names[i]);
00197         }
00198 }
00199 
00200 // ***************************************************************************
00201 void    CLandscapeIGManager::unloadZoneIG(const std::string &name)
00202 {
00203         NL3D_MEM_LANDSCAPE_IG
00204         if(name=="")
00205                 return;
00206 
00207         // try to find this InstanceGroup.
00208         ItZoneInstanceGroupMap  it;
00209         it= _ZoneInstanceGroupMap.find( translateName(name) );
00210 
00211         // if found.
00212         if( it!= _ZoneInstanceGroupMap.end() )
00213         {
00214                 // if really added to the scene.
00215                 if( it->second.AddedToScene )
00216                 {
00217                         // remove from the scene.
00218                         it->second.Ig->removeFromScene(*_Scene);
00219                         it->second.AddedToScene= false;
00220                 }
00221         }
00222 }
00223 
00224 // ***************************************************************************
00225 bool    CLandscapeIGManager::isIGAddedToScene(const std::string &name) const
00226 {
00227         NL3D_MEM_LANDSCAPE_IG
00228         if(name=="")
00229                 return false;
00230 
00231         // try to find this InstanceGroup.
00232         ConstItZoneInstanceGroupMap     it;
00233         it= _ZoneInstanceGroupMap.find( translateName(name) );
00234 
00235         // if found.
00236         if( it!= _ZoneInstanceGroupMap.end() )
00237                 return  it->second.AddedToScene;
00238         else
00239                 return false;
00240 }
00241 
00242 // ***************************************************************************
00243 UInstanceGroup  *CLandscapeIGManager::getIG(const std::string &name) const
00244 {
00245         NL3D_MEM_LANDSCAPE_IG
00246         if(name=="")
00247                 return NULL;
00248 
00249         // try to find this InstanceGroup.
00250         ConstItZoneInstanceGroupMap     it;
00251         it= _ZoneInstanceGroupMap.find( translateName(name) );
00252 
00253         // if found.
00254         if( it!= _ZoneInstanceGroupMap.end() )
00255                 return it->second.Ig;
00256         else
00257                 return NULL;
00258 }
00259 
00260 
00261 // ***************************************************************************
00262 std::string             CLandscapeIGManager::translateName(const std::string &name) const
00263 {
00264         NL3D_MEM_LANDSCAPE_IG
00265         std::string             ret;
00266         ret= name + ".ig";
00267         strupr(ret);
00268 
00269         return ret;
00270 }
00271 
00272 
00273 // ***************************************************************************
00274 void    CLandscapeIGManager::reset()
00275 {
00276         NL3D_MEM_LANDSCAPE_IG
00277         while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00278         {
00279                 string  name= _ZoneInstanceGroupMap.begin()->first;
00280                 // first remove from scene
00281                 unloadZoneIG( name.substr(0, name.find('.')) );
00282 
00283                 // then delete this entry.
00284                 _ZoneInstanceGroupMap.begin()->second.release();
00285                 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00286         }
00287 
00288         _Scene=NULL;
00289 }
00290 
00291 
00292 // ***************************************************************************
00293 void    CLandscapeIGManager::reloadAllIgs()
00294 {
00295         NL3D_MEM_LANDSCAPE_IG
00296         vector<std::string>             bkupIgFileNameList;
00297         vector<bool>                    bkupIgAddedToScene;
00298 
00299         // First, erase all igs.
00300         while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00301         {
00302                 string  name= _ZoneInstanceGroupMap.begin()->first;
00303 
00304                 // bkup the state of this ig.
00305                 bkupIgFileNameList.push_back(_ZoneInstanceGroupMap.begin()->second.FileName);
00306                 bkupIgAddedToScene.push_back(_ZoneInstanceGroupMap.begin()->second.AddedToScene);
00307 
00308                 // first remove from scene
00309                 unloadZoneIG( name.substr(0, name.find('.')) );
00310 
00311                 // then delete this entry.
00312                 _ZoneInstanceGroupMap.begin()->second.release();
00313                 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00314         }
00315 
00316         // Then reload all Igs.
00317         for(uint i=0; i<bkupIgFileNameList.size(); i++)
00318         {
00319                 const   char    *token= bkupIgFileNameList[i].c_str();
00320                 UInstanceGroup  *ig = UInstanceGroup::createInstanceGroup(token);
00321                 // add it to the map.
00322                 string  tokId= token;
00323                 strupr(tokId);
00324                 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00325 
00326                 // If was addedToScene before, re-add to scene now.
00327                 if(bkupIgAddedToScene[i])
00328                 {
00329                         loadZoneIG( tokId.substr(0, tokId.find('.')) );
00330                 }
00331         }
00332 }
00333 
00334 
00335 // ***************************************************************************
00336 void CLandscapeIGManager::getAllIG(std::vector<UInstanceGroup *> &dest) const
00337 {       
00338         NL3D_MEM_LANDSCAPE_IG
00339         dest.clear();
00340         dest.reserve(_ZoneInstanceGroupMap.size());
00341         // add the instances
00342         for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00343         {                       
00344                 dest.push_back(it->second.Ig);                  
00345         }
00346 }
00347 
00348 // ***************************************************************************
00349 void CLandscapeIGManager::getAllIGWithNames(std::vector<std::pair<UInstanceGroup *, std::string> > &dest) const
00350 {
00351         NL3D_MEM_LANDSCAPE_IG
00352         dest.clear();
00353         dest.reserve(_ZoneInstanceGroupMap.size());
00354         // add the instances
00355         for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00356         {                       
00357                 dest.push_back(std::make_pair(it->second.Ig, it->second.FileName));                     
00358         }
00359 }
00360 
00361 
00362 } // NL3D