# 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-2009 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 "OgreAtomicWrappers.h"
    00036
    00037 namespace Ogre {
    00038
    00039     typedef unsigned long long int ResourceHandle;
    00040
    00041
    00042     // Forward declaration
    00043     class ManualResourceLoader;
    00044
    00077     class _OgreExport Resource : public StringInterface, public ResourceAlloc
    00078     {
    00079     public:
    00080         OGRE_AUTO_MUTEX // public to allow external locking
    00081         class Listener
    00082         {
    00083         public:
    00084             Listener() {}
    00085             virtual ~Listener() {}
    00086
    00091             virtual void backgroundLoadingComplete(Resource*) {}
    00092
    00097             virtual void backgroundPreparingComplete(Resource*) {}
    00098
    00107             virtual void loadingComplete(Resource*) {}
    00108
    00109
    00118             virtual void preparingComplete(Resource*) {}
    00119
    00121             virtual void unloadingComplete(Resource*) {}
    00122         };
    00123
    00125         enum LoadingState
    00126         {
    00128             LOADSTATE_UNLOADED,
    00130             LOADSTATE_LOADING,
    00132             LOADSTATE_LOADED,
    00134             LOADSTATE_UNLOADING,
    00136             LOADSTATE_PREPARED,
    00138             LOADSTATE_PREPARING
    00139         };
    00140     protected:
    00142         ResourceManager* mCreator;
    00144         String mName;
    00146         String mGroup;
    00148         ResourceHandle mHandle;
    00150         AtomicScalar<LoadingState> mLoadingState;
    00152         volatile bool mIsBackgroundLoaded;
    00154         size_t mSize;
    00156         bool mIsManual;
    00158         String mOrigin;
    00160         ManualResourceLoader* mLoader;
    00162         size_t mStateCount;
    00163
    00164         typedef list<Listener*>::type ListenerList;
    00165         ListenerList mListenerList;
    00166         OGRE_MUTEX(mListenerListMutex)
    00167
    00168
    00170         Resource()
    00171             : mCreator(0), mHandle(0), mLoadingState(LOADSTATE_UNLOADED),
    00172             mIsBackgroundLoaded(false), mSize(0), mIsManual(0), mLoader(0)
    00173         {
    00174         }
    00175
    00182         virtual void preLoadImpl(void) {}
    00189         virtual void postLoadImpl(void) {}
    00190
    00194         virtual void preUnloadImpl(void) {}
    00199         virtual void postUnloadImpl(void) {}
    00200
    00203         virtual void prepareImpl(void) {}
    00208         virtual void unprepareImpl(void) {}
    00212         virtual void loadImpl(void) = 0;
    00216         virtual void unloadImpl(void) = 0;
    00218         virtual size_t calculateSize(void) const = 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) { 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
    00468
    00469     };
    00470
    00489     typedef SharedPtr<Resource> ResourcePtr;
    00490
    00512     class _OgreExport ManualResourceLoader
    00513     {
    00514     public:
    00515         ManualResourceLoader() {}
    00516         virtual ~ManualResourceLoader() {}
    00517
    00524         virtual void prepareResource(Resource* resource)
    00525                 { (void)resource; }
    00526
    00530         virtual void loadResource(Resource* resource) = 0;
    00531     };
    00534 }
    00535
    00536 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52