| 1 | /*! | 
|---|
| 2 |  * @file resource_manager.h | 
|---|
| 3 |   *  The Resource Manager checks if a file/resource is loaded. | 
|---|
| 4 |  | 
|---|
| 5 |     If a file/resource was already loaded the resourceManager will | 
|---|
| 6 |     return a void pointer to the desired resource. | 
|---|
| 7 |     Otherwise it will instruct the coresponding resource-loader to load, | 
|---|
| 8 |     and receive a pointer to it. | 
|---|
| 9 |  | 
|---|
| 10 |     it is possible to compile the resource Manager without some modules by | 
|---|
| 11 |     just adding the compile flag -D.... | 
|---|
| 12 |     (NO_MODEL) | 
|---|
| 13 |     (NO_AUDIO) | 
|---|
| 14 |     (NO_TEXT) | 
|---|
| 15 |     (NO_TEXTURES) | 
|---|
| 16 |     (NO_SHADERS) | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | #ifndef _RESOURCE_MANAGER_H | 
|---|
| 20 | #define _RESOURCE_MANAGER_H | 
|---|
| 21 |  | 
|---|
| 22 | #include "base_object.h" | 
|---|
| 23 | #include "filesys/file.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "multi_type.h" | 
|---|
| 26 | #include <vector> | 
|---|
| 27 |  | 
|---|
| 28 | //! An eumerator for different fileTypes the resourceManager supports | 
|---|
| 29 | typedef enum ResourceType | 
|---|
| 30 | { | 
|---|
| 31 | #ifndef NO_MODEL | 
|---|
| 32 |   OBJ,                  //!< loading .obj file | 
|---|
| 33 |   PRIM,                 //!< loading primitive model | 
|---|
| 34 |   MD2,                  //!< loading md2-file | 
|---|
| 35 | #endif /* NO_MODEL */ | 
|---|
| 36 | #ifndef NO_TEXT | 
|---|
| 37 |   TTF,                  //!< loading a TrueTypeFont | 
|---|
| 38 | #endif /* NO_TEXT */ | 
|---|
| 39 | #ifndef NO_AUDIO | 
|---|
| 40 |   WAV,                  //!< loading wav | 
|---|
| 41 |   MP3,                  //!< loading mp3 | 
|---|
| 42 |   OGG,                  //!< loading ogg | 
|---|
| 43 | #endif /* NO_AUDIO */ | 
|---|
| 44 | #ifndef NO_TEXTURES | 
|---|
| 45 |   IMAGE,                //!< loading an image | 
|---|
| 46 | #endif /* NO_TEXTURES */ | 
|---|
| 47 | #ifndef NO_SHADERS | 
|---|
| 48 |   SHADER,               //!< openGL-shader program | 
|---|
| 49 | #endif /* NO_SHADERS */ | 
|---|
| 50 |   RESOURCE_TYPE_SIZE | 
|---|
| 51 | }; | 
|---|
| 52 |  | 
|---|
| 53 | //! An enumerator for different UNLOAD-types. | 
|---|
| 54 | /** | 
|---|
| 55 |    RP_NO:        will be unloaded on request | 
|---|
| 56 |    RP_LEVEL:     will be unloaded at the end of a Level | 
|---|
| 57 |    RP_CAMPAIGN:  will be unloaded at the end of a Campaign | 
|---|
| 58 |    RP_GAME:      will be unloaded at the end of the whole Game (when closing orxonox) | 
|---|
| 59 | */ | 
|---|
| 60 | typedef enum ResourcePriority | 
|---|
| 61 | { | 
|---|
| 62 |   RP_NO        =   0, | 
|---|
| 63 |   RP_LEVEL     =   1, | 
|---|
| 64 |   RP_CAMPAIGN  =   2, | 
|---|
| 65 |   RP_GAME      =   4 | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | //! A Struct that keeps track about a resource its name its Type, and so on | 
|---|
| 69 | struct Resource | 
|---|
| 70 | { | 
|---|
| 71 |   BaseObject*       pointer;           //!< Pointer to the Resource. | 
|---|
| 72 |   unsigned int      count;             //!< How many times this Resource has been loaded. | 
|---|
| 73 |  | 
|---|
| 74 |   std::string       name;              //!< Name of the Resource. | 
|---|
| 75 |   ResourceType      type;              //!< ResourceType of this Resource. | 
|---|
| 76 |   ResourcePriority  prio;              //!< The Priority of this resource. (This will only be increased) | 
|---|
| 77 |  | 
|---|
| 78 |   MultiType         param[3];          //!< The Parameters given to this Resource. | 
|---|
| 79 | }; | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | //! The ResourceManager is a class, that decides if a file/resource should be loaded | 
|---|
| 83 | /** | 
|---|
| 84 |  * If a file/resource was already loaded the resourceManager will | 
|---|
| 85 |  * return a pointer to the desired resource. | 
|---|
| 86 |  * Otherwise it will instruct the corresponding resource-loader to load, | 
|---|
| 87 |  * and receive the pointer to it. | 
|---|
| 88 |  * | 
|---|
| 89 |  * It does it by looking, if a desired file has already been loaded. | 
|---|
| 90 |  * There is also the possibility to check for some variables | 
|---|
| 91 |  */ | 
|---|
| 92 | class ResourceManager : public BaseObject | 
|---|
| 93 | { | 
|---|
| 94 |  public: | 
|---|
| 95 |   virtual ~ResourceManager(); | 
|---|
| 96 |   /** @returns a Pointer to the only object of this Class */ | 
|---|
| 97 |   inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager();  return singletonRef; }; | 
|---|
| 98 |  | 
|---|
| 99 |   bool setDataDir(const std::string& dataDir); | 
|---|
| 100 |   /** @returns the Name of the data directory */ | 
|---|
| 101 |   inline const std::string& getDataDir() const { return this->dataDir; }; | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 |   bool tryDataDir(const std::string& dataDir); | 
|---|
| 105 |   bool verifyDataDir(const std::string& fileInside); | 
|---|
| 106 |   bool addImageDir(const std::string& imageDir); | 
|---|
| 107 |  | 
|---|
| 108 |   bool cache(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, | 
|---|
| 109 |              const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); | 
|---|
| 110 |   BaseObject* copy(BaseObject* resourcePointer); | 
|---|
| 111 |  | 
|---|
| 112 |   BaseObject* load(const std::string& fileName, ResourcePriority prio = RP_NO, | 
|---|
| 113 |                    const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); | 
|---|
| 114 |   BaseObject* load(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, | 
|---|
| 115 |                    const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); | 
|---|
| 116 |   bool unload(BaseObject* pointer, ResourcePriority prio = RP_NO); | 
|---|
| 117 |   bool unload(Resource* resource, ResourcePriority = RP_NO); | 
|---|
| 118 |   bool unloadAllByPriority(ResourcePriority prio); | 
|---|
| 119 |  | 
|---|
| 120 |   Resource* locateResourceByInfo(const std::string& fileName, ResourceType type, | 
|---|
| 121 |                                  const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const; | 
|---|
| 122 |   Resource* locateResourceByPointer(const void* pointer) const; | 
|---|
| 123 |  | 
|---|
| 124 |   std::string toResourcableString(unsigned int i); | 
|---|
| 125 |   bool fromResourceableString(const std::string& resourceableString); | 
|---|
| 126 |   /** @returns the Count of Resources the ResourceManager handles */ | 
|---|
| 127 |   unsigned int resourceCount() const { return this->resourceList.size(); } | 
|---|
| 128 |  | 
|---|
| 129 |   void debug() const; | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |   // utility functions for handling files in and around the data-directory | 
|---|
| 133 |   static std::string getFullName(const std::string& fileName); | 
|---|
| 134 |   static bool isInDataDir(const std::string& fileName); | 
|---|
| 135 |  | 
|---|
| 136 |   static const char* ResourceTypeToChar(ResourceType type); | 
|---|
| 137 |   static ResourceType stringToResourceType(const std::string& resourceType); | 
|---|
| 138 |  | 
|---|
| 139 |  private: | 
|---|
| 140 |   ResourceManager(); | 
|---|
| 141 |   Resource* loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio, | 
|---|
| 142 |                          const MultiType& param0, const MultiType& param1, const MultiType& param2); | 
|---|
| 143 |  | 
|---|
| 144 |  private: | 
|---|
| 145 |   static ResourceManager*    singletonRef;       //!< singleton Reference | 
|---|
| 146 |  | 
|---|
| 147 |   std::string                dataDir;            //!< The Data Directory, where all relevant Data is stored. | 
|---|
| 148 |   std::vector<std::string>   imageDirs;          //!< A list of directories in which images are stored. | 
|---|
| 149 |  | 
|---|
| 150 |   std::vector<Resource*>     resourceList;       //!< The List of Resources, that has already been loaded. | 
|---|
| 151 |  | 
|---|
| 152 |   static const char*         resourceNames[RESOURCE_TYPE_SIZE]; | 
|---|
| 153 | }; | 
|---|
| 154 |  | 
|---|
| 155 | #endif /* _RESOURCE_MANAGER_H */ | 
|---|