OgreResource.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef _Resource_H__
00029 #define _Resource_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 #include "OgreString.h"
00033 #include "OgreSharedPtr.h"
00034 #include "OgreStringInterface.h"
00035 #include "OgreAtomicScalar.h"
00036 #include "Threading/OgreThreadHeaders.h"
00037 #include "OgreHeaderPrefix.h"
00038 
00039 namespace Ogre {
00040 
00041     typedef unsigned long long int ResourceHandle;
00042 
00043 
00044     // Forward declaration
00045     class ManualResourceLoader;
00046 
00079     class _OgreExport Resource : public StringInterface, public ResourceAlloc
00080     {
00081     public:
00082         OGRE_AUTO_MUTEX; // public to allow external locking
00083         class Listener
00084         {
00085         public:
00086             Listener() {}
00087             virtual ~Listener() {}
00088 
00093             OGRE_DEPRECATED virtual void backgroundLoadingComplete(Resource*) {}
00094 
00099             OGRE_DEPRECATED virtual void backgroundPreparingComplete(Resource*) {}
00100 
00109             virtual void loadingComplete(Resource*) {}
00110 
00111 
00120             virtual void preparingComplete(Resource*) {}
00121 
00123             virtual void unloadingComplete(Resource*) {}
00124         };
00125         
00127         enum LoadingState
00128         {
00130             LOADSTATE_UNLOADED,
00132             LOADSTATE_LOADING,
00134             LOADSTATE_LOADED,
00136             LOADSTATE_UNLOADING,
00138             LOADSTATE_PREPARED,
00140             LOADSTATE_PREPARING
00141         };
00142     protected:
00144         ResourceManager* mCreator;
00146         String mName;
00148         String mGroup;
00150         ResourceHandle mHandle;
00152         AtomicScalar<LoadingState> mLoadingState;
00154         volatile bool mIsBackgroundLoaded;
00156         size_t mSize;
00158         bool mIsManual;
00160         String mOrigin;
00162         ManualResourceLoader* mLoader;
00164         size_t mStateCount;
00165 
00166         typedef set<Listener*>::type ListenerList;
00167         ListenerList mListenerList;
00168         OGRE_MUTEX(mListenerListMutex);
00169 
00172         Resource() 
00173             : mCreator(0), mHandle(0), mLoadingState(LOADSTATE_UNLOADED), 
00174             mIsBackgroundLoaded(false), mSize(0), mIsManual(0), mLoader(0)
00175         { 
00176         }
00177 
00184         virtual void preLoadImpl(void) {}
00191         virtual void postLoadImpl(void) {}
00192 
00196         virtual void preUnloadImpl(void) {}
00201         virtual void postUnloadImpl(void) {}
00202 
00205         virtual void prepareImpl(void) {}
00210         virtual void unprepareImpl(void) {}
00214         virtual void loadImpl(void) = 0;
00218         virtual void unloadImpl(void) = 0;
00219 
00220     public:
00235         Resource(ResourceManager* creator, const String& name, ResourceHandle handle,
00236             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
00237 
00243         virtual ~Resource();
00244 
00259         virtual void prepare(bool backgroundThread = false);
00260 
00271         virtual void load(bool backgroundThread = false);
00272 
00278         virtual void reload(void);
00279 
00282         virtual bool isReloadable(void) const
00283         {
00284             return !mIsManual || mLoader;
00285         }
00286 
00289         virtual bool isManuallyLoaded(void) const
00290         {
00291             return mIsManual;
00292         }
00293 
00297         virtual void unload(void);
00298 
00301         virtual size_t getSize(void) const
00302         { 
00303             return mSize; 
00304         }
00305 
00308         virtual void touch(void);
00309 
00312         virtual const String& getName(void) const 
00313         { 
00314             return mName; 
00315         }
00316 
00317         virtual ResourceHandle getHandle(void) const
00318         {
00319             return mHandle;
00320         }
00321 
00324         virtual bool isPrepared(void) const 
00325         { 
00326             // No lock required to read this state since no modify
00327             return (mLoadingState.get() == LOADSTATE_PREPARED); 
00328         }
00329 
00332         virtual bool isLoaded(void) const 
00333         { 
00334             // No lock required to read this state since no modify
00335             return (mLoadingState.get() == LOADSTATE_LOADED); 
00336         }
00337 
00341         virtual bool isLoading() const
00342         {
00343             return (mLoadingState.get() == LOADSTATE_LOADING);
00344         }
00345 
00348         virtual LoadingState getLoadingState() const
00349         {
00350             return mLoadingState.get();
00351         }
00352 
00353 
00354 
00365         virtual bool isBackgroundLoaded(void) const { return mIsBackgroundLoaded; }
00366 
00375         virtual void setBackgroundLoaded(bool bl) { mIsBackgroundLoaded = bl; }
00376 
00386         virtual void escalateLoading();
00387 
00391         virtual void addListener(Listener* lis);
00392 
00396         virtual void removeListener(Listener* lis);
00397 
00399         virtual const String& getGroup(void) const { return mGroup; }
00400 
00408         virtual void changeGroupOwnership(const String& newGroup);
00409 
00411         virtual ResourceManager* getCreator(void) { return mCreator; }
00418         virtual const String& getOrigin(void) const { return mOrigin; }
00420         virtual void _notifyOrigin(const String& origin) { mOrigin = origin; }
00421 
00429         virtual size_t getStateCount() const { return mStateCount; }
00430 
00436         virtual void _dirtyState();
00437 
00438 
00447         virtual void _fireLoadingComplete(bool wasBackgroundLoaded);
00448 
00457         virtual void _firePreparingComplete(bool wasBackgroundLoaded);
00458 
00466         virtual void _fireUnloadingComplete(void);
00467 
00469         virtual size_t calculateSize(void) const;
00470 
00471     };
00472 
00491     typedef SharedPtr<Resource> ResourcePtr;
00492 
00514     class _OgreExport ManualResourceLoader
00515     {
00516     public:
00517         ManualResourceLoader() {}
00518         virtual ~ManualResourceLoader() {}
00519 
00526         virtual void prepareResource(Resource* resource)
00527                 { (void)resource; }
00528 
00532         virtual void loadResource(Resource* resource) = 0;
00533     };
00536 }
00537 
00538 #include "OgreHeaderSuffix.h"
00539 
00540 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:45