photoCollection.cpp

00001 
00002 #include "main.h"
00003 #include "photo.h"
00004 #include "photoCollection.h"
00005 
00006 
00007 #define PHOTOCOLLECTION_MAX_NUM_PHOTOS  100
00008 
00009 
00010 
00011 PhotoCollection::PhotoCollection()
00012 : theOneToCallToCheckForPhotoDelete(NULL)
00013 {
00014         logger->log( "PhotoCollection ctor()" );
00015 }
00016 
00017 
00018 PhotoCollection::~PhotoCollection()
00019 {
00020         this->deleteAllPhotos();
00021         logger->log( "PhotoCollection dtor()" );
00022 }
00023 
00024 
00025 // loads a mesh from file, loads a texture from file, set up new photo
00026 ERR_TYPE PhotoCollection::addPhoto( stringc fileName, NxVec3 pos )
00027 {
00028         ERR_TYPE status = ERR_OK;
00029         vector3df scale(0.04f, 0.4f, 0.04f);
00030 
00031         // check for list out of bounce
00032         if ( photoList.getSize() >= PHOTOCOLLECTION_MAX_NUM_PHOTOS )
00033         {
00034                 stringc err( "addPhoto() MAX photo count reached: " );
00035                 err += photoList.getSize();
00036                 logger->log( err.c_str() );
00037                 return ERR_PHOTOCOLLECTION_MAX_NUM_PHOTOS_REACHED;
00038         }
00039 
00040         // create a photo
00041         Photo* tempPhoto = new Photo;
00042         // add to collection
00043         photoList.push_back( tempPhoto );
00044 
00045         // load mesh and texture, scale up
00046         status = tempPhoto->load( "../../media/cube_400x1x300.3ds", 
00047                                                           fileName.c_str(), 
00048                                                           scale );
00049 
00050         if ( ERR_OK != status )
00051         {
00052                 stringc err( "addPhoto() cannot load photo: " );
00053                 err += fileName;
00054                 logger->log( err.c_str() );
00055 
00056                 // undo photo create
00057                 this->deletePhoto( photoList.getLast() );
00058         }
00059         else
00060         {
00061                 // drop the new photo somewhere
00062                 tempPhoto->setPosition( pos );
00063         }
00064 
00065         return status;
00066 }
00067 
00068 
00069 // this function parses given directory for loadable images and then creates photo objects
00070 ERR_TYPE PhotoCollection::addPhotosFromDirectory( stringc dirPath )
00071 {
00072         f32 spawnIncrementX = 18.0f;
00073         f32 spawnIncrementY = 10.0f;
00074         f32 spawnIncrementZ = 12.0f;
00075         f32 spawnPosX = -3 * spawnIncrementX;
00076         f32 spawnPosY =  5 * spawnIncrementY;
00077         f32 spawnPosZ = -2 * spawnIncrementZ;
00078 
00079         // use the irrlicht fileSystem helper
00080         bool result = false;
00081         IFileSystem* fileSystem = device->getFileSystem();
00082 
00083         // set irrlicht working dir
00084         result = fileSystem->changeWorkingDirectoryTo( dirPath.c_str() );
00085         if ( false == result )
00086         {
00087                 stringc err( "addPhotosInDirectory() cannot change working directory to: " );
00088                 err += dirPath;
00089                 logger->log( err.c_str() );
00090                 return ERR_UNKWON;
00091         }
00092 
00093         // get irrlicht file enumerator
00094         IFileList* fileList = fileSystem->createFileList();
00095         if ( NULL == fileList )
00096         {
00097                 stringc err( "addPhotosInDirectory() cannot create fileList" );
00098                 logger->log( err.c_str() );
00099                 return ERR_UNKWON;
00100         }
00101 
00102         // this counts also directory defaults "." and ".." in first place
00103         s32 fileCount = fileList->getFileCount();
00104 
00105         for ( s32 i=2; i<fileCount; i++)                // exclude index 0 and 1
00106         {
00107                 // get filename of image
00108                 stringc fileName = fileList->getFullFileName( i );
00109                 if ( fileName.size() == 0 )
00110                 {
00111                         stringc err( "addPhotosInDirectory() cannot get fileName for index: " );
00112                         err += i;
00113                         logger->log( err.c_str() );
00114                 }
00115                 // do not try to load sub-directories
00116                 else if ( fileList->isDirectory( i ) )
00117                 {
00118                         stringc err( "addPhotosInDirectory() do not load sub-directory: " );
00119                         err += fileName;
00120                         logger->log( err.c_str() );                     
00121                 }
00122                 else
00123                 {
00124                         // drop the new photo on table
00125                         NxVec3 pos;
00126                         pos.x = spawnPosX;
00127                         pos.y = spawnPosY;
00128                         pos.z = spawnPosZ;
00129                         this->addPhoto( fileName, pos );
00130 
00131                         // update spawn position for next photo in directory
00132                         spawnPosX += spawnIncrementX;
00133 
00134                         if ( spawnPosX > spawnIncrementX*3 )
00135                         {
00136                                 spawnPosX = -3 * spawnIncrementX;
00137                                 spawnPosZ += spawnIncrementZ;
00138                         }
00139                         if ( spawnPosZ > spawnIncrementZ*2 )
00140                         {
00141                                 spawnPosX = -3 * spawnIncrementX;
00142                                 spawnPosY += spawnIncrementY;
00143                                 spawnPosZ = -2 * spawnIncrementZ;
00144                                 spawnIncrementX += spawnIncrementX/4;
00145                                 spawnIncrementZ += spawnIncrementZ/3;
00146                         }
00147                 }
00148         }
00149 
00150         // we create it, we destroy it
00151         fileList->drop();
00152 
00153         return ERR_OK;
00154 }
00155 
00156 
00157 // update all photos in our collection
00158 ERR_TYPE PhotoCollection::update( irr::u32 timeMS )
00159 {
00160         // iterate our list and call update()
00161         photoIterator_TYPE it = photoList.begin();
00162 
00163         for ( ; it != photoList.end(); ++it )
00164         {
00165                 // recalculate physx -> irrlicht transformation
00166                 (*it)->update( timeMS );
00167 
00168                 // auto-delete, if someone want to
00169                 if ( NULL != theOneToCallToCheckForPhotoDelete )
00170                 {
00171                         ERR_TYPE result = theOneToCallToCheckForPhotoDelete( (*it) );
00172                         if ( ERR_YES == result )
00173                         {
00174                                 it = this->deletePhoto( it );
00175                                 if ( it == photoList.end() )
00176                                 {
00177                                         // ouch, we hit last photo in list. exit loop
00178                                         break;
00179                                 }
00180                         }
00181                 }
00182         }
00183         return ERR_OK;
00184 }
00185 
00186 
00187 // attach unknown function which checks whether a photo should be deleted
00188 ERR_TYPE PhotoCollection::registerCheckForDeleteCallback( photoCollectionCheckForDeleteCallback_TYPE anyCallBackFunction )
00189 {
00190         this->theOneToCallToCheckForPhotoDelete = anyCallBackFunction;
00191         return ERR_OK;
00192 }
00193 
00194 
00195 // setup photos' position nicely
00196 ERR_TYPE PhotoCollection::resetPhotos()
00197 {
00198         f32 spawnIncrementX = 18.0f;
00199         f32 spawnIncrementY = 10.0f;
00200         f32 spawnIncrementZ = 12.0f;
00201         f32 spawnPosX = -3 * spawnIncrementX;
00202         f32 spawnPosY =  5 * spawnIncrementY;
00203         f32 spawnPosZ = -2 * spawnIncrementZ;
00204 
00205         // iterate our list and call setPosition()
00206         irr::core::list<Photo*>::Iterator it = photoList.begin();
00207 
00208         for ( ; it != photoList.end(); it++ )
00209         {
00210                 (*it)->setPosition( NxVec3(spawnPosX, spawnPosY, spawnPosZ) );
00211                 
00212                 // update spawn position for next photo
00213                 spawnPosX += spawnIncrementX;
00214 
00215                 if ( spawnPosX > spawnIncrementX*3 )
00216                 {
00217                         spawnPosX = -3 * spawnIncrementX;
00218                         spawnPosZ += spawnIncrementZ;
00219                 }
00220                 if ( spawnPosZ > spawnIncrementZ*2 )
00221                 {
00222                         spawnPosX = -3 * spawnIncrementX;
00223                         spawnPosY += spawnIncrementY;
00224                         spawnPosZ = -2 * spawnIncrementZ;
00225                         spawnIncrementX += spawnIncrementX/4;
00226                         spawnIncrementZ += spawnIncrementZ/3;
00227                 }
00228         }
00229         return ERR_OK;
00230 }
00231 
00232 
00233 
00234 
00235 
00236 // *** private stuff: ***
00237 
00238 
00239 // dispose a photos from our collection
00240 ERR_TYPE PhotoCollection::deletePhoto( Photo* photo )
00241 {
00242         // search for photo in list and delete it
00243         photoIterator_TYPE it = photoList.begin();
00244 
00245         for ( ; it != photoList.end(); it++ )
00246         {
00247                 if ( *it == photo )
00248                 {
00249                         this->deletePhoto( it );
00250                         return ERR_OK;
00251                 }
00252         }
00253         return ERR_PHOTOCOLLECTION_PHOTO_NOT_FOUND;
00254 }
00255 
00256 
00257 // dispose a photos from our collection
00258 photoIterator_TYPE PhotoCollection::deletePhoto( photoIterator_TYPE it )
00259 {
00260         photoIterator_TYPE result( it );
00261 
00262         if ( ! photoList.empty() )
00263         {
00264                 delete (*it);
00265                 // this call returns iterator to next photo or NULL if list is empty
00266                 result = photoList.erase( it );
00267                 
00268                 stringc text( "Photo deleted" );
00269                 logger->log( text.c_str() );
00270         }
00271         else
00272         {
00273                 stringc text( "deletePhoto() someone tries to delete from empty list" );
00274                 logger->log( text.c_str() );
00275         }
00276 
00277         return result;
00278 }
00279 
00280 
00281 // dispose all photos in our list
00282 ERR_TYPE PhotoCollection::deleteAllPhotos()
00283 {
00284         while ( ! photoList.empty() )
00285         {
00286                 photoIterator_TYPE it = photoList.getLast();
00287                 delete (*it);
00288                 photoList.erase( it );
00289         }
00290 
00291         return ERR_OK;
00292 }

Generated on Sun Dec 2 03:10:23 2007 for TableTop by  doxygen 1.5.4