AaronCameron.net
Not Left, nor right. Just correct.
Jimmy's World Games
Not a Member? - Login or Create an Account
Friday the 3rd of September 2010 @ 10:26am
[]
Front Page Journal Projects Your Profile About

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cResourceManager.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 #include "resourceManagement/cResourceManager.h"
00026 
00027 #include "n2l/vfs.h"
00028 
00029 #include "materials/cGLTexture.h"
00030 
00031 #include "gui/cGuiElement.h"
00032 #include "gui/cGuiFactory.h"
00033 
00034 /******************************************************************************/
00035 namespace n2l
00036 {
00037     cAutoPtr<const cVfsDirectoryInterface> cResourceManager::mData(0);
00038     cResourceManager::tResourceContainer cResourceManager::mResources;
00039     tUint cResourceManager::mAggLoadTime = 0;
00040     tBool cResourceManager::mTiming = false;
00041 
00042 
00043     /**************************************************************************/
00044     void cResourceManager::dataRoot(
00045         const cAutoPtr<const cVfsDirectoryInterface> &iDataRoot )
00046     {
00047         mData = iDataRoot;
00048     }
00049 
00050 
00051     /**************************************************************************/
00052     const cAutoPtr<const cVfsDirectoryInterface> &cResourceManager::dataRoot()
00053     {
00054         return mData;
00055     }
00056     
00057     /**************************************************************************/
00058     const cAutoPtr<const cGuiElement> &cResourceManager::getGuiElement(
00059         const tString &iName )
00060     {
00061         checkDataRoot();
00062         n2lDebug( "Loading Gui Element  \"", false );
00063         n2lDebug( iName, false );
00064         const tUint STime = n2lGetTicks();
00065         tBool countMyself = false;
00066         if (!mTiming) {
00067             mTiming = true;
00068             countMyself = true;
00069         }
00070         tResourceContainer::const_iterator rIT( mResources.find(iName) );
00071         if (rIT==mResources.end()) {
00072             n2lDebug( "\" from filesystem" );
00073             try {
00074                 mResources[iName] = new cResourceSheath<cGuiElement>(
00075                     cGuiFactory::current().load(*mData->mustFind(iName)) );
00076             }
00077             catch (const cException &iE) {
00078                 mTiming = false;
00079                 iE.rethrow();
00080             }
00081         } else n2lDebug( "\" from cache" );
00082         if (countMyself) {
00083             mAggLoadTime += n2lGetTicks()-STime;
00084             mTiming = false;
00085             countMyself = false;
00086         }
00087         return cAutoPtr<cResourceSheath<cGuiElement> >(
00088             mResources[iName])->resource();
00089     }
00090 
00091     /**************************************************************************/
00092     const cAutoPtr<const cVfsNodeInterface> &cResourceManager::getVfsNode(
00093         const tString &iName )
00094     {
00095         checkDataRoot();
00096         n2lDebug( "Loading VFS Node \"", false );
00097         n2lDebug( iName, false );
00098         const tUint STime = n2lGetTicks();
00099         tBool countMyself = false;
00100         if (!mTiming) {
00101             mTiming = true;
00102             countMyself = true;
00103         }
00104         tResourceContainer::const_iterator rIT( mResources.find(iName) );
00105         if (rIT==mResources.end()) {
00106             n2lDebug( "\" from filesystem" );
00107             try {
00108                 mResources[iName] =
00109                     new cResourceSheath<const cVfsNodeInterface>(
00110                     mData->mustFind(iName) );
00111             }
00112             catch (const cException &iE) {
00113                 mTiming = false;
00114                 iE.rethrow();
00115             }
00116         } else n2lDebug( "\" from cache" );
00117         if (countMyself) {
00118             mAggLoadTime += n2lGetTicks()-STime;
00119             mTiming = false;
00120             countMyself = false;
00121         }
00122         return cAutoPtr<cResourceSheath<const cVfsNodeInterface> >(
00123             mResources[iName])->resource();
00124     }
00125 
00126     /**************************************************************************/
00127     void cResourceManager::refreshTextures()
00128     {
00129         for (tResourceContainer::const_iterator it = mResources.begin();
00130             it!=mResources.end(); ++it)
00131         {
00132             tBool isTexture( true );
00133             try
00134             {
00135                 cAutoPtr<cResourceSheath<cGLTexture> >(it->second);
00136             }
00137             catch (...) 
00138             {
00139                 isTexture = false;
00140             }
00141             if (isTexture) {
00142                 n2lDebug( tString("Reloading texture: ") + it->first );
00143                 cAutoPtr<cResourceSheath<cGLTexture> >(
00144                     it->second)->resource()->refresh();
00145             }
00146         }
00147     }
00148 
00149     /**************************************************************************/
00150     const tUint cResourceManager::numReferences( const tString & iName )
00151     {
00152         const tResourceContainer::iterator ResourceIT( mResources.find(iName));
00153         if (ResourceIT==mResources.end()) return 0;
00154         return ResourceIT->second->count();
00155     }
00156 
00157     /**************************************************************************/
00158     const tString cResourceManager::getReferenceReport()
00159     {
00160         tString temp("cResourceManager References:\n");
00161         for (tResourceContainer::const_iterator it = mResources.begin();
00162             it!=mResources.end(); ++it)
00163         {
00164             temp += "\t";
00165             temp += asString(it->second->count());
00166             temp += "x\t";
00167             temp += it->first;
00168             temp += "\n";
00169         }
00170         temp += "/cResourceManager References";
00171         return temp;
00172     }
00173 
00174     /**************************************************************************/
00175     void cResourceManager::clearAllCache()
00176     {
00177         mResources.clear();
00178     }
00179 
00180 
00181     /**************************************************************************/
00182     void cResourceManager::clearCachedResource( const tString & iName )
00183     {
00184         n2lDebug( "Clearing cache of: \"",false );
00185         n2lDebug( iName, false );
00186         const tResourceContainer::iterator ResourceIT( mResources.find(iName));
00187         if (ResourceIT==mResources.end()) {
00188             n2lDebug( "\" - not in the cache" );
00189             return;
00190         }
00191         n2lDebug( "\"" );
00192         mResources.erase( ResourceIT );
00193     }
00194 
00195     /**************************************************************************/
00196     void cResourceManager::clearUnusedCache()
00197     {
00198         for (tResourceContainer::iterator it = mResources.begin();
00199             it!=mResources.end(); )
00200         {
00201             tResourceContainer::iterator nextIT = it;
00202             ++nextIT;   // In case we delete (there has to be a better way
00203                 // than this)
00204             if (it->second->count()==1) {
00205                 n2lDebug( "Clearing cache of: ",false );
00206                 n2lDebug( it->first );
00207                 mResources.erase(it);
00208             }
00209             it = nextIT;
00210         }
00211     }
00212 
00213     /**************************************************************************/
00214     void cResourceManager::checkDataRoot()
00215     {
00216         if (!mData)
00217             throw cBadDataUseException( "cResourceManager::checkDataRoot",
00218                 "No data root specified" );
00219     }
00220     
00221 } // namespace
00222 
©2010 Aaron Cameron
Benchmarks