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 "multi_type.h" |
---|
24 | |
---|
25 | #include <vector> |
---|
26 | |
---|
27 | //! An eumerator for different fileTypes the resourceManager supports |
---|
28 | typedef enum ResourceType |
---|
29 | { |
---|
30 | #ifndef NO_MODEL |
---|
31 | OBJ, //!< loading .obj file |
---|
32 | PRIM, //!< loading primitive model |
---|
33 | MD2, //!< loading md2-file |
---|
34 | #endif /* NO_MODEL */ |
---|
35 | #ifndef NO_TEXT |
---|
36 | TTF, //!< loading a TrueTypeFont |
---|
37 | #endif /* NO_TEXT */ |
---|
38 | #ifndef NO_AUDIO |
---|
39 | WAV, //!< loading wav |
---|
40 | MP3, //!< loading mp3 |
---|
41 | OGG, //!< loading ogg |
---|
42 | #endif /* NO_AUDIO */ |
---|
43 | #ifndef NO_TEXTURES |
---|
44 | IMAGE, //!< loading an image |
---|
45 | #endif /* NO_TEXTURES */ |
---|
46 | #ifndef NO_SHADERS |
---|
47 | SHADER, //!< openGL-shader program |
---|
48 | #endif /* NO_SHADERS */ |
---|
49 | RESOURCE_TYPE_SIZE |
---|
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 | std::string 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 | MultiType param[3]; //!< The Parameters given to this Resource. |
---|
78 | }; |
---|
79 | |
---|
80 | |
---|
81 | //! The ResourceManager is a class, that decides if a file/resource should be loaded |
---|
82 | /** |
---|
83 | * If a file/resource was already loaded the resourceManager will |
---|
84 | * return a pointer to the desired resource. |
---|
85 | * Otherwise it will instruct the corresponding resource-loader to load, |
---|
86 | * and receive the pointer to it. |
---|
87 | * |
---|
88 | * It does it by looking, if a desired file has already been loaded. |
---|
89 | * There is also the possibility to check for some variables |
---|
90 | */ |
---|
91 | class ResourceManager : public BaseObject |
---|
92 | { |
---|
93 | public: |
---|
94 | virtual ~ResourceManager(); |
---|
95 | /** @returns a Pointer to the only object of this Class */ |
---|
96 | inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager(); return singletonRef; }; |
---|
97 | |
---|
98 | bool setDataDir(const std::string& dataDir); |
---|
99 | /** @returns the Name of the data directory */ |
---|
100 | inline const std::string& getDataDir() const { return this->dataDir; }; |
---|
101 | |
---|
102 | |
---|
103 | bool tryDataDir(const std::string& dataDir); |
---|
104 | bool verifyDataDir(const std::string& fileInside); |
---|
105 | bool addImageDir(const std::string& imageDir); |
---|
106 | |
---|
107 | bool cache(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
108 | const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); |
---|
109 | BaseObject* copy(BaseObject* resourcePointer); |
---|
110 | |
---|
111 | BaseObject* load(const std::string& fileName, ResourcePriority prio = RP_NO, |
---|
112 | const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); |
---|
113 | BaseObject* load(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
114 | const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); |
---|
115 | bool unload(BaseObject* pointer, ResourcePriority prio = RP_NO); |
---|
116 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
---|
117 | bool unloadAllByPriority(ResourcePriority prio); |
---|
118 | |
---|
119 | Resource* locateResourceByInfo(const std::string& fileName, ResourceType type, |
---|
120 | const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const; |
---|
121 | Resource* locateResourceByPointer(const void* pointer) const; |
---|
122 | |
---|
123 | std::string toResourcableString(unsigned int i); |
---|
124 | bool fromResourceableString(const std::string& resourceableString); |
---|
125 | /** @returns the Count of Resources the ResourceManager handles */ |
---|
126 | unsigned int resourceCount() const { return this->resourceList.size(); } |
---|
127 | |
---|
128 | void debug() const; |
---|
129 | |
---|
130 | |
---|
131 | // utility functions for handling files in and around the data-directory |
---|
132 | static bool isDir(const std::string& directory); |
---|
133 | static bool isFile(const std::string& fileName); |
---|
134 | static bool touchFile(const std::string& fileName); |
---|
135 | static bool deleteFile(const std::string& fileName); |
---|
136 | static std::string homeDirCheck(const std::string& fileName); |
---|
137 | static std::string getFullName(const std::string& fileName); |
---|
138 | static bool isInDataDir(const std::string& fileName); |
---|
139 | static std::string getAbsDir(const std::string& fileName); |
---|
140 | |
---|
141 | static const char* ResourceTypeToChar(ResourceType type); |
---|
142 | static ResourceType stringToResourceType(const std::string& resourceType); |
---|
143 | |
---|
144 | private: |
---|
145 | ResourceManager(); |
---|
146 | Resource* loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio, |
---|
147 | const MultiType& param0, const MultiType& param1, const MultiType& param2); |
---|
148 | |
---|
149 | private: |
---|
150 | static ResourceManager* singletonRef; //!< singleton Reference |
---|
151 | |
---|
152 | std::string dataDir; //!< The Data Directory, where all relevant Data is stored. |
---|
153 | std::vector<std::string> imageDirs; //!< A list of directories in which images are stored. |
---|
154 | |
---|
155 | std::vector<Resource*> resourceList; //!< The List of Resources, that has already been loaded. |
---|
156 | |
---|
157 | static const char* resourceNames[RESOURCE_TYPE_SIZE]; |
---|
158 | }; |
---|
159 | |
---|
160 | #endif /* _RESOURCE_MANAGER_H */ |
---|