[4597] | 1 | /*! |
---|
[3655] | 2 | \file resource_manager.h |
---|
[4836] | 3 | * The Resource Manager checks if a file/resource is loaded. |
---|
[3329] | 4 | |
---|
[4597] | 5 | If a file/resource was already loaded the resourceManager will |
---|
[3655] | 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. |
---|
[4534] | 9 | |
---|
| 10 | it is possible to compile the resource Manager without some modules by |
---|
[4597] | 11 | just adding the compile flag -D.... |
---|
[4534] | 12 | (NO_MODEL) |
---|
| 13 | (NO_AUDIO) |
---|
| 14 | (NO_TEXT) |
---|
| 15 | (NO_TEXTURES) |
---|
[3245] | 16 | */ |
---|
[1853] | 17 | |
---|
[3655] | 18 | #ifndef _RESOURCE_MANAGER_H |
---|
| 19 | #define _RESOURCE_MANAGER_H |
---|
[1853] | 20 | |
---|
[3543] | 21 | #include "base_object.h" |
---|
[1853] | 22 | |
---|
[4381] | 23 | #include "stdlibincl.h" |
---|
| 24 | |
---|
[4597] | 25 | // FORWARD DEFINITION |
---|
[3911] | 26 | template<class T> class tList; |
---|
[3660] | 27 | |
---|
[4462] | 28 | //! An eumerator for different fileTypes the resourceManager supports |
---|
[4597] | 29 | typedef enum ResourceType |
---|
| 30 | { |
---|
[4534] | 31 | #ifndef NO_MODEL |
---|
| 32 | OBJ, //!< loading .obj file |
---|
| 33 | PRIM, //!< loading primitive model |
---|
| 34 | MD2, //!< loading md2-file |
---|
| 35 | #endif /* NO_MODEL */ |
---|
[4653] | 36 | #ifndef NO_TEXT |
---|
| 37 | TTF, //!< loading a TrueTypeFont |
---|
| 38 | #endif /* NO_TEXT */ |
---|
[4534] | 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 | }; |
---|
[4462] | 48 | |
---|
[4597] | 49 | //! An enumerator for different UNLOAD-types. |
---|
[3660] | 50 | /** |
---|
[4462] | 51 | RP_NO: will be unloaded on request |
---|
| 52 | RP_LEVEL: will be unloaded at the end of a Level |
---|
| 53 | RP_CAMPAIGN: will be unloaded at the end of a Campaign |
---|
| 54 | RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox) |
---|
[3660] | 55 | */ |
---|
[4597] | 56 | typedef enum ResourcePriority |
---|
| 57 | { |
---|
| 58 | RP_NO = 0, |
---|
| 59 | RP_LEVEL = 1, |
---|
| 60 | RP_CAMPAIGN = 2, |
---|
| 61 | RP_GAME = 4 |
---|
| 62 | }; |
---|
[3543] | 63 | |
---|
[3660] | 64 | //! A Struct that keeps track about A resource its name its Type, and so on |
---|
[3658] | 65 | struct Resource |
---|
| 66 | { |
---|
[4465] | 67 | void* pointer; //!< Pointer to the Resource. |
---|
| 68 | int count; //!< How many times this Resource has been loaded. |
---|
[4597] | 69 | |
---|
[4465] | 70 | char* name; //!< Name of the Resource. |
---|
| 71 | ResourceType type; //!< ResourceType of this Resource. |
---|
| 72 | ResourcePriority prio; //!< The Priority of this resource. (This will only be increased) |
---|
[3790] | 73 | |
---|
| 74 | // more specific |
---|
[4637] | 75 | float modelSize; //!< the size of the model (OBJ/PRIM) |
---|
[4534] | 76 | #ifndef NO_MODEL |
---|
| 77 | char* skinFileName; //!< skinFileName (MD2) |
---|
| 78 | #endif /* NO_MODEL */ |
---|
| 79 | #ifndef NO_TEXT |
---|
[4465] | 80 | unsigned int ttfSize; //!< the size of the ttf-font (TTF) |
---|
| 81 | unsigned char ttfColorR; //!< red Color (TTF) |
---|
| 82 | unsigned char ttfColorG; //!< green Color (TTF) |
---|
| 83 | unsigned char ttfColorB; //!< blue Color (TTF) |
---|
[4534] | 84 | #endif /* NO_TEXT */ |
---|
[3658] | 85 | }; |
---|
[3543] | 86 | |
---|
[2036] | 87 | |
---|
[3655] | 88 | //! The ResourceManager is a class, that decides if a file/resource should be loaded |
---|
[3329] | 89 | /** |
---|
[4597] | 90 | If a file/resource was already loaded the resourceManager will |
---|
[3655] | 91 | return a void 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. |
---|
[3329] | 96 | */ |
---|
[4597] | 97 | class ResourceManager : public BaseObject |
---|
[3655] | 98 | { |
---|
[1904] | 99 | public: |
---|
[3655] | 100 | virtual ~ResourceManager(); |
---|
[4836] | 101 | /** @returns a Pointer to the only object of this Class */ |
---|
[4746] | 102 | inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager(); return singletonRef; }; |
---|
[1853] | 103 | |
---|
[3883] | 104 | bool setDataDir(const char* dataDir); |
---|
[4836] | 105 | /** @returns the Name of the data directory */ |
---|
[4746] | 106 | inline const char* getDataDir() const {return this->dataDir;} |
---|
[4166] | 107 | |
---|
[4091] | 108 | bool checkDataDir(const char* fileInside); |
---|
[4370] | 109 | bool addImageDir(const char* imageDir); |
---|
[3790] | 110 | void* load(const char* fileName, ResourcePriority prio = RP_NO, |
---|
[4597] | 111 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
[3790] | 112 | void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
[4597] | 113 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
[3672] | 114 | bool unload(void* pointer, ResourcePriority prio = RP_NO); |
---|
| 115 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
---|
| 116 | bool unloadAllByPriority(ResourcePriority prio); |
---|
[4597] | 117 | |
---|
[4746] | 118 | void debug() const; |
---|
[3245] | 119 | |
---|
[4597] | 120 | |
---|
[3983] | 121 | // utility functions of this class |
---|
| 122 | static bool isDir(const char* directory); |
---|
[4032] | 123 | static bool isFile(const char* fileName); |
---|
| 124 | static bool touchFile(const char* fileName); |
---|
| 125 | static bool deleteFile(const char* fileName); |
---|
[4166] | 126 | static char* homeDirCheck(const char* fileName); |
---|
| 127 | static char* getFullName(const char* fileName); |
---|
[3983] | 128 | |
---|
[3245] | 129 | private: |
---|
[3655] | 130 | ResourceManager(); |
---|
[3245] | 131 | |
---|
[4465] | 132 | Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3); |
---|
[4597] | 133 | Resource* locateResourceByPointer(const void* pointer); |
---|
[3655] | 134 | |
---|
[4465] | 135 | private: |
---|
| 136 | static ResourceManager* singletonRef; //!< singleton Reference |
---|
[3672] | 137 | |
---|
[4465] | 138 | tList<Resource>* resourceList; //!< The List of Resources, that has already been loaded. |
---|
| 139 | char* dataDir; //!< The Data Directory, where all relevant Data is stored. |
---|
| 140 | tList<char>* imageDirs; //!< A list of directories in which images are stored. |
---|
| 141 | |
---|
[1853] | 142 | }; |
---|
| 143 | |
---|
[3655] | 144 | #endif /* _RESOURCE_MANAGER_H */ |
---|