YAPOG  0.0.1
Yet Another Pokemon Online Game
ContentManager.cpp
Go to the documentation of this file.
3 #include "YAPOG/Game/ID.hpp"
5 
6 namespace yap
7 {
8  const Path ContentManager::DEFAULT_ROOT_PATH = Path ("Content/");
9  const Path ContentManager::DEFAULT_IMAGE_PATH = Path ("Image/");
10  const Path ContentManager::DEFAULT_TEXTURE_PATH = Path ("Image/");
11  const Path ContentManager::DEFAULT_FONT_PATH = Path ("Font/");
12  const Path ContentManager::DEFAULT_SOUND_BUFFER_PATH = Path ("Sound/");
13  const Path ContentManager::DEFAULT_MUSIC_PATH = Path ("Sound/");
14 
16  ".xml";
17 
19  : rootPath_ (rootPath)
20  , imagePath_ (DEFAULT_IMAGE_PATH)
21  , texturePath_ (DEFAULT_TEXTURE_PATH)
22  , fontPath_ (DEFAULT_FONT_PATH)
23  , soundBufferPath_ (DEFAULT_SOUND_BUFFER_PATH)
24  , musicPath_ (DEFAULT_MUSIC_PATH)
25  , images_ ()
26  , textures_ ()
27  , fonts_ ()
28  , soundBuffers_ ()
29  , musics_ ()
30  {
31  }
32 
34  {
35  for (const auto& it : images_)
36  delete it.second;
37 
38  for (const auto& it : textures_)
39  delete it.second;
40 
41  for (const auto& it : fonts_)
42  delete it.second;
43 
44  for (const auto& it : soundBuffers_)
45  delete it.second;
46 
47  for (const auto& it : musics_)
48  delete it.second;
49  }
50 
52  {
53  static ContentManager instance (DEFAULT_ROOT_PATH);
54 
55  return instance;
56  }
57 
58  void ContentManager::Init (const Path& rootPath)
59  {
60  rootPath_ = rootPath;
61  }
62 
64  {
65  return rootPath_;
66  }
67 
69  {
70  imagePath_ = path;
71  }
72 
74  {
75  texturePath_ = path;
76  }
77 
78  void ContentManager::SetFontPath (const Path& path)
79  {
80  fontPath_ = path;
81  }
82 
84  {
85  soundBufferPath_ = path;
86  }
87 
89  {
90  musicPath_ = path;
91  }
92 
93  sf::Image& ContentManager::LoadImage (const String& name)
94  {
95  if (images_.Contains (name))
96  return *images_[name];
97 
98  sf::Image* image = new sf::Image;
99 
100  String path = (rootPath_ + imagePath_ + Path (name)).GetValue ();
101  if (!image->loadFromFile (path))
102  throw ContentLoadingFailException (path);
103 
104  images_.Add (name, image);
105 
106  return *image;
107  }
108 
109  sf::Texture& ContentManager::LoadTexture (const String& name)
110  {
111  if (textures_.Contains (name))
112  return *textures_[name];
113 
114  sf::Texture* texture = new sf::Texture;
115 
116  String path = (rootPath_ + texturePath_ + Path (name)).GetValue ();
117  if (!texture->loadFromFile (path))
118  throw ContentLoadingFailException (path);
119 
120  textures_.Add (name, texture);
121 
122  return *texture;
123  }
124 
125  sf::Font& ContentManager::LoadFont (const String& name)
126  {
127  if (fonts_.Contains (name))
128  return *fonts_[name];
129 
130  sf::Font* font = new sf::Font;
131 
132  String path = (rootPath_ + fontPath_ + Path (name)).GetValue ();
133  if (!font->loadFromFile (path))
134  throw ContentLoadingFailException (path);
135 
136  fonts_.Add (name, font);
137 
138  return *font;
139  }
140 
141  sf::SoundBuffer& ContentManager::LoadSoundBuffer (const String& name)
142  {
143  if (soundBuffers_.Contains (name))
144  return *soundBuffers_[name];
145 
146  sf::SoundBuffer* soundBuffer = new sf::SoundBuffer;
147 
148  String path = (rootPath_ + soundBufferPath_ + Path (name)).GetValue ();
149  if (!soundBuffer->loadFromFile (path))
150  throw ContentLoadingFailException (path);
151 
152  soundBuffers_.Add (name, soundBuffer);
153 
154  return *soundBuffer;
155  }
156 
157  sf::Music& ContentManager::LoadMusic (const String& name)
158  {
159  if (musics_.Contains (name))
160  return *musics_[name];
161 
162  sf::Music* music = new sf::Music;
163 
164  String path = (rootPath_ + musicPath_ + Path (name)).GetValue ();
165  if (!music->openFromFile (path))
166  throw ContentLoadingFailException (path);
167 
168  musics_.Add (name, music);
169 
170  return *music;
171  }
172 
174  {
175  delete images_[name];
176  images_.Remove (name);
177  }
178 
180  {
181  delete textures_[name];
182  textures_.Remove (name);
183  }
184 
186  {
187  delete fonts_[name];
188  fonts_.Remove (name);
189  }
190 
192  {
193  delete soundBuffers_[name];
194  soundBuffers_.Remove (name);
195  }
196 
198  {
199  delete musics_[name];
200  musics_.Remove (name);
201  }
202 
204  {
205  String path = (rootPath_ + Path (name)).GetValue ();
206 
207  iFStream.open (path);
208 
209  if (!iFStream.is_open ())
210  throw ContentLoadingFailException (path);
211 
212  return iFStream;
213  }
214 
216  const Path& rootPath,
217  const ID& id,
218  IFStream& iFStream)
219  {
220  return LoadFile (
221  (rootPath +
222  Path (StringHelper::ToString (id.GetValue ())) +
224  iFStream);
225  }
226 } // namespace yap