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 DEFINITION |
---|
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 | }; |
---|
48 | |
---|
49 | //! An enumerator for different UNLOAD-types. |
---|
50 | /** |
---|
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) |
---|
55 | */ |
---|
56 | typedef enum ResourcePriority |
---|
57 | { |
---|
58 | RP_NO = 0, |
---|
59 | RP_LEVEL = 1, |
---|
60 | RP_CAMPAIGN = 2, |
---|
61 | RP_GAME = 4 |
---|
62 | }; |
---|
63 | |
---|
64 | //! A Struct that keeps track about A resource its name its Type, and so on |
---|
65 | struct Resource |
---|
66 | { |
---|
67 | void* pointer; //!< Pointer to the Resource. |
---|
68 | int count; //!< How many times this Resource has been loaded. |
---|
69 | |
---|
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) |
---|
73 | |
---|
74 | // more specific |
---|
75 | float modelSize; //!< the size of the model (OBJ/PRIM) |
---|
76 | #ifndef NO_MODEL |
---|
77 | char* skinFileName; //!< skinFileName (MD2) |
---|
78 | #endif /* NO_MODEL */ |
---|
79 | #ifndef NO_TEXT |
---|
80 | unsigned int ttfSize; //!< the size of the ttf-font (TTF) |
---|
81 | #endif /* NO_TEXT */ |
---|
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 void 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 | */ |
---|
94 | class ResourceManager : public BaseObject |
---|
95 | { |
---|
96 | public: |
---|
97 | virtual ~ResourceManager(); |
---|
98 | /** @returns a Pointer to the only object of this Class */ |
---|
99 | inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager(); return singletonRef; }; |
---|
100 | |
---|
101 | bool setDataDir(const char* dataDir); |
---|
102 | /** @returns the Name of the data directory */ |
---|
103 | inline const char* getDataDir() const {return this->dataDir;} |
---|
104 | |
---|
105 | bool checkDataDir(const char* fileInside); |
---|
106 | bool addImageDir(const char* imageDir); |
---|
107 | void* load(const char* fileName, ResourcePriority prio = RP_NO, |
---|
108 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
109 | void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
110 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
111 | bool unload(void* pointer, ResourcePriority prio = RP_NO); |
---|
112 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
---|
113 | bool unloadAllByPriority(ResourcePriority prio); |
---|
114 | |
---|
115 | void debug() const; |
---|
116 | |
---|
117 | |
---|
118 | // utility functions of this class |
---|
119 | static bool isDir(const char* directory); |
---|
120 | static bool isFile(const char* fileName); |
---|
121 | static bool touchFile(const char* fileName); |
---|
122 | static bool deleteFile(const char* fileName); |
---|
123 | static char* homeDirCheck(const char* fileName); |
---|
124 | static char* getFullName(const char* fileName); |
---|
125 | |
---|
126 | private: |
---|
127 | ResourceManager(); |
---|
128 | |
---|
129 | Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3); |
---|
130 | Resource* locateResourceByPointer(const void* pointer); |
---|
131 | |
---|
132 | private: |
---|
133 | static ResourceManager* singletonRef; //!< singleton Reference |
---|
134 | |
---|
135 | tList<Resource>* resourceList; //!< The List of Resources, that has already been loaded. |
---|
136 | char* dataDir; //!< The Data Directory, where all relevant Data is stored. |
---|
137 | tList<char>* imageDirs; //!< A list of directories in which images are stored. |
---|
138 | |
---|
139 | }; |
---|
140 | |
---|
141 | #endif /* _RESOURCE_MANAGER_H */ |
---|