00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <map>
00024
00025
00026
00027
00028
00029
00030
00031 #include "util/log/logger.h"
00032 #include "util/resource/resourcemanager.h"
00033 #include "util/resource/resource.h"
00034
00035 #include "soundclipmanager.h"
00036
00037 namespace FIFE {
00038 static Logger _log(LM_RESMGR);
00039
00040 SoundClipManager::~SoundClipManager() {
00041
00042 }
00043
00044 size_t SoundClipManager::getMemoryUsed() const {
00045 size_t totalSize = 0;
00046
00047 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
00048 itend = m_sclipHandleMap.end();
00049
00050 for ( ; it != itend; ++it) {
00051 totalSize += it->second->getSize();
00052 }
00053
00054 return totalSize;
00055 }
00056
00057 size_t SoundClipManager::getTotalResourcesCreated() const {
00058 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
00059 itend = m_sclipHandleMap.end();
00060 size_t count = 0;
00061
00062 for ( ; it != itend; ++it) {
00063 if ( it->second->getState() == IResource::RES_NOT_LOADED ) {
00064 count++;
00065 }
00066 }
00067
00068 return count;
00069 }
00070
00071 size_t SoundClipManager::getTotalResourcesLoaded() const {
00072 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
00073 itend = m_sclipHandleMap.end();
00074 size_t count = 0;
00075
00076 for ( ; it != itend; ++it) {
00077 if ( it->second->getState() == IResource::RES_LOADED ) {
00078 count++;
00079 }
00080 }
00081
00082 return count;
00083 }
00084
00085 size_t SoundClipManager::getTotalResources() const {
00086 return m_sclipHandleMap.size();
00087 }
00088
00089 SoundClipPtr SoundClipManager::create(IResourceLoader* loader){
00090 SoundClip* ptr = new SoundClip(loader);
00091 return add(ptr);
00092 }
00093
00094 SoundClipPtr SoundClipManager::create(const std::string& name, IResourceLoader* loader){
00095 if (exists(name)) {
00096 FL_WARN(_log, LMsg("SoundClipManager::create(std::string, IResourceLoader* loader) - ") << "Resource name " << name << " was previously created. Returning original SoundClip...");
00097 return get(name);
00098 }
00099
00100 SoundClip* ptr = new SoundClip(name, loader);
00101 return add(ptr);
00102 }
00103
00104 SoundClipPtr SoundClipManager::load(const std::string& name, IResourceLoader* loader) {
00105 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00106
00107 if (nit != m_sclipNameMap.end()) {
00108 if ( nit->second->getState() == IResource::RES_NOT_LOADED ) {
00109 nit->second->load();
00110 }
00111
00112 return nit->second;
00113 }
00114
00115
00116 SoundClipPtr ptr = create(name, loader);
00117 ptr->load();
00118
00119 if (ptr->getState() == IResource::RES_NOT_LOADED){
00120 FL_WARN(_log, LMsg("SoundClipManager::load(std::string) - ") << "Resource name " << name << " was not found and could not be loaded.");
00121 remove(name);
00122 }
00123
00124 return ptr;
00125 }
00126
00127 SoundClipPtr SoundClipManager::add(SoundClip* res) {
00128 assert(res);
00129 assert(!(exists(res->getHandle()) || exists(res->getName())));
00130
00131 SoundClipPtr resptr(res);
00132
00133 std::pair<SoundClipHandleMapIterator, bool> returnValue;
00134 returnValue = m_sclipHandleMap.insert ( SoundClipHandleMapPair(res->getHandle(), resptr));
00135
00136 if (returnValue.second) {
00137 m_sclipNameMap.insert ( SoundClipNameMapPair(returnValue.first->second->getName(), returnValue.first->second) );
00138 }
00139 else {
00140 FL_WARN(_log, LMsg("SoundClipManager::add(IResource*) - ") << "Resource " << res->getName() << " already exists.... ignoring.");
00141 }
00142
00143 return returnValue.first->second;
00144 }
00145
00146 bool SoundClipManager::exists(const std::string& name) {
00147 SoundClipNameMapIterator it = m_sclipNameMap.find(name);
00148 if (it != m_sclipNameMap.end()) {
00149 return true;
00150 }
00151
00152 return false;
00153 }
00154
00155 bool SoundClipManager::exists(ResourceHandle handle) {
00156 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
00157 if (it != m_sclipHandleMap.end()) {
00158 return true;
00159 }
00160
00161 return false;
00162 }
00163
00164 void SoundClipManager::reload(const std::string& name) {
00165 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00166
00167 if (nit != m_sclipNameMap.end()) {
00168 if ( nit->second->getState() == IResource::RES_LOADED) {
00169 nit->second->free();
00170 }
00171 nit->second->load();
00172 return;
00173 }
00174
00175 FL_WARN(_log, LMsg("SoundClipManager::reload(std::string) - ") << "Resource name " << name << " not found.");
00176 }
00177
00178 void SoundClipManager::reload(ResourceHandle handle) {
00179 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
00180
00181 if ( it != m_sclipHandleMap.end()) {
00182 if ( it->second->getState() == IResource::RES_LOADED) {
00183 it->second->free();
00184 }
00185 it->second->load();
00186 return;
00187 }
00188
00189 FL_WARN(_log, LMsg("SoundClipManager::reload(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
00190
00191 }
00192
00193 void SoundClipManager::reloadAll() {
00194 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
00195 itend = m_sclipHandleMap.end();
00196
00197 for ( ; it != itend; ++it) {
00198 if ( it->second->getState() == IResource::RES_LOADED) {
00199 it->second->free();
00200 }
00201 it->second->load();
00202 }
00203 }
00204
00205 void SoundClipManager::loadUnreferenced() {
00206 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
00207 itend = m_sclipHandleMap.end();
00208
00209 int32_t count = 0;
00210 for ( ; it != itend; ++it) {
00211 if (it->second.useCount() == 2 && it->second->getState() != IResource::RES_LOADED){
00212 it->second->load();
00213 count++;
00214 }
00215 }
00216 FL_DBG(_log, LMsg("SoundClipManager::loadUnreferenced() - ") << "Loaded " << count << " unreferenced resources.");
00217 }
00218
00219 void SoundClipManager::free(const std::string& name) {
00220 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00221
00222 if (nit != m_sclipNameMap.end()) {
00223 if ( nit->second->getState() == IResource::RES_LOADED) {
00224 nit->second->free();
00225 }
00226 return;
00227 }
00228
00229 FL_WARN(_log, LMsg("SoundClipManager::free(std::string) - ") << "Resource name " << name << " not found.");
00230 }
00231
00232 void SoundClipManager::free(ResourceHandle handle) {
00233 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
00234 if (it != m_sclipHandleMap.end()) {
00235 if ( it->second->getState() == IResource::RES_LOADED) {
00236 it->second->free();
00237 }
00238 return;
00239 }
00240
00241 FL_WARN(_log, LMsg("SoundClipManager::free(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
00242 }
00243
00244 void SoundClipManager::freeAll() {
00245 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
00246 itend = m_sclipHandleMap.end();
00247
00248 int32_t count = 0;
00249
00250 for ( ; it != itend; ++it) {
00251 if ( it->second->getState() == IResource::RES_LOADED) {
00252 it->second->free();
00253 count++;
00254 }
00255 }
00256
00257 FL_DBG(_log, LMsg("SoundClipManager::freeAll() - ") << "Freed all " << count << " resources.");
00258 }
00259
00260 void SoundClipManager::freeUnreferenced() {
00261 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
00262 itend = m_sclipHandleMap.end();
00263
00264 int32_t count = 0;
00265 for ( ; it != itend; ++it) {
00266 if (it->second.useCount() == 2 && it->second->getState() == IResource::RES_LOADED ){
00267 it->second->free();
00268 count++;
00269 }
00270 }
00271
00272 FL_DBG(_log, LMsg("SoundClipManager::freeUnreferenced() - ") << "Freed " << count << " unreferenced resources.");
00273 }
00274
00275 void SoundClipManager::remove(SoundClipPtr& resource) {
00276 SoundClipHandleMapIterator it = m_sclipHandleMap.find(resource->getHandle());
00277 SoundClipNameMapIterator nit = m_sclipNameMap.find(resource->getName());
00278
00279 if (it != m_sclipHandleMap.end()) {
00280 m_sclipHandleMap.erase(it);
00281
00282 if (nit != m_sclipNameMap.end()) {
00283 m_sclipNameMap.erase(nit);
00284 return;
00285 }
00286 assert(false);
00287 }
00288
00289 FL_WARN(_log, LMsg("SoundClipManager::remove(ResourcePtr&) - ") << "Resource " << resource->getName() << " was not found.");
00290 }
00291
00292 void SoundClipManager::remove(const std::string& name) {
00293 std::size_t handle;
00294
00295 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00296 if (nit != m_sclipNameMap.end()) {
00297 handle = nit->second->getHandle();
00298 m_sclipNameMap.erase(nit);
00299 }
00300 else {
00301 FL_WARN(_log, LMsg("SoundClipManager::remove(std::string) - ") << "Resource " << name << " was not found.");
00302 return;
00303 }
00304
00305 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
00306 if ( it != m_sclipHandleMap.end()) {
00307 m_sclipHandleMap.erase(it);
00308 return;
00309 }
00310
00311 assert(false);
00312 }
00313
00314 void SoundClipManager::remove(ResourceHandle handle) {
00315 std::string name;
00316
00317 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
00318
00319 if (it != m_sclipHandleMap.end()) {
00320 name = it->second->getName();
00321 m_sclipHandleMap.erase(it);
00322 }
00323 else {
00324 FL_WARN(_log, LMsg("SoundClipManager::remove(ResourceHandle) - ") << "Resource handle " << handle << " was not found.");
00325 return;
00326 }
00327
00328 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00329 if ( nit != m_sclipNameMap.end() ) {
00330 m_sclipNameMap.erase(nit);
00331 return;
00332 }
00333
00334 assert(false);
00335 }
00336
00337 void SoundClipManager::removeAll() {
00338
00339 assert (m_sclipHandleMap.size() == m_sclipNameMap.size());
00340
00341 size_t count = m_sclipHandleMap.size();
00342
00343 m_sclipHandleMap.clear();
00344 m_sclipNameMap.clear();
00345
00346 FL_DBG(_log, LMsg("SoundClipManager::removeAll() - ") << "Removed all " << count << " resources.");
00347 }
00348
00349 void SoundClipManager::removeUnreferenced() {
00350 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
00351 itend = m_sclipHandleMap.end();
00352
00353 int32_t count = 0;
00354 for ( ; it != itend; ++it) {
00355 if ( it->second.useCount() == 2) {
00356 remove(it->second->getHandle());
00357 count++;
00358 }
00359 }
00360
00361 FL_DBG(_log, LMsg("SoundClipManager::removeUnreferenced() - ") << "Removed " << count << " unreferenced resources.");
00362 }
00363
00364 SoundClipPtr SoundClipManager::get(const std::string& name) {
00365 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00366
00367 if (nit != m_sclipNameMap.end()) {
00368 if (nit->second->getState() != IResource::RES_LOADED){
00369
00370 nit->second->load();
00371 }
00372 return nit->second;
00373 }
00374
00375
00376 SoundClipPtr ptr = load(name);
00377 return ptr;
00378 }
00379
00380 SoundClipPtr SoundClipManager::get(ResourceHandle handle) {
00381 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
00382 if (it != m_sclipHandleMap.end()) {
00383 if (it->second->getState() != IResource::RES_LOADED){
00384
00385 it->second->load();
00386 }
00387 return it->second;
00388 }
00389
00390 FL_WARN(_log, LMsg("SoundClipManager::get(ResourceHandle) - ") << "Resource handle " << handle << " is undefined.");
00391
00392 return SoundClipPtr();
00393 }
00394
00395 ResourceHandle SoundClipManager::getResourceHandle(const std::string& name) {
00396 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
00397 if (nit != m_sclipNameMap.end()) {
00398 return nit->second->getHandle();
00399 }
00400
00401 FL_WARN(_log, LMsg("SoundClipManager::getResourceHandle(std::string) - ") << "Resource " << name << " is undefined.");
00402
00403 return 0;
00404 }
00405
00406 }