1 | /*! |
---|
2 | \file resource_manager.h |
---|
3 | \brief 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 | |
---|
11 | #ifndef _RESOURCE_MANAGER_H |
---|
12 | #define _RESOURCE_MANAGER_H |
---|
13 | |
---|
14 | #include "base_object.h" |
---|
15 | |
---|
16 | #include "stdlibincl.h" |
---|
17 | |
---|
18 | // FORWARD DEFINITION |
---|
19 | template<class T> class tList; |
---|
20 | |
---|
21 | //! An eumerator for different fileTypes the resourceManager supports |
---|
22 | typedef enum ResourceType { OBJ, |
---|
23 | PRIM, |
---|
24 | MD2, |
---|
25 | WAV, |
---|
26 | MP3, |
---|
27 | OGG, |
---|
28 | TTF, |
---|
29 | IMAGE }; |
---|
30 | |
---|
31 | //! An enumerator for different UNLOAD-types. |
---|
32 | /** |
---|
33 | RP_NO: will be unloaded on request |
---|
34 | RP_LEVEL: will be unloaded at the end of a Level |
---|
35 | RP_CAMPAIGN: will be unloaded at the end of a Campaign |
---|
36 | RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox) |
---|
37 | */ |
---|
38 | typedef enum ResourcePriority { RP_NO = 0, |
---|
39 | RP_LEVEL = 1, |
---|
40 | RP_CAMPAIGN = 2, |
---|
41 | RP_GAME = 4 }; |
---|
42 | |
---|
43 | //! A Struct that keeps track about A resource its name its Type, and so on |
---|
44 | struct Resource |
---|
45 | { |
---|
46 | void* pointer; //!< Pointer to the Resource. |
---|
47 | int count; //!< How many times this Resource has been loaded. |
---|
48 | |
---|
49 | char* name; //!< Name of the Resource. |
---|
50 | ResourceType type; //!< ResourceType of this Resource. |
---|
51 | ResourcePriority prio; //!< The Priority of this resource. (This will only be increased) |
---|
52 | |
---|
53 | // more specific |
---|
54 | float modelSize; |
---|
55 | unsigned int ttfSize; |
---|
56 | unsigned char ttfColorR; |
---|
57 | unsigned char ttfColorG; |
---|
58 | unsigned char ttfColorB; |
---|
59 | char* skinFileName; |
---|
60 | }; |
---|
61 | |
---|
62 | |
---|
63 | //! The ResourceManager is a class, that decides if a file/resource should be loaded |
---|
64 | /** |
---|
65 | If a file/resource was already loaded the resourceManager will |
---|
66 | return a void pointer to the desired resource. |
---|
67 | Otherwise it will instruct the corresponding resource-loader to load, |
---|
68 | and receive the pointer to it. |
---|
69 | |
---|
70 | It does it by looking, if a desired file has already been loaded. |
---|
71 | */ |
---|
72 | class ResourceManager : public BaseObject |
---|
73 | { |
---|
74 | public: |
---|
75 | static ResourceManager* getInstance(); |
---|
76 | virtual ~ResourceManager(); |
---|
77 | |
---|
78 | bool setDataDir(const char* dataDir); |
---|
79 | /** \returns the Name of the data directory */ |
---|
80 | inline const char* getDataDir(void) const {return this->dataDir;} |
---|
81 | |
---|
82 | bool checkDataDir(const char* fileInside); |
---|
83 | bool addImageDir(const char* imageDir); |
---|
84 | void* load(const char* fileName, ResourcePriority prio = RP_NO, |
---|
85 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
86 | void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
87 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
88 | bool unload(void* pointer, ResourcePriority prio = RP_NO); |
---|
89 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
---|
90 | bool unloadAllByPriority(ResourcePriority prio); |
---|
91 | |
---|
92 | void debug(void); |
---|
93 | |
---|
94 | // utility functions of this class |
---|
95 | static bool isDir(const char* directory); |
---|
96 | static bool isFile(const char* fileName); |
---|
97 | static bool touchFile(const char* fileName); |
---|
98 | static bool deleteFile(const char* fileName); |
---|
99 | static char* homeDirCheck(const char* fileName); |
---|
100 | static char* getFullName(const char* fileName); |
---|
101 | |
---|
102 | private: |
---|
103 | ResourceManager(); |
---|
104 | static ResourceManager* singletonRef; |
---|
105 | |
---|
106 | tList<Resource>* resourceList; //!< The List of Resources, that has already been loaded. |
---|
107 | char* dataDir; //!< The Data Directory, where all relevant Data is stored. |
---|
108 | tList<char>* imageDirs; //!< A list of directories in which images are stored. |
---|
109 | |
---|
110 | |
---|
111 | Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3); |
---|
112 | Resource* locateResourceByPointer(const void* pointer); |
---|
113 | |
---|
114 | }; |
---|
115 | |
---|
116 | #endif /* _RESOURCE_MANAGER_H */ |
---|