Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreResourceManager.cpp @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 10.9 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreResourceManager.h"
31
32#include "OgreException.h"
33#include "OgreArchive.h"
34#include "OgreArchiveManager.h"
35#include "OgreStringVector.h"
36#include "OgreStringConverter.h"
37#include "OgreResourceGroupManager.h"
38
39namespace Ogre {
40
41    //-----------------------------------------------------------------------
42    ResourceManager::ResourceManager()
43                : mNextHandle(1), mMemoryUsage(0), mLoadOrder(0)
44    {
45        // Init memory limit & usage
46        mMemoryBudget = std::numeric_limits<unsigned long>::max();
47    }
48    //-----------------------------------------------------------------------
49    ResourceManager::~ResourceManager()
50    {
51        removeAll();
52    }
53        //-----------------------------------------------------------------------
54    ResourcePtr ResourceManager::create(const String& name, const String& group, 
55                bool isManual, ManualResourceLoader* loader, const NameValuePairList* params)
56        {
57                // Call creation implementation
58                ResourcePtr ret = ResourcePtr(
59            createImpl(name, getNextHandle(), group, isManual, loader, params));
60        if (params)
61            ret->setParameterList(*params);
62
63                addImpl(ret);
64                // Tell resource group manager
65                ResourceGroupManager::getSingleton()._notifyResourceCreated(ret);
66                return ret;
67
68        }
69    //-----------------------------------------------------------------------
70    ResourceManager::ResourceCreateOrRetrieveResult
71        ResourceManager::createOrRetrieve(
72                const String& name, const String& group, 
73                bool isManual, ManualResourceLoader* loader, 
74                const NameValuePairList* params)
75        {
76                // Lock for the whole get / insert
77                OGRE_LOCK_AUTO_MUTEX
78
79                ResourcePtr res = getByName(name);
80                bool created = false;
81                if (res.isNull())
82                {
83                        created = true;
84                        res = create(name, group, isManual, loader, params);
85                }
86
87                return ResourceCreateOrRetrieveResult(res, created);
88        }
89    //-----------------------------------------------------------------------
90    ResourcePtr ResourceManager::load(const String& name, 
91        const String& group, bool isManual, ManualResourceLoader* loader, 
92        const NameValuePairList* loadParams)
93    {
94        ResourcePtr ret = getByName(name);
95        if (ret.isNull())
96        {
97            ret = create(name, group, isManual, loader, loadParams);
98        }
99                // ensure loaded
100        ret->load();
101        return ret;
102    }
103    //-----------------------------------------------------------------------
104    void ResourceManager::addImpl( ResourcePtr& res )
105    {
106                OGRE_LOCK_AUTO_MUTEX
107
108        std::pair<ResourceMap::iterator, bool> result = 
109            mResources.insert( ResourceMap::value_type( res->getName(), res ) );
110        if (!result.second)
111        {
112            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the name " + res->getName() + 
113                " already exists.", "ResourceManager::add");
114        }
115        std::pair<ResourceHandleMap::iterator, bool> resultHandle = 
116            mResourcesByHandle.insert( ResourceHandleMap::value_type( res->getHandle(), res ) );
117        if (!resultHandle.second)
118        {
119            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the handle " + 
120                StringConverter::toString((long) (res->getHandle())) + 
121                " already exists.", "ResourceManager::add");
122        }
123
124    }
125        //-----------------------------------------------------------------------
126        void ResourceManager::removeImpl( ResourcePtr& res )
127        {
128                OGRE_LOCK_AUTO_MUTEX
129
130                ResourceMap::iterator nameIt = mResources.find(res->getName());
131                if (nameIt != mResources.end())
132                {
133                        mResources.erase(nameIt);
134                }
135
136                ResourceHandleMap::iterator handleIt = mResourcesByHandle.find(res->getHandle());
137                if (handleIt != mResourcesByHandle.end())
138                {
139                        mResourcesByHandle.erase(handleIt);
140                }
141                // Tell resource group manager
142                ResourceGroupManager::getSingleton()._notifyResourceRemoved(res);
143        }
144    //-----------------------------------------------------------------------
145    void ResourceManager::setMemoryBudget( size_t bytes)
146    {
147        // Update limit & check usage
148        mMemoryBudget = bytes;
149        checkUsage();
150    }
151    //-----------------------------------------------------------------------
152    size_t ResourceManager::getMemoryBudget(void) const
153    {
154        return mMemoryBudget;
155    }
156        //-----------------------------------------------------------------------
157        void ResourceManager::unload(const String& name)
158        {
159                ResourcePtr res = getByName(name);
160
161                if (!res.isNull())
162                {
163                        // Unload resource
164                        res->unload();
165
166                }
167        }
168        //-----------------------------------------------------------------------
169        void ResourceManager::unload(ResourceHandle handle)
170        {
171                ResourcePtr res = getByHandle(handle);
172
173                if (!res.isNull())
174                {
175                        // Unload resource
176                        res->unload();
177
178                }
179        }
180        //-----------------------------------------------------------------------
181        void ResourceManager::unloadAll(bool reloadableOnly)
182        {
183                OGRE_LOCK_AUTO_MUTEX
184
185                ResourceMap::iterator i, iend;
186                iend = mResources.end();
187                for (i = mResources.begin(); i != iend; ++i)
188                {
189                        if (!reloadableOnly || i->second->isReloadable())
190                        {
191                                i->second->unload();
192                        }
193                }
194
195        }
196        //-----------------------------------------------------------------------
197        void ResourceManager::reloadAll(bool reloadableOnly)
198        {
199                OGRE_LOCK_AUTO_MUTEX
200
201                ResourceMap::iterator i, iend;
202                iend = mResources.end();
203                for (i = mResources.begin(); i != iend; ++i)
204                {
205                        if (!reloadableOnly || i->second->isReloadable())
206                        {
207                                i->second->reload();
208                        }
209                }
210
211        }
212    //-----------------------------------------------------------------------
213    void ResourceManager::unloadUnreferencedResources(bool reloadableOnly)
214    {
215        OGRE_LOCK_AUTO_MUTEX
216
217        ResourceMap::iterator i, iend;
218        iend = mResources.end();
219        for (i = mResources.begin(); i != iend; ++i)
220        {
221            // A use count of 3 means that only RGM and RM have references
222            // RGM has one (this one) and RM has 2 (by name and by handle)
223            if (i->second.useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS)
224            {
225                Resource* res = i->second.get();
226                if (!reloadableOnly || res->isReloadable())
227                {
228                    res->unload();
229                }
230            }
231        }
232    }
233    //-----------------------------------------------------------------------
234    void ResourceManager::reloadUnreferencedResources(bool reloadableOnly)
235    {
236        OGRE_LOCK_AUTO_MUTEX
237
238        ResourceMap::iterator i, iend;
239        iend = mResources.end();
240        for (i = mResources.begin(); i != iend; ++i)
241        {
242            // A use count of 3 means that only RGM and RM have references
243            // RGM has one (this one) and RM has 2 (by name and by handle)
244            if (i->second.useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS)
245            {
246                Resource* res = i->second.get();
247                if (!reloadableOnly || res->isReloadable())
248                {
249                    res->reload();
250                }
251            }
252        }
253    }
254    //-----------------------------------------------------------------------
255    void ResourceManager::remove(ResourcePtr& res)
256    {
257        removeImpl(res);
258    }
259        //-----------------------------------------------------------------------
260        void ResourceManager::remove(const String& name)
261        {
262                ResourcePtr res = getByName(name);
263
264                if (!res.isNull())
265                {
266                        removeImpl(res);
267                }
268        }
269        //-----------------------------------------------------------------------
270        void ResourceManager::remove(ResourceHandle handle)
271        {
272                ResourcePtr res = getByHandle(handle);
273
274                if (!res.isNull())
275                {
276                        removeImpl(res);
277                }
278        }
279        //-----------------------------------------------------------------------
280        void ResourceManager::removeAll(void)
281        {
282                OGRE_LOCK_AUTO_MUTEX
283
284                mResources.clear();
285                mResourcesByHandle.clear();
286                // Notify resource group manager
287                ResourceGroupManager::getSingleton()._notifyAllResourcesRemoved(this);
288        }
289    //-----------------------------------------------------------------------
290    ResourcePtr ResourceManager::getByName(const String& name)
291    {
292                OGRE_LOCK_AUTO_MUTEX
293
294        ResourceMap::iterator it = mResources.find(name);
295
296        if( it == mResources.end())
297                {
298            return ResourcePtr();
299                }
300        else
301        {
302            return it->second;
303        }
304    }
305    //-----------------------------------------------------------------------
306    ResourcePtr ResourceManager::getByHandle(ResourceHandle handle)
307    {
308                OGRE_LOCK_AUTO_MUTEX
309
310        ResourceHandleMap::iterator it = mResourcesByHandle.find(handle);
311        if (it == mResourcesByHandle.end())
312        {
313            return ResourcePtr();
314        }
315        else
316        {
317            return it->second;
318        }
319    }
320    //-----------------------------------------------------------------------
321    ResourceHandle ResourceManager::getNextHandle(void)
322    {
323                OGRE_LOCK_AUTO_MUTEX
324
325        return mNextHandle++;
326    }
327    //-----------------------------------------------------------------------
328    void ResourceManager::checkUsage(void)
329    {
330        // TODO Page out here?
331    }
332        //-----------------------------------------------------------------------
333        void ResourceManager::_notifyResourceTouched(Resource* res)
334        {
335                // TODO
336        }
337        //-----------------------------------------------------------------------
338        void ResourceManager::_notifyResourceLoaded(Resource* res)
339        {
340                OGRE_LOCK_AUTO_MUTEX
341
342                mMemoryUsage += res->getSize();
343        }
344        //-----------------------------------------------------------------------
345        void ResourceManager::_notifyResourceUnloaded(Resource* res)
346        {
347                OGRE_LOCK_AUTO_MUTEX
348
349                mMemoryUsage -= res->getSize();
350        }
351        //-----------------------------------------------------------------------
352
353}
354
355
356
Note: See TracBrowser for help on using the repository browser.