00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <cstring>
00027 #include <stdlib.h>
00028 #include <SDL_image.h>
00029 #include <set>
00030
00031 #include "iconrepository.h"
00032 #include "basestrm.h"
00033 #include "util/messaginghub.h"
00034
00035
00036 IconRepository::Repository IconRepository::repository;
00037
00038 set<ASCString> errorsShown;
00039
00040 Surface& IconRepository::getIcon( const ASCString& name )
00041 {
00042 Repository::iterator i = repository.find( name );
00043 if ( i != repository.end() )
00044 return *i->second;
00045 else {
00046 try {
00047 tnfilestream fs ( name, tnstream::reading );
00048 if ( name.endswith(".raw") ) {
00049 repository[name] = new Surface();
00050 repository[name]->read( fs );
00051 } else {
00052 repository[name] = new Surface ( IMG_Load_RW ( SDL_RWFromStream( &fs ), 1));
00053 }
00054 return *repository[name];
00055 }
00056 catch ( tfileerror err ) {
00057 if ( errorsShown.find( name ) == errorsShown.end() ) {
00058 errorMessage("could not load " + err.getFileName() );
00059 errorsShown.insert( name );
00060 }
00061 if ( name != "dummy.png" )
00062 return getIcon( "dummy.png" );
00063 else
00064 throw;
00065 }
00066 }
00067 }
00068
00069 IconRepository::Repository::~Repository()
00070 {
00071 for ( Repository::iterator i = repository.begin(); i != repository.end(); ++i )
00072 delete i->second;
00073 }
00074
00075 bool IconRepository::exists( const ASCString& name )
00076 {
00077 return repository.find(name) != repository.end();
00078 }
00079
00080 void IconRepository::insert( const ASCString& name, Surface* s )
00081 {
00082 repository[name] = s;
00083 }
00084
00085 int IconRepository::getMemoryFootprint()
00086 {
00087 int size = 0;
00088 for ( Repository::iterator i = repository.begin(); i != repository.end(); ++i )
00089 size += i->second->getMemoryFootprint();
00090 return size;
00091 }
00092