Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/resource_manager.h @ 6640

Last change on this file since 6640 was 6640, checked in by bensch, 19 years ago

trunk: ResourceManager: make the ResourceManager more modular

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