iconrepository.cpp

Go to the documentation of this file.
00001 
00005 /*
00006     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00007     Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with this program; see the file COPYING. If not, write to the 
00021     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00022     Boston, MA  02111-1307  USA
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 

Generated on Mon May 21 01:26:34 2012 for Advanced Strategic Command by  doxygen 1.5.1