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, //!< loading .obj file |
---|
23 | PRIM, //!< loading primitive model |
---|
24 | MD2, //!< loading md2-file |
---|
25 | WAV, //!< loading wav |
---|
26 | MP3, //!< loading mp3 |
---|
27 | OGG, //!< loading ogg |
---|
28 | TTF, //!< loading a TrueTypeFont |
---|
29 | IMAGE }; //!< loading an 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; //!< the size of the model (OBJ/PRIM) |
---|
55 | unsigned int ttfSize; //!< the size of the ttf-font (TTF) |
---|
56 | unsigned char ttfColorR; //!< red Color (TTF) |
---|
57 | unsigned char ttfColorG; //!< green Color (TTF) |
---|
58 | unsigned char ttfColorB; //!< blue Color (TTF) |
---|
59 | char* skinFileName; //!< skinFileName (MD2) |
---|
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 | virtual ~ResourceManager(); |
---|
76 | |
---|
77 | static ResourceManager* getInstance(); |
---|
78 | |
---|
79 | bool setDataDir(const char* dataDir); |
---|
80 | /** \returns the Name of the data directory */ |
---|
81 | inline const char* getDataDir(void) const {return this->dataDir;} |
---|
82 | |
---|
83 | bool checkDataDir(const char* fileInside); |
---|
84 | bool addImageDir(const char* imageDir); |
---|
85 | void* load(const char* fileName, ResourcePriority prio = RP_NO, |
---|
86 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
87 | void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, |
---|
88 | void* param1 = NULL, void* param2 = NULL, void* param3 = NULL); |
---|
89 | bool unload(void* pointer, ResourcePriority prio = RP_NO); |
---|
90 | bool unload(Resource* resource, ResourcePriority = RP_NO); |
---|
91 | bool unloadAllByPriority(ResourcePriority prio); |
---|
92 | |
---|
93 | void debug(void); |
---|
94 | |
---|
95 | |
---|
96 | // utility functions of this class |
---|
97 | static bool isDir(const char* directory); |
---|
98 | static bool isFile(const char* fileName); |
---|
99 | static bool touchFile(const char* fileName); |
---|
100 | static bool deleteFile(const char* fileName); |
---|
101 | static char* homeDirCheck(const char* fileName); |
---|
102 | static char* getFullName(const char* fileName); |
---|
103 | |
---|
104 | private: |
---|
105 | ResourceManager(); |
---|
106 | |
---|
107 | Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3); |
---|
108 | Resource* locateResourceByPointer(const void* pointer); |
---|
109 | |
---|
110 | private: |
---|
111 | static ResourceManager* singletonRef; //!< singleton Reference |
---|
112 | |
---|
113 | tList<Resource>* resourceList; //!< The List of Resources, that has already been loaded. |
---|
114 | char* dataDir; //!< The Data Directory, where all relevant Data is stored. |
---|
115 | tList<char>* imageDirs; //!< A list of directories in which images are stored. |
---|
116 | |
---|
117 | }; |
---|
118 | |
---|
119 | #endif /* _RESOURCE_MANAGER_H */ |
---|