Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYImageManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "TYImageManager.h"
22 
23 #include "OImageBmp.h"
24 #include "OImageFont.h"
25 
26 #include <sstream>
27 
28 // Initialise a NULL.
30 
32 
34 {
35  for (std::map<std::string, OImage*>::iterator it = _imageMap.begin(); it != _imageMap.end(); ++it)
36  {
37  delete it->second;
38  }
39  _imageMap.clear();
40 }
41 
43 {
44  bool ret = _pInstance ? true : false;
45  _pInstance = this;
46  return ret;
47 }
48 
50 {
51  if (!_pInstance)
52  {
53  _pInstance = new TYImageManager();
54  }
55 
56  return _pInstance;
57 }
58 
60 {
61  // Chargement des images pour les terrains.
62  for (unsigned int i = 0; i < 10; ++i)
63  {
64  std::ostringstream oss;
65  oss << "id_terrain_" << i;
66 
67  if (!loadImage(OLocalizator::getPicture("TYTerrainGraphic", oss.str().c_str()).toLatin1().data()))
68  {
69  return false;
70  }
71  }
72 
73  // Chargement de la police de caracteres pour les barres de legende.
74  if (!loadImage(OLocalizator::getPicture("TYPaletteGraphic", "id_font").toLatin1().data()))
75  {
76  return false;
77  }
78 
79  return true;
80 }
81 
82 bool TYImageManager::loadImage(const char* filename)
83 {
84  // On verifie si l'image existe deja
85  std::map<std::string, OImage*>::iterator it;
86  it = _imageMap.find(filename);
87  if (it != _imageMap.end())
88  {
89  return true;
90  }
91 
92  // On recupere l'extension
93  std::string str(filename);
94  size_t pos = str.rfind('.');
95  std::string ext("None");
96  if (pos != std::string::npos)
97  {
98  ext = std::string(str, pos + 1, str.size() - pos);
99  }
100 
101  // On cree une nouvelle image
102  OImage* im = nullptr;
103  if (ext == "bmp")
104  {
105  im = new OImageBmp();
106  }
107  else if (ext == "fnt")
108  {
109  im = new OImageFont();
110  }
111  else
112  {
113  return false;
114  }
115 
116  // On charge l'image
117  if (!im->load(filename))
118  {
119  delete im;
120  return false;
121  }
122 
123  // On l'ajoute a la map
124  _imageMap[filename] = im;
125 
126  return true;
127 }
128 
129 OImage* TYImageManager::getImage(const char* filename)
130 {
131  // On verifie si l'image existe deja
132  std::map<std::string, OImage*>::iterator it;
133  it = _imageMap.find(filename);
134  if (it != _imageMap.end())
135  {
136  return it->second;
137  }
138 
139  // On recupere l'extension
140  std::string str(filename);
141  size_t pos = str.rfind('.');
142  if (pos == std::string::npos)
143  {
144  return NULL;
145  }
146  std::string ext(str, pos + 1, str.size() - pos);
147 
148  // On cree une nouvelle image
149  OImage* im = NULL;
150  if (ext == "bmp")
151  {
152  im = new OImageBmp();
153  }
154  else if (ext == "fnt")
155  {
156  im = new OImageFont();
157  }
158  // On charge l'image
159  if (im != NULL && !im->load(filename))
160  {
161  delete im;
162  return 0;
163  }
164 
165  // Add it to the map
166  _imageMap[filename] = im;
167  return im;
168 }
Definition: OImage.h:34
virtual bool load(const std::string &filename)=0
static QString getPicture(const QString &classname, const QString &pictureId)
std::map< std::string, OImage * > _imageMap
OImage * getImage(const char *filename)
static LPTYImageManager _pInstance
bool loadImage(const char *filename)
static LPTYImageManager get()