| 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 | */ |
|---|
| 17 | |
|---|
| 18 | #ifndef _RESOURCE_MANAGER_H |
|---|
| 19 | #define _RESOURCE_MANAGER_H |
|---|
| 20 | |
|---|
| 21 | #include "base_object.h" |
|---|
| 22 | |
|---|
| 23 | #include "stdlibincl.h" |
|---|
| 24 | |
|---|
| 25 | // FORWARD DECLARATION |
|---|
| 26 | template<class T> class tList; |
|---|
| 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 | }; |
|---|
| 51 | |
|---|
| 52 | //! An enumerator for different UNLOAD-types. |
|---|
| 53 | /** |
|---|
| 54 | RP_NO: will be unloaded on request |
|---|
| 55 | RP_LEVEL: will be unloaded at the end of a Level |
|---|
| 56 | RP_CAMPAIGN: will be unloaded at the end of a Campaign |
|---|
| 57 | RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox) |
|---|
| 58 | */ |
|---|
| 59 | typedef enum ResourcePriority |
|---|
| 60 | { |
|---|
| 61 | RP_NO = 0, |
|---|
| 62 | RP_LEVEL = 1, |
|---|
| 63 | RP_CAMPAIGN = 2, |
|---|
| 64 | RP_GAME = 4 |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | //! A Struct that keeps track about A resource its name its Type, and so on |
|---|
| 68 | struct Resource |
|---|
| 69 | { |
|---|
| 70 | BaseObject* pointer; //!< Pointer to the Resource. |
|---|
| 71 | unsigned int count; //!< How many times this Resource has been loaded. |
|---|
| 72 | |
|---|
| 73 | char* name; //!< Name of the Resource. |
|---|
| 74 | ResourceType type; //!< ResourceType of this Resource. |
|---|
| 75 | ResourcePriority prio; //!< The Priority of this resource. (This will only be increased) |
|---|
| 76 | |
|---|
| 77 | // more specific |
|---|
| 78 | float modelSize; //!< the size of the model (OBJ/PRIM) |
|---|
| 79 | #ifndef NO_MODEL |
|---|
| 80 | char* secFileName; //!< a seconf fileName |
|---|
| 81 | #endif /* NO_MODEL */ |
|---|
| 82 | #ifndef NO_TEXT |
|---|
| 83 | unsigned int ttfSize; //!< the size of the ttf-font (TTF) |
|---|
| 84 | #endif /* NO_TEXT */ |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | //! The ResourceManager is a class, that decides if a file/resource should be loaded |
|---|
| 89 | /** |
|---|
| 90 | * If a file/resource was already loaded the resourceManager will |
|---|
| 91 | * return a pointer to the desired resource. |
|---|
| 92 | * Otherwise it will instruct the corresponding resource-loader to load, |
|---|
| 93 | * and receive the pointer to it. |
|---|
| 94 | * |
|---|
| 95 | * It does it by looking, if a desired file has already been loaded. |
|---|
| 96 | * There is also the possibility to check for some variables |
|---|
| 97 | */ |
|---|
| 98 | class ResourceManager : public BaseObject |
|---|
| 99 | { |
|---|
| 100 | public: |
|---|
| 101 | virtual ~ResourceManager(); |
|---|
| 102 | /** @returns a Pointer to the only object of this Class */ |
|---|
| 103 | inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager(); return singletonRef; }; |
|---|
| 104 | |
|---|
| 105 | bool setDataDir(const char* dataDir); |
|---|
| 106 | /** @returns the Name of the data directory */ |
|---|
| 107 | inline const char* getDataDir() const { return this->dataDir; }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | bool tryDataDir(const char* dataDir); |
|---|
| 111 | bool verifyDataDir(const char* fileInside); |
|---|
| 112 | bool addImageDir(const char* imageDir); |
|---|
| 113 | BaseObject* load(const char* fileName, ResourcePriority prio = RP_NO, |
|---|
| 114 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
|---|
| 115 | BaseObject* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, |
|---|
| 116 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
|---|
| 117 | bool unload(void* pointer, ResourcePriority prio = RP_NO); |
|---|
| 118 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
|---|
| 119 | bool unloadAllByPriority(ResourcePriority prio); |
|---|
| 120 | |
|---|
| 121 | void debug() const; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | // utility functions for handling files in and around the data-directory |
|---|
| 125 | static bool isDir(const char* directory); |
|---|
| 126 | static bool isFile(const char* fileName); |
|---|
| 127 | static bool touchFile(const char* fileName); |
|---|
| 128 | static bool deleteFile(const char* fileName); |
|---|
| 129 | static char* homeDirCheck(const char* fileName); |
|---|
| 130 | static char* getFullName(const char* fileName); |
|---|
| 131 | static bool isInDataDir(const char* fileName); |
|---|
| 132 | |
|---|
| 133 | static const char* ResourceTypeToChar(ResourceType type); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | private: |
|---|
| 137 | ResourceManager(); |
|---|
| 138 | |
|---|
| 139 | Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3); |
|---|
| 140 | Resource* locateResourceByPointer(const void* pointer); |
|---|
| 141 | |
|---|
| 142 | private: |
|---|
| 143 | static ResourceManager* singletonRef; //!< singleton Reference |
|---|
| 144 | |
|---|
| 145 | char* dataDir; //!< The Data Directory, where all relevant Data is stored. |
|---|
| 146 | tList<Resource>* resourceList; //!< The List of Resources, that has already been loaded. |
|---|
| 147 | tList<char>* imageDirs; //!< A list of directories in which images are stored. |
|---|
| 148 | |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | #endif /* _RESOURCE_MANAGER_H */ |
|---|