Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6645 was 6645, checked in by bensch, 18 years ago

orxonox/trunk: totally remastered the ResourceManager.
Now it takes MultiTypes instead of (void*) as parameters

  1. This is TypeSafe
  2. This is easier to use
  3. This makes much more sense, and is objectOriented

also made some minor adjustments to the MultiType, some comparisons

also fixed the loading in all the Other classes like material md2 and so on

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#include "multi_type.h"
24
25#include <vector>
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  MultiType         param[3];          //!< The Parameters given to this Resource.
77};
78
79
80//! The ResourceManager is a class, that decides if a file/resource should be loaded
81/**
82 * If a file/resource was already loaded the resourceManager will
83 * return a pointer to the desired resource.
84 * Otherwise it will instruct the corresponding resource-loader to load,
85 * and receive the pointer to it.
86 *
87 * It does it by looking, if a desired file has already been loaded.
88 * There is also the possibility to check for some variables
89 */
90class ResourceManager : public BaseObject
91{
92 public:
93  virtual ~ResourceManager();
94  /** @returns a Pointer to the only object of this Class */
95  inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager();  return singletonRef; };
96
97  bool setDataDir(const char* dataDir);
98  /** @returns the Name of the data directory */
99  inline const char* getDataDir() const { return this->dataDir; };
100
101
102  bool tryDataDir(const char* dataDir);
103  bool verifyDataDir(const char* fileInside);
104  bool addImageDir(const char* imageDir);
105
106  void cache(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO,
107             const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
108
109  BaseObject* load(const char* fileName, ResourcePriority prio = RP_NO,
110                   const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
111  BaseObject* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO,
112                   const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
113  bool unload(void* pointer, ResourcePriority prio = RP_NO);
114  bool unload(Resource* resource, ResourcePriority = RP_NO);
115  bool unloadAllByPriority(ResourcePriority prio);
116
117  Resource* locateResourceByInfo(const char* fileName, ResourceType type,
118                                 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const;
119  Resource* locateResourceByPointer(const void* pointer) const;
120
121  void debug() const;
122
123
124  // utility functions for handling files in and around the data-directory
125  static bool isDir(const char* directory);
126  static bool isFile(const char* fileName);
127  static bool touchFile(const char* fileName);
128  static bool deleteFile(const char* fileName);
129  static char* homeDirCheck(const char* fileName);
130  static char* getFullName(const char* fileName);
131  static bool isInDataDir(const char* fileName);
132
133  static const char* ResourceTypeToChar(ResourceType type);
134
135
136 private:
137  ResourceManager();
138  Resource* loadResource(const char* fileName, ResourceType type, ResourcePriority prio,
139                         const MultiType& param0, const MultiType& param1, const MultiType& param2);
140
141 private:
142  static ResourceManager*  singletonRef;       //!< singleton Reference
143
144  char*                    dataDir;            //!< The Data Directory, where all relevant Data is stored.
145  std::vector<char*>       imageDirs;          //!< A list of directories in which images are stored.
146
147  std::vector<Resource*>   resourceList;       //!< The List of Resources, that has already been loaded.
148};
149
150#endif /* _RESOURCE_MANAGER_H */
Note: See TracBrowser for help on using the repository browser.