00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ImageManager.h"
00021
00022 using namespace Graphics;
00023
00024 ImageManager* ImageManager::instance = NULL;
00025
00026 ImageManager* ImageManager::getInstance() {
00027 if (instance == NULL) {
00028 instance = new ImageManager;
00029 }
00030 return instance;
00031 }
00032
00033 ImageManager::ImageManager() { }
00034
00035 ImageManager::~ImageManager() {
00036 freeAll();
00037 }
00038
00039 SDL_Surface* ImageManager::getImage(const string& str) {
00040 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00041 debugOut << "ImageManager::getImage(), ";
00042 #endif
00043 mapStrSurf::iterator i = nameReferenceTable.find(str);
00044 if (i != this->nameReferenceTable.end()) {
00045 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00046 debugOut << str << " found in table" << endl;
00047 #endif
00048 return i->second;
00049 } else {
00050
00051 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00052 debugOut << str << " not found in table, being allocated now" << endl;
00053 #endif
00054 SDL_Surface* newSurf = SDL_LoadBMP(str.c_str());
00055 if (newSurf == NULL) throw Util::GeneralException(std::string("BMP not loaded successfully ") + str.c_str(), __FILE__, __LINE__);
00056 nameReferenceTable[str] = newSurf;
00057 return newSurf;
00058 }
00059 }
00060
00061 void ImageManager::freeBMP(SDL_Surface* surf) {
00062
00063 for (mapStrSurf::iterator i = nameReferenceTable.begin();i != nameReferenceTable.end(); i++) {
00064 if (i->second == surf) {
00065 removeFromTable(i);
00066 return;
00067 }
00068 }
00069
00070 }
00071
00072 void ImageManager::freeBMP(const string& str) {
00073 mapStrSurf::iterator i = nameReferenceTable.find(str);
00074 if (i != this->nameReferenceTable.end()) {
00075 removeFromTable(i);
00076 return;
00077 }
00078 }
00079
00080 void ImageManager::removeFromTable(mapStrSurf::iterator &i) {
00081 SDL_FreeSurface(i->second);
00082 this->nameReferenceTable.erase(i);
00083 }
00084
00085 void ImageManager::destroy() {
00086 delete instance;
00087 }
00088
00089 void ImageManager::freeAll() {
00090 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00091 debugOut << "ImageManager::freeAll() called, freeing images from following files\n [ ";
00092 #endif
00093
00094 for (mapStrSurf::iterator i = nameReferenceTable.begin(); i != nameReferenceTable.end(); i++) {
00095 if (i->second != NULL) {
00096 SDL_FreeSurface(i->second);
00097 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00098 debugOut << i->first << ", \t";
00099 #endif
00100 }
00101 }
00102 nameReferenceTable.clear();
00103
00104 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00105 debugOut << "]" << endl;
00106 #endif
00107 }
00108
00109 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00110 void ImageManager::debugPrintPairs() {
00111 debugOut << "printing pairs (name, surf) of reference table " << endl;
00112 for (mapStrSurf::iterator i = nameReferenceTable.begin(); i != nameReferenceTable.end(); ++i) {
00113 debugOut << "( " << i->first << ", " << i->second << " ) \t";
00114 }
00115 debugOut << endl;
00116 }
00117 #endif
00118
00119
00120