source: nrEngine/include/Resource.h @ 1

Revision 1, 7.8 KB checked in by art, 13 years ago (diff)
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13
14#ifndef _NR_I_RESOURCE__H_
15#define _NR_I_RESOURCE__H_
16
17
18//----------------------------------------------------------------------------------
19// Includes
20//----------------------------------------------------------------------------------
21#include "Prerequisities.h"
22#include "ResourceSystem.h"
23
24namespace nrEngine{
25
26        //! General interface to hold any kind of resources
27        /**
28        * This is an interface which is describing how resource classes should looks like.
29        * Derive your own classes to get resource management system working. Each
30        * resource containing only once in the memory if it has the same name. So all
31        * names of resources should be unique. Resource is not directly stored by the
32        * manager but by the ResourceHolder. This holder just stores the resource
33        * and is unique for each resource. Manager just manages such holders and
34        * gives the resource pointers access to it.
35        *
36        * All derived classes must implement the behavior of resource objects correctly.
37        * Correct behavior of resource objects is done if resources do nothing what can
38        * change the empty resource behavior if they are empty. For example if you use texture
39        * and user is setting some values to it and it is at the moment unloaded, so the method
40        * will be called by the empty texture object. Empty texture object should stay empty and
41        * therefor there should not be any changes. So texture object has to check whenever it is
42        * an empty resource and should not do anything.
43        *
44        * The empty flag should be set by the loader, because only the loader does know
45        * if and when a resource can be defined as empty. For example you have to load the empty
46        * resource texture before you declare this resource as empty.
47        *
48        * \ingroup resource
49        **/
50        class _NRExport IResource{
51        public:
52
53
54                /**
55                 * As soon as this destructor is called, the according loader will be
56                 * notified about removing the resource from the memory. So the loader
57                 * will correct it's database and notify also the ResourceManager about this
58                 * event.
59                 *
60                 * You have to call the unloadRes() method if you have some specific unloading
61                 * routines for your resource object. We can not call this from here, because
62                 * calling of virtual functions from constructor/destructor cause in
63                 * undefined behaviour. However we call the appropriate resource loader
64                 * to notify him that this resource is getting unloaded now.
65                 **/
66                virtual ~IResource();
67
68                /**
69                * Here the class should return cound of bytes reserved
70                * by the resource. The size must not contain the size of the class itself.
71                * For example: 32Bit Texture of 16x16 Pixel has the size: 16*16*4 = 1024 Bytes +
72                * some more bytes which does class need to store himself in the memory.
73                **/
74                NR_FORCEINLINE std::size_t getResDataSize(){return mResDataSize;}
75
76
77                /**
78                * Return here the name of the resource type. This name will be used
79                * to assign right empty resources to the resource pointer/holder objects.
80                * E.g: "Texture", "Sound",  ...
81                **/
82                NR_FORCEINLINE const std::string& getResType() const { return mResType; }
83
84
85                /**
86                * Return true if the resource is marked as loaded. If any resource is loaded
87                * so it contains in the memory and has it's full data there.
88                **/
89                NR_FORCEINLINE bool                             isResLoaded() const {return mResIsLoaded;}
90
91                /**
92                * Return handle of this resource
93                **/
94                NR_FORCEINLINE ResourceHandle           getResHandle() const {return mResHandle;}
95
96                /**
97                * Return group name to which one this resource belongs
98                **/
99                NR_FORCEINLINE const std::string&       getResGroup() const {return mResGroup;}
100
101                /**
102                * Get the name of the resource
103                **/
104                NR_FORCEINLINE const std::string&       getResName() const {return mResName;}
105
106                /**
107                * Get the file name from which one this resource can be restored or loaded
108                **/
109                NR_FORCEINLINE const std::string&       getResFileName() const {return mResFileName;}
110
111                /**
112                * Returns true if this resource is a empty resource. Empty resources are used
113                * to replace normal resources if they are unloaded.
114                **/
115                NR_FORCEINLINE bool                             isResEmpty()    {return mResIsEmpty;}
116
117        protected:
118               
119                /**
120                 * Create resource instance.
121                 **/
122                IResource(const std::string& resourceType);
123
124                /**
125                 * Unload the resource. Each resource should know how to unload it from
126                 * the memory. Unloading does not remove the resource it just unload used
127                 * data from memory. To remove the resource from memory you have either
128                 * to use ResourceManager or the ResourceLoader.
129                 *
130                 * Do here resource specific stuff. The method will be called by the loader,
131                 * so you do not have to notify the loader about this event.
132                 **/
133                virtual Result unloadRes();
134
135                /**
136                 * Loader will notify the resource that it has been restored.
137                 * A resource is restored if it was reloaded by the manager or
138                 * loader. The loader will restore the data in the way like it
139                 * has done when loading the resource. After that the loader will
140                 * notify the resource that it has been restored, so resource has
141                 * to do some initialization on this data if needed.
142                 *
143                 * The default behaviour on this function is just not to do anything.
144                 * So overload this method with your own, if you need any specific behaviour
145                 * on the restored data.
146                 **/
147                virtual void notifyResReloaded() { }
148               
149                /**
150                 * Mark a resource as loaded. This method will not call the reloadRes()
151                 * method. Instead it will just mark this resource as it were loaded,
152                 * so you get access to real resource object through the pointers instead
153                 * of emtpy resource. Call this method if you create a resource by your
154                 * own without loading by loader.
155                 **/
156                NR_FORCEINLINE void markResLoaded() { mResIsLoaded = true; }
157               
158        private:
159               
160                //! Only loader can change resource data
161                friend class IResourceLoader;
162
163                //! Also resource manager is a friend
164                friend class ResourceManager;
165
166                //! Shows whenever resource is loaded (is in the memory)
167                bool mResIsLoaded;
168
169                //! If true so this is a empty resource
170                bool mResIsEmpty;
171
172                //! Resource's loader with which one it was created
173                IResourceLoader*        mResLoader;
174
175                //! Handle of the resource given from manager
176                ResourceHandle  mResHandle;
177
178                //! Filename from which one this resource can be reloaded/loaded
179                std::string     mResFileName;
180
181                //! Count of bytes that this resource is occupies in the memory
182                std::size_t             mResDataSize;
183
184                //! Name of the resource type
185                std::string             mResType;
186               
187                //! Group name to which one this resource own
188                std::string mResGroup;
189
190                //! Name of the resource
191                std::string mResName;
192               
193                /**
194                 * Set the resource type for this resource.
195                 * Method is declared as protected, so only engine can do this.
196                 *
197                 * @param type Name of the resource type
198                 **/
199                NR_FORCEINLINE void     setResourceType(const std::string& type) { mResType = type; }
200
201                /**
202                 * If the unloading of resource is done through the resource manager,
203                 * so this method will be called.
204                 **/
205                Result unloadResThroughLoader();
206               
207                /**
208                 * Reload the resource through the according resource loader.
209                 **/
210                Result reloadResThroughLoader();
211
212                /**
213                 * Remove the resource through it's loader
214                 **/
215                Result removeResThroughLoader();
216               
217        };
218
219};
220
221#endif
Note: See TracBrowser for help on using the repository browser.