Debian Codegraph @

engine/core/video/animationmanager.cpp

raw
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"
33 #include "util/resource/resourcemanager.h"
34 #include "util/resource/resource.h"
35 #include "video/renderbackend.h"
36
37 #include "animationmanager.h"
38
39 namespace FIFE {
40 /** Logger to use for this source file.
41 * @relates Logger
42 */
43 static Logger _log(LM_RESMGR);
44
45 AnimationManager::~AnimationManager() {
46
47 }
48
49 size_t AnimationManager::getMemoryUsed() const {
50 size_t totalSize = 0;
51
52 AnimationHandleMapConstIterator it = m_animHandleMap.begin(),
53 itend = m_animHandleMap.end();
54
55 for ( ; it != itend; ++it) {
56 totalSize += it->second->getSize();
57 }
58
59 return totalSize;
60 }
61
62 size_t AnimationManager::getTotalResourcesCreated() const {
63 AnimationHandleMapConstIterator it = m_animHandleMap.begin(),
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
76 size_t AnimationManager::getTotalResourcesLoaded() const {
77 AnimationHandleMapConstIterator it = m_animHandleMap.begin(),
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
90 size_t AnimationManager::getTotalResources() const {
91 return m_animHandleMap.size();
92 }
93
94 AnimationPtr AnimationManager::create(IResourceLoader* loader){
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
132 AnimationPtr AnimationManager::add(Animation* res) {
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
160 bool AnimationManager::exists(ResourceHandle handle) {
161 AnimationHandleMapConstIterator it = m_animHandleMap.find(handle);
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
183 void AnimationManager::reload(ResourceHandle handle) {
184 AnimationHandleMapIterator it = m_animHandleMap.find(handle);
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
198 void AnimationManager::reloadAll() {
199 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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
210 void AnimationManager::loadUnreferenced() {
211 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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
237 void AnimationManager::free(ResourceHandle handle) {
238 AnimationHandleMapConstIterator it = m_animHandleMap.find(handle);
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
249 void AnimationManager::freeAll() {
250 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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
265 void AnimationManager::freeUnreferenced() {
266 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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
280 void AnimationManager::remove(AnimationPtr& resource) {
281 AnimationHandleMapIterator it = m_animHandleMap.find(resource->getHandle());
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
310 AnimationHandleMapIterator it = m_animHandleMap.find(handle);
311 if ( it != m_animHandleMap.end()) {
312 m_animHandleMap.erase(it);
313 return;
314 }
315
316 assert(false); //should never get here
317 }
318
319 void AnimationManager::remove(ResourceHandle handle) {
320 std::string name;
321
322 AnimationHandleMapIterator it = m_animHandleMap.find(handle);
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
342 void AnimationManager::removeAll() {
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
354 void AnimationManager::removeUnreferenced() {
355 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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
391 AnimationPtr AnimationManager::get(ResourceHandle handle) {
392 AnimationHandleMapConstIterator it = m_animHandleMap.find(handle);
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
418 AnimationPtr AnimationManager::getPtr(ResourceHandle handle) {
419 AnimationHandleMapConstIterator it = m_animHandleMap.find(handle);
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
429 ResourceHandle AnimationManager::getResourceHandle(const std::string& name) {
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
449 void AnimationManager::invalidate(ResourceHandle handle) {
450 AnimationHandleMapIterator it = m_animHandleMap.find(handle);
451 if (it != m_animHandleMap.end()) {
452 if (it->second->getState() == IResource::RES_LOADED) {
453 it->second.get()->invalidate();
454 }
455 }
456 }
457
458 void AnimationManager::invalidateAll() {
459 AnimationHandleMapIterator it = m_animHandleMap.begin(),
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