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

Revision 690, 8.7 KB checked in by mattausch, 19 years ago (diff)

added ogre 1.07 main

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(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(void)
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                        i->second->unload();
166                }
167
168        }
169        //-----------------------------------------------------------------------
170        void ResourceManager::reloadAll(void)
171        {
172                OGRE_LOCK_AUTO_MUTEX
173
174                ResourceMap::iterator i, iend;
175                iend = mResources.end();
176                for (i = mResources.begin(); i != iend; ++i)
177                {
178                        i->second->reload();
179                }
180
181        }
182    //-----------------------------------------------------------------------
183    void ResourceManager::remove(ResourcePtr& res)
184    {
185        removeImpl(res);
186    }
187        //-----------------------------------------------------------------------
188        void ResourceManager::remove(const String& name)
189        {
190                ResourcePtr res = getByName(name);
191
192                if (!res.isNull())
193                {
194                        removeImpl(res);
195                }
196        }
197        //-----------------------------------------------------------------------
198        void ResourceManager::remove(ResourceHandle handle)
199        {
200                ResourcePtr res = getByHandle(handle);
201
202                if (!res.isNull())
203                {
204                        removeImpl(res);
205                }
206        }
207        //-----------------------------------------------------------------------
208        void ResourceManager::removeAll(void)
209        {
210                OGRE_LOCK_AUTO_MUTEX
211
212                mResources.clear();
213                mResourcesByHandle.clear();
214                // Notify resource group manager
215                ResourceGroupManager::getSingleton()._notifyAllResourcesRemoved(this);
216        }
217    //-----------------------------------------------------------------------
218    ResourcePtr ResourceManager::getByName(const String& name)
219    {
220                OGRE_LOCK_AUTO_MUTEX
221
222        ResourceMap::iterator it = mResources.find(name);
223
224        if( it == mResources.end())
225                {
226            return ResourcePtr();
227                }
228        else
229        {
230            return it->second;
231        }
232    }
233    //-----------------------------------------------------------------------
234    ResourcePtr ResourceManager::getByHandle(ResourceHandle handle)
235    {
236                OGRE_LOCK_AUTO_MUTEX
237
238        ResourceHandleMap::iterator it = mResourcesByHandle.find(handle);
239        if (it == mResourcesByHandle.end())
240        {
241            return ResourcePtr();
242        }
243        else
244        {
245            return it->second;
246        }
247    }
248    //-----------------------------------------------------------------------
249    ResourceHandle ResourceManager::getNextHandle(void)
250    {
251                OGRE_LOCK_AUTO_MUTEX
252
253        return mNextHandle++;
254    }
255    //-----------------------------------------------------------------------
256    void ResourceManager::checkUsage(void)
257    {
258        // TODO Page out here?
259    }
260        //-----------------------------------------------------------------------
261        void ResourceManager::_notifyResourceTouched(Resource* res)
262        {
263                // TODO
264        }
265        //-----------------------------------------------------------------------
266        void ResourceManager::_notifyResourceLoaded(Resource* res)
267        {
268                OGRE_LOCK_AUTO_MUTEX
269
270                mMemoryUsage += res->getSize();
271        }
272        //-----------------------------------------------------------------------
273        void ResourceManager::_notifyResourceUnloaded(Resource* res)
274        {
275                OGRE_LOCK_AUTO_MUTEX
276
277                mMemoryUsage -= res->getSize();
278        }
279        //-----------------------------------------------------------------------
280
281}
282
283
284
Note: See TracBrowser for help on using the repository browser.