Advanced Strategic Command
iconrepository.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 #include <stdio.h>
26 #include <cstring>
27 #include <stdlib.h>
28 #include <SDL_image.h>
29 #include <set>
30 
31 #include "iconrepository.h"
32 #include "basestrm.h"
33 #include "util/messaginghub.h"
34 
35 
36 IconRepository::Repository IconRepository::repository;
37 
38 set<ASCString> errorsShown;
39 
41 {
42  Repository::iterator i = repository.find( name );
43  if ( i != repository.end() )
44  return *i->second;
45  else {
46  try {
47  tnfilestream fs ( name, tnstream::reading );
48  if ( name.endswith(".raw") ) {
49  repository[name] = new Surface();
50  repository[name]->read( fs );
51  } else {
52  repository[name] = new Surface ( IMG_Load_RW ( SDL_RWFromStream( &fs ), 1));
53  }
54  return *repository[name];
55  }
56  catch ( tfileerror err ) {
57  if ( errorsShown.find( name ) == errorsShown.end() ) {
58  errorMessage("could not load " + err.getFileName() );
59  errorsShown.insert( name );
60  }
61  if ( name != "dummy.png" )
62  return getIcon( "dummy.png" );
63  else
64  throw;
65  }
66  }
67 }
68 
69 IconRepository::Repository::~Repository()
70 {
71  for ( Repository::iterator i = repository.begin(); i != repository.end(); ++i )
72  delete i->second;
73 }
74 
75 bool IconRepository::exists( const ASCString& name )
76 {
77  return repository.find(name) != repository.end();
78 }
79 
80 void IconRepository::insert( const ASCString& name, Surface* s )
81 {
82  repository[name] = s;
83 }
84 
86 {
87  int size = 0;
88  for ( Repository::iterator i = repository.begin(); i != repository.end(); ++i )
89  size += i->second->getMemoryFootprint();
90  return size;
91 }
92 
static bool exists(const ASCString &name)
const ASCString & getFileName() const
Definition: simplestream.h:80
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static int getMemoryFootprint()
The interface for the various streams that ASC offers, like file and memory streams.
static void insert(const ASCString &name, Surface *s)
void errorMessage(const ASCString &string)
static Surface & getIcon(const ASCString &name)
SDL_RWops * SDL_RWFromStream(tnstream *stream)
Definition: basestrm.cpp:693
bool endswith(const ASCString &s) const
Checks if the last characters of string are equal to s.
Definition: ascstring.cpp:146
set< ASCString > errorsShown