Aller à la documentation de ce fichier.00001
00002
00003
00004
00005
00006
00007 #ifndef ASSETMANAGER_HPP
00008 #define ASSETMANAGER_HPP
00009
00010 #include <list>
00011 #include <map>
00012 #include <iostream>
00013 #include <string>
00014
00015 namespace bib {
00016
00017 template <class T>
00018 class AssetManager
00019 {
00020 public:
00021 AssetManager() : resource_directories_(), assets_()
00022 {
00023 }
00024 virtual ~AssetManager()
00025 {
00026 }
00027 public:
00028
00033 void add_resource_directory( const std::string& directory )
00034 {
00035
00036 for( std::list<std::string>::const_iterator it = resource_directories_.begin();
00037 it != resource_directories_.end();
00038 ++it )
00039 {
00040
00041 if( directory == (*it) )
00042 return;
00043 }
00044
00045
00046 resource_directories_.push_back( directory );
00047 }
00048
00053 void remove_resource_directory( const std::string& directory )
00054 {
00055 for( std::list<std::string>::iterator it = resource_directories_.begin();
00056 it != resource_directories_.end(); )
00057 {
00058
00059 if( directory == (*it) )
00060 it = resource_directories_.erase( it );
00061 else
00062 ++it;
00063 }
00064 }
00065
00070 void load_asset( int ID, const std::string& filename )
00071 {
00072 bool succes = false;
00073 T* asset ;
00074
00075
00076 for( std::list< std::string >::iterator it = resource_directories_.begin();
00077 it != resource_directories_.end();
00078 ++it )
00079 {
00080 asset = creer_ressource(((*it) + filename ), &succes);
00081 if( succes )
00082 {
00083 assets_.insert( std::pair<int,T*>(ID, asset) );
00084 std::cout << "DEBUG_MESSAGE: " << filename << " loading asset.\n";
00085 return;
00086 }
00087 }
00088 std::cout << "GAME_ERROR: asset was not found. It is filled with an empty asset.\n";
00089 assets_.insert( std::pair<int,T*>(ID, asset) );
00090 }
00091
00097 T& get_asset( const int ID )
00098 {
00099 return *assets_[ID];
00100 }
00101
00107 bool is_loaded( const int ID )
00108 {
00109 return assets_.find(ID) != assets_.end();
00110 }
00111
00116 void delete_asset( const T& asset )
00117 {
00118 typename std::map<int, T>::iterator it = assets_.begin();
00119 for( it; it != assets_.end(); ++it)
00120 if( &asset == &it->second )
00121 assets_.erase( it );
00122 }
00123
00128 void delete_asset( const int id )
00129 {
00130 assets_.erase( id );
00131 }
00132
00136 std::pair <typename std::map<int, T>::iterator, typename std::map<int, T>::iterator> get_iterator()
00137 {
00138 return std::pair <typename std::map<int, T>::iterator, typename std::map<int, T>::iterator>
00139 (assets_.begin(),assets_.end());
00140 }
00141
00142 protected:
00143
00144 virtual T* creer_ressource(const std::string& path, bool *succes) =0;
00145
00146 std::list< std::string > resource_directories_;
00147
00148 std::map< int, T* > assets_;
00149 };
00150
00151 }
00152
00153 #endif // ASSETMANAGER_HPP