Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/util/loading/resource_manager.h @ 8590

Last change on this file since 8590 was 8590, checked in by patrick, 18 years ago

bsp: md3 integrated to the resource manager

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