Hosting courtesy of Sourceforge

SourceForge Logo
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

ImageManager.cpp

Go to the documentation of this file.
00001 //
00002 //      Copyright (C) 2002 Robert Renaud
00003 //
00004 //      This program is free software; you can redistribute it and/or
00005 //      modify it under the terms of the GNU General Public License
00006 //      as published by the Free Software Foundation; either version 2
00007 //      of the License, or (at your option) any later version.
00008 //
00009 //      This program is distributed in the hope that it will be useful,
00010 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 //
00013 //      See the GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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()) { // string found in map       
00045                 #ifdef DEBUG_GRAPHICS_IMAGE_MANAGER
00046                         debugOut << str << " found in table" << endl;
00047                 #endif
00048                 return i->second; 
00049         } else { // string not found in map
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         // linear search, will make this faster if profiling finds need to
00063         for (mapStrSurf::iterator i = nameReferenceTable.begin();i != nameReferenceTable.end(); i++) {
00064                 if (i->second == surf) {
00065                         removeFromTable(i);
00066                         return;
00067                 }
00068         }
00069         // if it reaches here, there wasno entry is in the list
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;  // deleting null harmless, no need to check
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) { // there is an allocated surface here
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 

Generated on Tue May 21 07:26:51 2002 for BomberLAN by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001