# 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-2006 Torus Knot Software Ltd
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023
    00024 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef _Resource_H__
    00030 #define _Resource_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreString.h"
    00034 #include "OgreSharedPtr.h"
    00035 #include "OgreStringInterface.h"
    00036 #include "OgreAtomicWrappers.h"
    00037
    00038 namespace Ogre {
    00039
    00040     typedef unsigned long ResourceHandle;
    00041
    00042
    00043     // Forward declaration
    00044     class ManualResourceLoader;
    00045
    00072     class _OgreExport Resource : public StringInterface, public ResourceAlloc
    00073     {
    00074     public:
    00075         OGRE_AUTO_MUTEX // public to allow external locking
    00076         class Listener
    00077         {
    00078         public:
    00079             Listener() {}
    00080             virtual ~Listener() {}
    00081
    00092             virtual void backgroundLoadingComplete(Resource*) {}
    00093
    00104             virtual void backgroundPreparingComplete(Resource*) {}
    00105
    00106         };
    00107
    00109         enum LoadingState
    00110         {
    00112             LOADSTATE_UNLOADED,
    00114             LOADSTATE_LOADING,
    00116             LOADSTATE_LOADED,
    00118             LOADSTATE_UNLOADING,
    00120             LOADSTATE_PREPARED,
    00122             LOADSTATE_PREPARING
    00123         };
    00124     protected:
    00126         ResourceManager* mCreator;
    00128         String mName;
    00130         String mGroup;
    00132         ResourceHandle mHandle;
    00134         AtomicScalar<LoadingState> mLoadingState;
    00136         volatile bool mIsBackgroundLoaded;
    00138         size_t mSize;
    00140         bool mIsManual;
    00142         String mOrigin;
    00144         ManualResourceLoader* mLoader;
    00146         size_t mStateCount;
    00147
    00148         typedef std::list<Listener*> ListenerList;
    00149         ListenerList mListenerList;
    00150         OGRE_MUTEX(mListenerListMutex)
    00151
    00152
    00154         Resource()
    00155             : mCreator(0), mHandle(0), mLoadingState(LOADSTATE_UNLOADED),
    00156             mIsBackgroundLoaded(false), mSize(0), mIsManual(0), mLoader(0)
    00157         {
    00158         }
    00159
    00166         virtual void preLoadImpl(void) {}
    00173         virtual void postLoadImpl(void) {}
    00174
    00178         virtual void preUnloadImpl(void) {}
    00183         virtual void postUnloadImpl(void) {}
    00184
    00187         virtual void prepareImpl(void) {}
    00192         virtual void unprepareImpl(void) {}
    00196         virtual void loadImpl(void) = 0;
    00200         virtual void unloadImpl(void) = 0;
    00202         virtual size_t calculateSize(void) const = 0;
    00203
    00205         virtual void queueFireBackgroundLoadingComplete(void);
    00206
    00208         virtual void queueFireBackgroundPreparingComplete(void);
    00209
    00210     public:
    00225         Resource(ResourceManager* creator, const String& name, ResourceHandle handle,
    00226             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
    00227
    00233         virtual ~Resource();
    00234
    00248         virtual void prepare();
    00249
    00260         virtual void load(bool backgroundThread = false);
    00261
    00267         virtual void reload(void);
    00268
    00271         virtual bool isReloadable(void) const
    00272         {
    00273             return !mIsManual || mLoader;
    00274         }
    00275
    00278         virtual bool isManuallyLoaded(void) const
    00279         {
    00280             return mIsManual;
    00281         }
    00282
    00286         virtual void unload(void);
    00287
    00290         virtual size_t getSize(void) const
    00291         {
    00292             return mSize;
    00293         }
    00294
    00297         virtual void touch(void);
    00298
    00301         virtual const String& getName(void) const
    00302         {
    00303             return mName;
    00304         }
    00305
    00306         virtual ResourceHandle getHandle(void) const
    00307         {
    00308             return mHandle;
    00309         }
    00310
    00313         virtual bool isPrepared(void) const
    00314         {
    00315             // No lock required to read this state since no modify
    00316             return (mLoadingState.get() == LOADSTATE_PREPARED);
    00317         }
    00318
    00321         virtual bool isLoaded(void) const
    00322         {
    00323             // No lock required to read this state since no modify
    00324             return (mLoadingState.get() == LOADSTATE_LOADED);
    00325         }
    00326
    00330         virtual bool isLoading() const
    00331         {
    00332             return (mLoadingState.get() == LOADSTATE_LOADING);
    00333         }
    00334
    00337         virtual LoadingState getLoadingState() const
    00338         {
    00339             return mLoadingState.get();
    00340         }
    00341
    00342
    00343
    00354         virtual bool isBackgroundLoaded(void) const { return mIsBackgroundLoaded; }
    00355
    00364         virtual void setBackgroundLoaded(bool bl) { mIsBackgroundLoaded = bl; }
    00365
    00375         virtual void escalateLoading();
    00376
    00380         virtual void addListener(Listener* lis);
    00381
    00385         virtual void removeListener(Listener* lis);
    00386
    00388         virtual const String& getGroup(void) { return mGroup; }
    00389
    00397         virtual void changeGroupOwnership(const String& newGroup);
    00398
    00400         virtual ResourceManager* getCreator(void) { return mCreator; }
    00407         virtual const String& getOrigin(void) const { return mOrigin; }
    00409         virtual void _notifyOrigin(const String& origin) { mOrigin = origin; }
    00410
    00418         virtual size_t getStateCount() const { return mStateCount; }
    00419
    00425         virtual void _dirtyState();
    00426
    00427
    00435         virtual void _fireBackgroundLoadingComplete(void);
    00436
    00444         virtual void _fireBackgroundPreparingComplete(void);
    00445
    00446     };
    00447
    00466     typedef SharedPtr<Resource> ResourcePtr;
    00467
    00489     class _OgreExport ManualResourceLoader
    00490     {
    00491     public:
    00492         ManualResourceLoader() {}
    00493         virtual ~ManualResourceLoader() {}
    00494
    00501         virtual void prepareResource(Resource* resource) { }
    00502
    00506         virtual void loadResource(Resource* resource) = 0;
    00507     };
    00508 }
    00509
    00510 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sat Apr 11 14:26:46 2009