source: OGRE/trunk/ogrenew/OgreMain/src/OgreResourceManager.cpp @ 692

Revision 692, 10.4 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

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-2005 The OGRE Team
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-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgreResourceManager.h"
27
28#include "OgreException.h"
29#include "OgreArchive.h"
30#include "OgreArchiveManager.h"
31#include "OgreStringVector.h"
32#include "OgreStringConverter.h"
33#include "OgreResourceGroupManager.h"
34
35namespace Ogre {
36
37    //-----------------------------------------------------------------------
38    ResourceManager::ResourceManager()
39                : mNextHandle(1), mMemoryUsage(0), mLoadOrder(0)
40    {
41        // Init memory limit & usage
42        mMemoryBudget = std::numeric_limits<unsigned long>::max();
43    }
44    //-----------------------------------------------------------------------
45    ResourceManager::~ResourceManager()
46    {
47        removeAll();
48    }
49        //-----------------------------------------------------------------------
50    ResourcePtr ResourceManager::create(const String& name, const String& group,
51                bool isManual, ManualResourceLoader* loader, const NameValuePairList* params)
52        {
53                // Call creation implementation
54                ResourcePtr ret = ResourcePtr(
55            createImpl(name, getNextHandle(), group, isManual, loader, params));
56        if (params)
57            ret->setParameterList(*params);
58
59                addImpl(ret);
60                // Tell resource group manager
61                ResourceGroupManager::getSingleton()._notifyResourceCreated(ret);
62                return ret;
63
64        }
65    //-----------------------------------------------------------------------
66    ResourcePtr ResourceManager::load(const String& name,
67        const String& group, bool isManual, ManualResourceLoader* loader,
68        const NameValuePairList* loadParams)
69    {
70        ResourcePtr ret = getByName(name);
71        if (ret.isNull())
72        {
73            ret = create(name, group, isManual, loader, loadParams);
74        }
75                // ensure loaded
76        ret->load();
77        return ret;
78    }
79    //-----------------------------------------------------------------------
80    void ResourceManager::addImpl( ResourcePtr& res )
81    {
82                OGRE_LOCK_AUTO_MUTEX
83
84        std::pair<ResourceMap::iterator, bool> result =
85            mResources.insert( ResourceMap::value_type( res->getName(), res ) );
86        if (!result.second)
87        {
88            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the name " + res->getName() +
89                " already exists.", "ResourceManager::add");
90        }
91        std::pair<ResourceHandleMap::iterator, bool> resultHandle =
92            mResourcesByHandle.insert( ResourceHandleMap::value_type( res->getHandle(), res ) );
93        if (!resultHandle.second)
94        {
95            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the handle " +
96                StringConverter::toString((long) (res->getHandle())) +
97                " already exists.", "ResourceManager::add");
98        }
99
100    }
101        //-----------------------------------------------------------------------
102        void ResourceManager::removeImpl( ResourcePtr& res )
103        {
104                OGRE_LOCK_AUTO_MUTEX
105
106                ResourceMap::iterator nameIt = mResources.find(res->getName());
107                if (nameIt != mResources.end())
108                {
109                        mResources.erase(nameIt);
110                }
111
112                ResourceHandleMap::iterator handleIt = mResourcesByHandle.find(res->getHandle());
113                if (handleIt != mResourcesByHandle.end())
114                {
115                        mResourcesByHandle.erase(handleIt);
116                }
117                // Tell resource group manager
118                ResourceGroupManager::getSingleton()._notifyResourceRemoved(res);
119        }
120    //-----------------------------------------------------------------------
121    void ResourceManager::setMemoryBudget( size_t bytes)
122    {
123        // Update limit & check usage
124        mMemoryBudget = bytes;
125        checkUsage();
126    }
127    //-----------------------------------------------------------------------
128    size_t ResourceManager::getMemoryBudget(void) const
129    {
130        return mMemoryBudget;
131    }
132        //-----------------------------------------------------------------------
133        void ResourceManager::unload(const String& name)
134        {
135                ResourcePtr res = getByName(name);
136
137                if (!res.isNull())
138                {
139                        // Unload resource
140                        res->unload();
141
142                }
143        }
144        //-----------------------------------------------------------------------
145        void ResourceManager::unload(ResourceHandle handle)
146        {
147                ResourcePtr res = getByHandle(handle);
148
149                if (!res.isNull())
150                {
151                        // Unload resource
152                        res->unload();
153
154                }
155        }
156        //-----------------------------------------------------------------------
157        void ResourceManager::unloadAll(bool reloadableOnly)
158        {
159                OGRE_LOCK_AUTO_MUTEX
160
161                ResourceMap::iterator i, iend;
162                iend = mResources.end();
163                for (i = mResources.begin(); i != iend; ++i)
164                {
165                        if (!reloadableOnly || i->second->isReloadable())
166                        {
167                                i->second->unload();
168                        }
169                }
170
171        }
172        //-----------------------------------------------------------------------
173        void ResourceManager::reloadAll(bool reloadableOnly)
174        {
175                OGRE_LOCK_AUTO_MUTEX
176
177                ResourceMap::iterator i, iend;
178                iend = mResources.end();
179                for (i = mResources.begin(); i != iend; ++i)
180                {
181                        if (!reloadableOnly || i->second->isReloadable())
182                        {
183                                i->second->reload();
184                        }
185                }
186
187        }
188    //-----------------------------------------------------------------------
189    void ResourceManager::unloadUnreferencedResources(bool reloadableOnly)
190    {
191        OGRE_LOCK_AUTO_MUTEX
192
193        ResourceMap::iterator i, iend;
194        iend = mResources.end();
195        for (i = mResources.begin(); i != iend; ++i)
196        {
197            // A use count of 3 means that only RGM and RM have references
198            // RGM has one (this one) and RM has 2 (by name and by handle)
199            if (i->second.useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS)
200            {
201                Resource* res = i->second.get();
202                if (!reloadableOnly || res->isReloadable())
203                {
204                    res->unload();
205                }
206            }
207        }
208    }
209    //-----------------------------------------------------------------------
210    void ResourceManager::reloadUnreferencedResources(bool reloadableOnly)
211    {
212        OGRE_LOCK_AUTO_MUTEX
213
214        ResourceMap::iterator i, iend;
215        iend = mResources.end();
216        for (i = mResources.begin(); i != iend; ++i)
217        {
218            // A use count of 3 means that only RGM and RM have references
219            // RGM has one (this one) and RM has 2 (by name and by handle)
220            if (i->second.useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS)
221            {
222                Resource* res = i->second.get();
223                if (!reloadableOnly || res->isReloadable())
224                {
225                    res->reload();
226                }
227            }
228        }
229    }
230    //-----------------------------------------------------------------------
231    void ResourceManager::remove(ResourcePtr& res)
232    {
233        removeImpl(res);
234    }
235        //-----------------------------------------------------------------------
236        void ResourceManager::remove(const String& name)
237        {
238                ResourcePtr res = getByName(name);
239
240                if (!res.isNull())
241                {
242                        removeImpl(res);
243                }
244        }
245        //-----------------------------------------------------------------------
246        void ResourceManager::remove(ResourceHandle handle)
247        {
248                ResourcePtr res = getByHandle(handle);
249
250                if (!res.isNull())
251                {
252                        removeImpl(res);
253                }
254        }
255        //-----------------------------------------------------------------------
256        void ResourceManager::removeAll(void)
257        {
258                OGRE_LOCK_AUTO_MUTEX
259
260                mResources.clear();
261                mResourcesByHandle.clear();
262                // Notify resource group manager
263                ResourceGroupManager::getSingleton()._notifyAllResourcesRemoved(this);
264        }
265    //-----------------------------------------------------------------------
266    ResourcePtr ResourceManager::getByName(const String& name)
267    {
268                OGRE_LOCK_AUTO_MUTEX
269
270        ResourceMap::iterator it = mResources.find(name);
271
272        if( it == mResources.end())
273                {
274            return ResourcePtr();
275                }
276        else
277        {
278            return it->second;
279        }
280    }
281    //-----------------------------------------------------------------------
282    ResourcePtr ResourceManager::getByHandle(ResourceHandle handle)
283    {
284                OGRE_LOCK_AUTO_MUTEX
285
286        ResourceHandleMap::iterator it = mResourcesByHandle.find(handle);
287        if (it == mResourcesByHandle.end())
288        {
289            return ResourcePtr();
290        }
291        else
292        {
293            return it->second;
294        }
295    }
296    //-----------------------------------------------------------------------
297    ResourceHandle ResourceManager::getNextHandle(void)
298    {
299                OGRE_LOCK_AUTO_MUTEX
300
301        return mNextHandle++;
302    }
303    //-----------------------------------------------------------------------
304    void ResourceManager::checkUsage(void)
305    {
306        // TODO Page out here?
307    }
308        //-----------------------------------------------------------------------
309        void ResourceManager::_notifyResourceTouched(Resource* res)
310        {
311                // TODO
312        }
313        //-----------------------------------------------------------------------
314        void ResourceManager::_notifyResourceLoaded(Resource* res)
315        {
316                OGRE_LOCK_AUTO_MUTEX
317
318                mMemoryUsage += res->getSize();
319        }
320        //-----------------------------------------------------------------------
321        void ResourceManager::_notifyResourceUnloaded(Resource* res)
322        {
323                OGRE_LOCK_AUTO_MUTEX
324
325                mMemoryUsage -= res->getSize();
326        }
327        //-----------------------------------------------------------------------
328
329}
330
331
332
Note: See TracBrowser for help on using the repository browser.