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