ResourceManager.cpp
Go to the documentation of this file.00001 //*** ResourceManager.cpp *** 00002 00003 #include "ResourceManager.h" 00004 #include "Debug.h" 00005 #include "BitmapStrip.h" 00006 #include "Bitmap.h" 00007 #include "Bitmap_RLE8.h" 00008 #include "Bitmap_16Bit.h" 00009 #include "Bitmap_16BitAlpha.h" 00010 #include "Bitmap_16BitAlphaCrop.h" 00011 #include "Bitmap_Alpha.h" 00012 #include "Image.h" 00013 #include "Asset.h" 00014 #include "HashTableIterator.h" 00015 #include "Font.h" 00016 #include "Sound.h" 00017 #include "Filename.h" 00018 00019 00020 //*** Constructor *** 00021 00022 ResourceManager::ResourceManager() 00023 { 00024 } 00025 00026 00027 //*** Destructor *** 00028 00029 ResourceManager::~ResourceManager() 00030 { 00031 // Delete all bitmap strips 00032 HashTableIterator<HashTableKey_Pointer,BitmapStripEntry*> itBS(bitmapStrips_); 00033 while(itBS.IsValid()) 00034 { 00035 BitmapStripEntry* bitmapStripEntry=itBS.GetCurrent(); 00036 delete bitmapStripEntry->bitmapStrip; 00037 delete bitmapStripEntry; 00038 itBS.MoveNext(); 00039 } 00040 00041 00042 // Delete all fonts 00043 HashTableIterator<HashTableKey_Pointer,FontEntry*> itF(fonts_); 00044 while(itF.IsValid()) 00045 { 00046 FontEntry* fontEntry=itF.GetCurrent(); 00047 delete fontEntry->font; 00048 delete fontEntry; 00049 itF.MoveNext(); 00050 } 00051 00052 // Delete all sounds 00053 HashTableIterator<HashTableKey_Pointer,SoundEntry*> itS(sounds_); 00054 while(itS.IsValid()) 00055 { 00056 SoundEntry* soundEntry=itS.GetCurrent(); 00057 delete soundEntry->sound; 00058 delete soundEntry; 00059 itS.MoveNext(); 00060 } 00061 } 00062 00063 00064 //*** GetBitmapStripEntry *** 00065 00066 ResourceManager::BitmapStripEntry* ResourceManager::GetBitmapStripEntry(const BitmapStrip* bitmapStrip) 00067 { 00068 HashTableIterator<HashTableKey_Pointer,BitmapStripEntry*> it(bitmapStrips_); 00069 if (it.Find(HashTableKey_Pointer(bitmapStrip))) 00070 { 00071 return it.GetCurrent(); 00072 } 00073 00074 BitmapStripEntry* entry=new BitmapStripEntry(); 00075 entry->filename=0; 00076 entry->bitmapStrip=bitmapStrip; 00077 entry->referenceCount=0; 00078 bitmapStrips_.Insert(HashTableKey_Pointer(bitmapStrip),entry); 00079 return entry; 00080 } 00081 00082 00083 00084 //*** GetFontEntry *** 00085 00086 ResourceManager::FontEntry* ResourceManager::GetFontEntry(const Font* font) 00087 { 00088 HashTableIterator<HashTableKey_Pointer,FontEntry*> it(fonts_); 00089 if (it.Find(HashTableKey_Pointer(font))) 00090 { 00091 return it.GetCurrent(); 00092 } 00093 00094 FontEntry* entry=new FontEntry(); 00095 entry->filename=0; 00096 entry->font=font; 00097 entry->referenceCount=0; 00098 fonts_.Insert(HashTableKey_Pointer(font),entry); 00099 return entry; 00100 } 00101 00102 00103 //*** GetSoundEntry *** 00104 00105 ResourceManager::SoundEntry* ResourceManager::GetSoundEntry(const Sound* sound) 00106 { 00107 HashTableIterator<HashTableKey_Pointer,SoundEntry*> it(sounds_); 00108 if (it.Find(HashTableKey_Pointer(sound))) 00109 { 00110 return it.GetCurrent(); 00111 } 00112 00113 SoundEntry* entry=new SoundEntry(); 00114 entry->filename=0; 00115 entry->sound=sound; 00116 entry->referenceCount=0; 00117 sounds_.Insert(HashTableKey_Pointer(sound),entry); 00118 return entry; 00119 } 00120 00121 00122 //*** IncreaseReferenceCount *** 00123 00124 void ResourceManager::IncreaseReferenceCount(const BitmapStrip* bitmapStrip) 00125 { 00126 BitmapStripEntry* entry=GetBitmapStripEntry(bitmapStrip); 00127 entry->referenceCount++; 00128 } 00129 00130 00131 //*** DecreaseReferenceCount *** 00132 00133 void ResourceManager::DecreaseReferenceCount(const BitmapStrip* bitmapStrip) 00134 { 00135 BitmapStripEntry* entry=GetBitmapStripEntry(bitmapStrip); 00136 Assert(entry->referenceCount>=1,"Tried to decrease reference count when already 0"); 00137 if (entry->referenceCount<=0) 00138 { 00139 return; 00140 } 00141 00142 entry->referenceCount--; 00143 00144 if (entry->referenceCount==0) 00145 { 00146 bitmapStrips_.Remove(HashTableKey_Pointer(bitmapStrip)); 00147 delete entry->bitmapStrip; 00148 delete entry; 00149 } 00150 } 00151 00152 00153 //*** IncreaseReferenceCount *** 00154 00155 void ResourceManager::IncreaseReferenceCount(const Font* font) 00156 { 00157 FontEntry* entry=GetFontEntry(font); 00158 entry->referenceCount++; 00159 } 00160 00161 00162 //*** DecreaseReferenceCount *** 00163 00164 void ResourceManager::DecreaseReferenceCount(const Font* font) 00165 { 00166 FontEntry* entry=GetFontEntry(font); 00167 Assert(entry->referenceCount>=1,"Tried to decrease reference count when already 0"); 00168 if (entry->referenceCount<=0) 00169 { 00170 return; 00171 } 00172 00173 entry->referenceCount--; 00174 00175 if (entry->referenceCount==0) 00176 { 00177 fonts_.Remove(HashTableKey_Pointer(font)); 00178 delete entry->font; 00179 delete entry; 00180 } 00181 } 00182 00183 00184 //*** IncreaseReferenceCount *** 00185 00186 void ResourceManager::IncreaseReferenceCount(const Sound* sound) 00187 { 00188 SoundEntry* entry=GetSoundEntry(sound); 00189 entry->referenceCount++; 00190 } 00191 00192 00193 //*** DecreaseReferenceCount *** 00194 00195 void ResourceManager::DecreaseReferenceCount(const Sound* sound) 00196 { 00197 SoundEntry* entry=GetSoundEntry(sound); 00198 Assert(entry->referenceCount>=1,"Tried to decrease reference count when already 0"); 00199 if (entry->referenceCount<=0) 00200 { 00201 return; 00202 } 00203 00204 entry->referenceCount--; 00205 00206 if (entry->referenceCount==0) 00207 { 00208 sounds_.Remove(HashTableKey_Pointer(sound)); 00209 delete entry->sound; 00210 delete entry; 00211 } 00212 } 00213 00214 00215 00216 //*** GetBitmapStrip *** 00217 00218 const BitmapStrip* ResourceManager::GetBitmapStrip(StringId filename, int celCount) 00219 { 00220 HashTableIterator<HashTableKey_Pointer,BitmapStripEntry*> it(bitmapStrips_); 00221 while(it.IsValid()) 00222 { 00223 BitmapStripEntry* entry=it.GetCurrent(); 00224 if (entry->filename==filename && entry->bitmapStrip) 00225 { 00226 return entry->bitmapStrip; 00227 } 00228 it.MoveNext(); 00229 } 00230 00231 const BitmapStrip* bitmapStrip=0; 00232 int l=StrLen(filename.GetString()); 00233 00234 // If new style .PIX file 00235 if (l>3 && ToLower(filename.GetString()[l-3])=='p' && ToLower(filename.GetString()[l-2])=='i' && ToLower(filename.GetString()[l-1])=='x') 00236 { 00237 bitmapStrip=new BitmapStrip(filename); 00238 } 00239 00240 // If old style .RLE file 00241 else if (l>3 && ToLower(filename.GetString()[l-3])=='r' && ToLower(filename.GetString()[l-2])=='l' && ToLower(filename.GetString()[l-1])=='e') 00242 { 00243 bitmapStrip=new BitmapStrip(filename); 00244 } 00245 00246 // If old style .ABM file 00247 else if (l>3 && ToLower(filename.GetString()[l-3])=='a' && ToLower(filename.GetString()[l-2])=='b' && ToLower(filename.GetString()[l-1])=='m') 00248 { 00249 bitmapStrip=new BitmapStrip(filename); 00250 } 00251 00252 // If old style .BM file 00253 else if (l>2 && ToLower(filename.GetString()[l-2])=='b' && ToLower(filename.GetString()[l-1])=='m') 00254 { 00255 bitmapStrip=new BitmapStrip(filename); 00256 } 00257 00258 // If old style .AM file 00259 else if (l>2 && ToLower(filename.GetString()[l-2])=='a' && ToLower(filename.GetString()[l-1])=='m') 00260 { 00261 bitmapStrip=new BitmapStrip(filename); 00262 } 00263 00264 // If arbitrary image format, .TGA, .GIF etc 00265 else 00266 { 00267 bitmapStrip=new BitmapStrip(Image(filename,celCount)); 00268 } 00269 00270 00271 BitmapStripEntry* entry=GetBitmapStripEntry(bitmapStrip); 00272 entry->filename=filename; 00273 00274 return entry->bitmapStrip; 00275 } 00276 00277 00278 //*** GetBitmapStrip *** 00279 00280 const BitmapStrip* ResourceManager::GetBitmapStrip(const Image& image) 00281 { 00282 const BitmapStrip* bitmapStrip=new BitmapStrip(image); 00283 00284 BitmapStripEntry* entry=GetBitmapStripEntry(bitmapStrip); 00285 00286 return entry->bitmapStrip; 00287 } 00288 00289 00290 //*** GetBitmapStrip *** 00291 00292 const BitmapStrip* ResourceManager::GetBitmapStrip(const Bitmap* bitmap) 00293 { 00294 const BitmapStrip* bitmapStrip=new BitmapStrip(bitmap); 00295 00296 BitmapStripEntry* entry=GetBitmapStripEntry(bitmapStrip); 00297 00298 return entry->bitmapStrip; 00299 } 00300 00301 00302 //*** GetFont *** 00303 00304 const Font* ResourceManager::GetFont(StringId filename) 00305 { 00306 HashTableIterator<HashTableKey_Pointer,FontEntry*> it(fonts_); 00307 while(it.IsValid()) 00308 { 00309 FontEntry* entry=it.GetCurrent(); 00310 if (entry->filename==filename && entry->font) 00311 { 00312 return entry->font; 00313 } 00314 it.MoveNext(); 00315 } 00316 00317 const Font* font=0; 00318 int l=StrLen(filename.GetString()); 00319 if (ToLower(filename.GetString()[l-3])=='f' && ToLower(filename.GetString()[l-2])=='n' && ToLower(filename.GetString()[l-1])=='t') 00320 { 00321 font=new Font(Asset(filename)); 00322 } 00323 else 00324 { 00325 font=new Font(Filename(filename)); 00326 } 00327 00328 00329 FontEntry* entry=GetFontEntry(font); 00330 entry->filename=filename; 00331 00332 return entry->font; 00333 } 00334 00335 00336 //*** GetSound *** 00337 00338 const Sound* ResourceManager::GetSound(StringId filename) 00339 { 00340 HashTableIterator<HashTableKey_Pointer,SoundEntry*> it(sounds_); 00341 while(it.IsValid()) 00342 { 00343 SoundEntry* entry=it.GetCurrent(); 00344 if (entry->filename==filename && entry->sound) 00345 { 00346 return entry->sound; 00347 } 00348 it.MoveNext(); 00349 } 00350 00351 const Sound* sound=new Sound(filename); 00352 00353 SoundEntry* entry=GetSoundEntry(sound); 00354 entry->filename=filename; 00355 00356 return entry->sound; 00357 } 00358 00359 00360 //*** LoadBitmapStrip *** 00361 00362 void ResourceManager::LoadBitmapStrip(const Filename& filename, int celCount) 00363 { 00364 const BitmapStrip* bitmapStrip=GetBitmapStrip(filename.GetStringId(),celCount); 00365 IncreaseReferenceCount(bitmapStrip); 00366 } 00367 00368 00369 //*** UnloadBitmapStrip *** 00370 00371 void ResourceManager::UnloadBitmapStrip(const Filename& filename) 00372 { 00373 const BitmapStrip* bitmapStrip=GetBitmapStrip(filename.GetStringId()); 00374 DecreaseReferenceCount(bitmapStrip); 00375 } 00376 00377 00378 //*** LoadFont *** 00379 00380 void ResourceManager::LoadFont(const Filename& filename) 00381 { 00382 const Font* font=GetFont(filename.GetStringId()); 00383 IncreaseReferenceCount(font); 00384 } 00385 00386 00387 //*** UnloadFont *** 00388 00389 void ResourceManager::UnloadFont(const Filename& filename) 00390 { 00391 const Font* font=GetFont(filename.GetStringId()); 00392 DecreaseReferenceCount(font); 00393 } 00394 00395 00396 //*** LoadSound *** 00397 00398 void ResourceManager::LoadSound(const Filename& filename) 00399 { 00400 const Sound* sound=GetSound(filename.GetStringId()); 00401 IncreaseReferenceCount(sound); 00402 } 00403 00404 00405 //*** UnloadSound *** 00406 00407 void ResourceManager::UnloadSound(const Filename& filename) 00408 { 00409 const Sound* sound=GetSound(filename.GetStringId()); 00410 DecreaseReferenceCount(sound); 00411 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
