FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
animationmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <map>
24 
25 // 3rd party library includes
26 #include <tinyxml.h>
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/log/logger.h"
34 #include "util/resource/resource.h"
35 #include "video/renderbackend.h"
36 
37 #include "animationmanager.h"
38 
39 namespace FIFE {
43  static Logger _log(LM_RESMGR);
44 
46 
47  }
48 
50  size_t totalSize = 0;
51 
53  itend = m_animHandleMap.end();
54 
55  for ( ; it != itend; ++it) {
56  totalSize += it->second->getSize();
57  }
58 
59  return totalSize;
60  }
61 
64  itend = m_animHandleMap.end();
65  size_t count = 0;
66 
67  for ( ; it != itend; ++it) {
68  if ( it->second->getState() == IResource::RES_NOT_LOADED ) {
69  count++;
70  }
71  }
72 
73  return count;
74  }
75 
78  itend = m_animHandleMap.end();
79  size_t count = 0;
80 
81  for ( ; it != itend; ++it) {
82  if ( it->second->getState() == IResource::RES_LOADED ) {
83  count++;
84  }
85  }
86 
87  return count;
88  }
89 
91  return m_animHandleMap.size();
92  }
93 
95  Animation* ptr = new Animation(loader);
96  return add(ptr);
97  }
98 
99  AnimationPtr AnimationManager::create(const std::string& name, IResourceLoader* loader){
100  if (exists(name)) {
101  FL_WARN(_log, LMsg("AnimationManager::create(std::string, IResourceLoader* loader) - ") << "Resource name " << name << " was previously created. Returning original Animation...");
102  return getPtr(name);
103  }
104 
105  Animation* ptr = new Animation(name, loader);
106  return add(ptr);
107  }
108 
109  AnimationPtr AnimationManager::load(const std::string& name, IResourceLoader* loader) {
110  AnimationNameMapIterator nit = m_animNameMap.find(name);
111 
112  if (nit != m_animNameMap.end()) {
113  if ( nit->second->getState() == IResource::RES_NOT_LOADED ) {
114  nit->second->load();
115  }
116 
117  return nit->second;
118  }
119 
120  //was not found so create and load resource
121  AnimationPtr ptr = create(name, loader);
122  ptr->load();
123 
124  if (ptr->getState() == IResource::RES_NOT_LOADED){
125  FL_WARN(_log, LMsg("AnimationManager::load(std::string) - ") << "Resource name " << name << " was not found and could not be loaded.");
126  remove(name);
127  }
128 
129  return ptr;
130  }
131 
133  assert(res);
134  assert(!(exists(res->getHandle()) || exists(res->getName())));
135 
136  AnimationPtr resptr(res);
137 
138  std::pair<AnimationHandleMapIterator, bool> returnValue;
139  returnValue = m_animHandleMap.insert ( AnimationHandleMapPair(res->getHandle(), resptr));
140 
141  if (returnValue.second) {
142  m_animNameMap.insert ( AnimationNameMapPair(returnValue.first->second->getName(), returnValue.first->second) );
143  }
144  else {
145  FL_WARN(_log, LMsg("AnimationManager::add(IResource*) - ") << "Resource " << res->getName() << " already exists.... ignoring.");
146  }
147 
148  return returnValue.first->second;
149  }
150 
151  bool AnimationManager::exists(const std::string& name) {
152  AnimationNameMapIterator it = m_animNameMap.find(name);
153  if (it != m_animNameMap.end()) {
154  return true;
155  }
156 
157  return false;
158  }
159 
162  if (it != m_animHandleMap.end()) {
163  return true;
164  }
165 
166  return false;
167  }
168 
169  void AnimationManager::reload(const std::string& name) {
170  AnimationNameMapIterator nit = m_animNameMap.find(name);
171 
172  if (nit != m_animNameMap.end()) {
173  if ( nit->second->getState() == IResource::RES_LOADED) {
174  nit->second->free();
175  }
176  nit->second->load();
177  return;
178  }
179 
180  FL_WARN(_log, LMsg("AnimationManager::reload(std::string) - ") << "Resource name " << name << " not found.");
181  }
182 
185 
186  if ( it != m_animHandleMap.end()) {
187  if ( it->second->getState() == IResource::RES_LOADED) {
188  it->second->free();
189  }
190  it->second->load();
191  return;
192  }
193 
194  FL_WARN(_log, LMsg("AnimationManager::reload(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
195 
196  }
197 
200  itend = m_animHandleMap.end();
201 
202  for ( ; it != itend; ++it) {
203  if ( it->second->getState() == IResource::RES_LOADED) {
204  it->second->free();
205  }
206  it->second->load();
207  }
208  }
209 
212  itend = m_animHandleMap.end();
213 
214  int32_t count = 0;
215  for ( ; it != itend; ++it) {
216  if (it->second.useCount() == 2 && it->second->getState() != IResource::RES_LOADED){
217  it->second->load();
218  count++;
219  }
220  }
221  FL_DBG(_log, LMsg("AnimationManager::loadUnreferenced() - ") << "Loaded " << count << " unreferenced resources.");
222  }
223 
224  void AnimationManager::free(const std::string& name) {
225  AnimationNameMapIterator nit = m_animNameMap.find(name);
226 
227  if (nit != m_animNameMap.end()) {
228  if ( nit->second->getState() == IResource::RES_LOADED) {
229  nit->second->free();
230  }
231  return;
232  }
233 
234  FL_WARN(_log, LMsg("AnimationManager::free(std::string) - ") << "Resource name " << name << " not found.");
235  }
236 
239  if (it != m_animHandleMap.end()) {
240  if ( it->second->getState() == IResource::RES_LOADED) {
241  it->second->free();
242  }
243  return;
244  }
245 
246  FL_WARN(_log, LMsg("AnimationManager::free(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
247  }
248 
251  itend = m_animHandleMap.end();
252 
253  int32_t count = 0;
254 
255  for ( ; it != itend; ++it) {
256  if ( it->second->getState() == IResource::RES_LOADED) {
257  it->second->free();
258  count++;
259  }
260  }
261 
262  FL_DBG(_log, LMsg("AnimationManager::freeAll() - ") << "Freed all " << count << " resources.");
263  }
264 
267  itend = m_animHandleMap.end();
268 
269  int32_t count = 0;
270  for ( ; it != itend; ++it) {
271  if (it->second.useCount() == 2 && it->second->getState() == IResource::RES_LOADED ){
272  it->second->free();
273  count++;
274  }
275  }
276 
277  FL_DBG(_log, LMsg("AnimationManager::freeUnreferenced() - ") << "Freed " << count << " unreferenced resources.");
278  }
279 
282  AnimationNameMapIterator nit = m_animNameMap.find(resource->getName());
283 
284  if (it != m_animHandleMap.end()) {
285  m_animHandleMap.erase(it);
286 
287  if (nit != m_animNameMap.end()) {
288  m_animNameMap.erase(nit);
289  return;
290  }
291  assert(false); //should never get here
292  }
293 
294  FL_WARN(_log, LMsg("AnimationManager::remove(ResourcePtr&) - ") << "Resource " << resource->getName() << " was not found.");
295  }
296 
297  void AnimationManager::remove(const std::string& name) {
298  std::size_t handle;
299 
300  AnimationNameMapIterator nit = m_animNameMap.find(name);
301  if (nit != m_animNameMap.end()) {
302  handle = nit->second->getHandle();
303  m_animNameMap.erase(nit);
304  }
305  else {
306  FL_WARN(_log, LMsg("AnimationManager::remove(std::string) - ") << "Resource " << name << " was not found.");
307  return;
308  }
309 
311  if ( it != m_animHandleMap.end()) {
312  m_animHandleMap.erase(it);
313  return;
314  }
315 
316  assert(false); //should never get here
317  }
318 
320  std::string name;
321 
323 
324  if (it != m_animHandleMap.end()) {
325  name = it->second->getName();
326  m_animHandleMap.erase(it);
327  }
328  else {
329  FL_WARN(_log, LMsg("AnimationManager::remove(ResourceHandle) - ") << "Resource handle " << handle << " was not found.");
330  return;
331  }
332 
333  AnimationNameMapIterator nit = m_animNameMap.find(name);
334  if ( nit != m_animNameMap.end() ) {
335  m_animNameMap.erase(nit);
336  return;
337  }
338 
339  assert(false); //should never get here
340  }
341 
343  //should always be equal
344  assert (m_animHandleMap.size() == m_animNameMap.size());
345 
346  size_t count = m_animHandleMap.size();
347 
348  m_animHandleMap.clear();
349  m_animNameMap.clear();
350 
351  FL_DBG(_log, LMsg("AnimationManager::removeAll() - ") << "Removed all " << count << " resources.");
352  }
353 
356  itend = m_animHandleMap.end();
357 
358  std::vector<int> imgHandles;
359 
360  int32_t count = 0;
361  for ( ; it != itend; ++it) {
362  if ( it->second.useCount() == 2) {
363  imgHandles.push_back(it->second->getHandle());
364  count++;
365  }
366  }
367 
368  for (std::vector<int>::iterator it = imgHandles.begin(); it != imgHandles.end(); ++it) {
369  remove(*it);
370  }
371 
372  FL_DBG(_log, LMsg("AnimationManager::removeUnreferenced() - ") << "Removed " << count << " unreferenced resources.");
373  }
374 
375  AnimationPtr AnimationManager::get(const std::string& name) {
376  AnimationNameMapIterator nit = m_animNameMap.find(name);
377 
378  if (nit != m_animNameMap.end()) {
379  if (nit->second->getState() != IResource::RES_LOADED){
380  //resource is not loaded so load it
381  nit->second->load();
382  }
383  return nit->second;
384  }
385 
386  //not found so attempt to create and load the resource
387  AnimationPtr ptr = load(name);
388  return ptr;
389  }
390 
393  if (it != m_animHandleMap.end()) {
394  if (it->second->getState() != IResource::RES_LOADED){
395  //resource is not loaded so load it
396  it->second->load();
397  }
398  return it->second;
399  }
400 
401  FL_WARN(_log, LMsg("AnimationManager::get(ResourceHandle) - ") << "Resource handle " << handle << " is undefined.");
402 
403  return AnimationPtr();
404  }
405 
406  AnimationPtr AnimationManager::getPtr(const std::string& name) {
407  AnimationNameMapIterator nit = m_animNameMap.find(name);
408 
409  if (nit != m_animNameMap.end()) {
410  return nit->second;
411  }
412 
413  FL_WARN(_log, LMsg("AnimationManager::getPtr(std::string) - ") << "Resource " << name << " is undefined.");
414 
415  return AnimationPtr();
416  }
417 
420  if (it != m_animHandleMap.end()) {
421  return it->second;
422  }
423 
424  FL_WARN(_log, LMsg("AnimationManager::getPtr(ResourceHandle) - ") << "Resource handle " << handle << " is undefined.");
425 
426  return AnimationPtr();
427  }
428 
430  AnimationNameMapIterator nit = m_animNameMap.find(name);
431  if (nit != m_animNameMap.end()) {
432  return nit->second->getHandle();
433  }
434 
435  FL_WARN(_log, LMsg("AnimationManager::getResourceHandle(std::string) - ") << "Resource " << name << " is undefined.");
436 
437  return 0;
438  }
439 
440  void AnimationManager::invalidate(const std::string& name) {
441  AnimationNameMapIterator it = m_animNameMap.find(name);
442  if (it != m_animNameMap.end()) {
443  if (it->second->getState() == IResource::RES_LOADED){
444  it->second.get()->invalidate();
445  }
446  }
447  }
448 
451  if (it != m_animHandleMap.end()) {
452  if (it->second->getState() == IResource::RES_LOADED) {
453  it->second.get()->invalidate();
454  }
455  }
456  }
457 
460  itend = m_animHandleMap.end();
461 
462  for ( ; it != itend; ++it) {
463  if (it->second->getState() == IResource::RES_LOADED) {
464  it->second.get()->invalidate();
465  }
466  }
467 
468  }
469 
470 } //FIFE
#define FL_WARN(logger, msg)
Definition: logger.h:72
virtual ResourceState getState()
Definition: resource.h:70
virtual ResourceHandle getResourceHandle(const std::string &name)
Gets an Animation handle by name.
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
virtual size_t getTotalResourcesLoaded() const
Returns the number of loaded resources.
Animation.
Definition: animation.h:53
virtual void remove(AnimationPtr &resource)
Removes an Animation from the manager.
static Logger _log(LM_AUDIO)
virtual AnimationPtr getPtr(const std::string &name)
virtual size_t getTotalResources() const
Returns the number of defined resources.
virtual void freeUnreferenced()
Frees all unreferenced Animation.
std::size_t ResourceHandle
Definition: resource.h:38
virtual size_t getMemoryUsed() const
Gets the total amount of memory used by resources.
virtual AnimationPtr add(Animation *res)
Add an Animation to the manager.
virtual void removeAll()
Removes all Animations from the manager.
virtual AnimationPtr create(IResourceLoader *loader=0)
Creates a blank Animation but does not load it immediately.
virtual ~AnimationManager()
Destructor.
virtual void loadUnreferenced()
Loads all unreferenced Animations.
virtual void load()
Definition: animation.cpp:63
std::map< std::string, AnimationPtr >::iterator AnimationNameMapIterator
AnimationHandleMap m_animHandleMap
virtual AnimationPtr load(const std::string &name, IResourceLoader *loader=0)
Creates a blank resource and loads it from disk.
virtual const std::string & getName()
Definition: resource.h:66
virtual size_t getTotalResourcesCreated() const
Returns the number of unloaded resources.
virtual void removeUnreferenced()
Removes all unreferenced Animations.
std::map< ResourceHandle, AnimationPtr >::const_iterator AnimationHandleMapConstIterator
virtual void invalidate(const std::string &name)
SharedPtr< Animation > AnimationPtr
Definition: animation.h:168
virtual bool exists(const std::string &name)
Checks to see if an Animation exists.
std::map< ResourceHandle, AnimationPtr >::iterator AnimationHandleMapIterator
virtual AnimationPtr get(const std::string &name)
Gets a shared pointer to the Animation.
virtual void reloadAll()
Reloads all Animations.
virtual void reload(const std::string &name)
Reloads an Animation.
ResourceHandle getHandle()
Definition: resource.h:68
AnimationNameMap m_animNameMap
std::pair< std::string, AnimationPtr > AnimationNameMapPair
std::pair< ResourceHandle, AnimationPtr > AnimationHandleMapPair
#define FL_DBG(logger, msg)
Definition: logger.h:70
virtual void free(const std::string &name)
Frees an Animation from memory.
virtual void freeAll()
Frees all Animations.