Debian Codegraph @

engine/core/audio/soundclipmanager.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 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
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/log/logger.h"
32 #include "util/resource/resourcemanager.h"
33 #include "util/resource/resource.h"
34
35 #include "soundclipmanager.h"
36
37 namespace FIFE {
38 static Logger _log(LM_RESMGR);
39
40 SoundClipManager::~SoundClipManager() {
41
42 }
43
44 size_t SoundClipManager::getMemoryUsed() const {
45 size_t totalSize = 0;
46
47 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
48 itend = m_sclipHandleMap.end();
49
50 for ( ; it != itend; ++it) {
51 totalSize += it->second->getSize();
52 }
53
54 return totalSize;
55 }
56
57 size_t SoundClipManager::getTotalResourcesCreated() const {
58 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
59 itend = m_sclipHandleMap.end();
60 size_t count = 0;
61
62 for ( ; it != itend; ++it) {
63 if ( it->second->getState() == IResource::RES_NOT_LOADED ) {
64 count++;
65 }
66 }
67
68 return count;
69 }
70
71 size_t SoundClipManager::getTotalResourcesLoaded() const {
72 SoundClipHandleMapConstIterator it = m_sclipHandleMap.begin(),
73 itend = m_sclipHandleMap.end();
74 size_t count = 0;
75
76 for ( ; it != itend; ++it) {
77 if ( it->second->getState() == IResource::RES_LOADED ) {
78 count++;
79 }
80 }
81
82 return count;
83 }
84
85 size_t SoundClipManager::getTotalResources() const {
86 return m_sclipHandleMap.size();
87 }
88
89 SoundClipPtr SoundClipManager::create(IResourceLoader* loader){
90 SoundClip* ptr = new SoundClip(loader);
91 return add(ptr);
92 }
93
94 SoundClipPtr SoundClipManager::create(const std::string& name, IResourceLoader* loader){
95 if (exists(name)) {
96 FL_WARN(_log, LMsg("SoundClipManager::create(std::string, IResourceLoader* loader) - ") << "Resource name " << name << " was previously created. Returning original SoundClip...");
97 return get(name);
98 }
99
100 SoundClip* ptr = new SoundClip(name, loader);
101 return add(ptr);
102 }
103
104 SoundClipPtr SoundClipManager::load(const std::string& name, IResourceLoader* loader) {
105 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
106
107 if (nit != m_sclipNameMap.end()) {
108 if ( nit->second->getState() == IResource::RES_NOT_LOADED ) {
109 nit->second->load();
110 }
111
112 return nit->second;
113 }
114
115 //was not found so create and load resource
116 SoundClipPtr ptr = create(name, loader);
117 ptr->load();
118
119 if (ptr->getState() == IResource::RES_NOT_LOADED){
120 FL_WARN(_log, LMsg("SoundClipManager::load(std::string) - ") << "Resource name " << name << " was not found and could not be loaded.");
121 remove(name);
122 }
123
124 return ptr;
125 }
126
127 SoundClipPtr SoundClipManager::add(SoundClip* res) {
128 assert(res);
129 assert(!(exists(res->getHandle()) || exists(res->getName())));
130
131 SoundClipPtr resptr(res);
132
133 std::pair<SoundClipHandleMapIterator, bool> returnValue;
134 returnValue = m_sclipHandleMap.insert ( SoundClipHandleMapPair(res->getHandle(), resptr));
135
136 if (returnValue.second) {
137 m_sclipNameMap.insert ( SoundClipNameMapPair(returnValue.first->second->getName(), returnValue.first->second) );
138 }
139 else {
140 FL_WARN(_log, LMsg("SoundClipManager::add(IResource*) - ") << "Resource " << res->getName() << " already exists.... ignoring.");
141 }
142
143 return returnValue.first->second;
144 }
145
146 bool SoundClipManager::exists(const std::string& name) {
147 SoundClipNameMapIterator it = m_sclipNameMap.find(name);
148 if (it != m_sclipNameMap.end()) {
149 return true;
150 }
151
152 return false;
153 }
154
155 bool SoundClipManager::exists(ResourceHandle handle) {
156 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
157 if (it != m_sclipHandleMap.end()) {
158 return true;
159 }
160
161 return false;
162 }
163
164 void SoundClipManager::reload(const std::string& name) {
165 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
166
167 if (nit != m_sclipNameMap.end()) {
168 if ( nit->second->getState() == IResource::RES_LOADED) {
169 nit->second->free();
170 }
171 nit->second->load();
172 return;
173 }
174
175 FL_WARN(_log, LMsg("SoundClipManager::reload(std::string) - ") << "Resource name " << name << " not found.");
176 }
177
178 void SoundClipManager::reload(ResourceHandle handle) {
179 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
180
181 if ( it != m_sclipHandleMap.end()) {
182 if ( it->second->getState() == IResource::RES_LOADED) {
183 it->second->free();
184 }
185 it->second->load();
186 return;
187 }
188
189 FL_WARN(_log, LMsg("SoundClipManager::reload(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
190
191 }
192
193 void SoundClipManager::reloadAll() {
194 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
195 itend = m_sclipHandleMap.end();
196
197 for ( ; it != itend; ++it) {
198 if ( it->second->getState() == IResource::RES_LOADED) {
199 it->second->free();
200 }
201 it->second->load();
202 }
203 }
204
205 void SoundClipManager::loadUnreferenced() {
206 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
207 itend = m_sclipHandleMap.end();
208
209 int32_t count = 0;
210 for ( ; it != itend; ++it) {
211 if (it->second.useCount() == 2 && it->second->getState() != IResource::RES_LOADED){
212 it->second->load();
213 count++;
214 }
215 }
216 FL_DBG(_log, LMsg("SoundClipManager::loadUnreferenced() - ") << "Loaded " << count << " unreferenced resources.");
217 }
218
219 void SoundClipManager::free(const std::string& name) {
220 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
221
222 if (nit != m_sclipNameMap.end()) {
223 if ( nit->second->getState() == IResource::RES_LOADED) {
224 nit->second->free();
225 }
226 return;
227 }
228
229 FL_WARN(_log, LMsg("SoundClipManager::free(std::string) - ") << "Resource name " << name << " not found.");
230 }
231
232 void SoundClipManager::free(ResourceHandle handle) {
233 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
234 if (it != m_sclipHandleMap.end()) {
235 if ( it->second->getState() == IResource::RES_LOADED) {
236 it->second->free();
237 }
238 return;
239 }
240
241 FL_WARN(_log, LMsg("SoundClipManager::free(ResourceHandle) - ") << "Resource handle " << handle << " not found.");
242 }
243
244 void SoundClipManager::freeAll() {
245 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
246 itend = m_sclipHandleMap.end();
247
248 int32_t count = 0;
249
250 for ( ; it != itend; ++it) {
251 if ( it->second->getState() == IResource::RES_LOADED) {
252 it->second->free();
253 count++;
254 }
255 }
256
257 FL_DBG(_log, LMsg("SoundClipManager::freeAll() - ") << "Freed all " << count << " resources.");
258 }
259
260 void SoundClipManager::freeUnreferenced() {
261 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
262 itend = m_sclipHandleMap.end();
263
264 int32_t count = 0;
265 for ( ; it != itend; ++it) {
266 if (it->second.useCount() == 2 && it->second->getState() == IResource::RES_LOADED ){
267 it->second->free();
268 count++;
269 }
270 }
271
272 FL_DBG(_log, LMsg("SoundClipManager::freeUnreferenced() - ") << "Freed " << count << " unreferenced resources.");
273 }
274
275 void SoundClipManager::remove(SoundClipPtr& resource) {
276 SoundClipHandleMapIterator it = m_sclipHandleMap.find(resource->getHandle());
277 SoundClipNameMapIterator nit = m_sclipNameMap.find(resource->getName());
278
279 if (it != m_sclipHandleMap.end()) {
280 m_sclipHandleMap.erase(it);
281
282 if (nit != m_sclipNameMap.end()) {
283 m_sclipNameMap.erase(nit);
284 return;
285 }
286 assert(false); //should never get here
287 }
288
289 FL_WARN(_log, LMsg("SoundClipManager::remove(ResourcePtr&) - ") << "Resource " << resource->getName() << " was not found.");
290 }
291
292 void SoundClipManager::remove(const std::string& name) {
293 std::size_t handle;
294
295 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
296 if (nit != m_sclipNameMap.end()) {
297 handle = nit->second->getHandle();
298 m_sclipNameMap.erase(nit);
299 }
300 else {
301 FL_WARN(_log, LMsg("SoundClipManager::remove(std::string) - ") << "Resource " << name << " was not found.");
302 return;
303 }
304
305 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
306 if ( it != m_sclipHandleMap.end()) {
307 m_sclipHandleMap.erase(it);
308 return;
309 }
310
311 assert(false); //should never get here
312 }
313
314 void SoundClipManager::remove(ResourceHandle handle) {
315 std::string name;
316
317 SoundClipHandleMapIterator it = m_sclipHandleMap.find(handle);
318
319 if (it != m_sclipHandleMap.end()) {
320 name = it->second->getName();
321 m_sclipHandleMap.erase(it);
322 }
323 else {
324 FL_WARN(_log, LMsg("SoundClipManager::remove(ResourceHandle) - ") << "Resource handle " << handle << " was not found.");
325 return;
326 }
327
328 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
329 if ( nit != m_sclipNameMap.end() ) {
330 m_sclipNameMap.erase(nit);
331 return;
332 }
333
334 assert(false); //should never get here
335 }
336
337 void SoundClipManager::removeAll() {
338 //should always be equal
339 assert (m_sclipHandleMap.size() == m_sclipNameMap.size());
340
341 size_t count = m_sclipHandleMap.size();
342
343 m_sclipHandleMap.clear();
344 m_sclipNameMap.clear();
345
346 FL_DBG(_log, LMsg("SoundClipManager::removeAll() - ") << "Removed all " << count << " resources.");
347 }
348
349 void SoundClipManager::removeUnreferenced() {
350 SoundClipHandleMapIterator it = m_sclipHandleMap.begin(),
351 itend = m_sclipHandleMap.end();
352
353 int32_t count = 0;
354 for ( ; it != itend; ++it) {
355 if ( it->second.useCount() == 2) {
356 remove(it->second->getHandle());
357 count++;
358 }
359 }
360
361 FL_DBG(_log, LMsg("SoundClipManager::removeUnreferenced() - ") << "Removed " << count << " unreferenced resources.");
362 }
363
364 SoundClipPtr SoundClipManager::get(const std::string& name) {
365 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
366
367 if (nit != m_sclipNameMap.end()) {
368 if (nit->second->getState() != IResource::RES_LOADED){
369 //resource is not loaded so load it
370 nit->second->load();
371 }
372 return nit->second;
373 }
374
375 //not found so attempt to create and load the resource
376 SoundClipPtr ptr = load(name);
377 return ptr;
378 }
379
380 SoundClipPtr SoundClipManager::get(ResourceHandle handle) {
381 SoundClipHandleMapConstIterator it = m_sclipHandleMap.find(handle);
382 if (it != m_sclipHandleMap.end()) {
383 if (it->second->getState() != IResource::RES_LOADED){
384 //resource is not loaded so load it
385 it->second->load();
386 }
387 return it->second;
388 }
389
390 FL_WARN(_log, LMsg("SoundClipManager::get(ResourceHandle) - ") << "Resource handle " << handle << " is undefined.");
391
392 return SoundClipPtr();
393 }
394
395 ResourceHandle SoundClipManager::getResourceHandle(const std::string& name) {
396 SoundClipNameMapIterator nit = m_sclipNameMap.find(name);
397 if (nit != m_sclipNameMap.end()) {
398 return nit->second->getHandle();
399 }
400
401 FL_WARN(_log, LMsg("SoundClipManager::getResourceHandle(std::string) - ") << "Resource " << name << " is undefined.");
402
403 return 0;
404 }
405
406 } //FIFE