Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: copy Resources from the ResourceManger (this is not as safe as with smart-pointers :( )

maybe implement:
http://ootips.org/yonat/4dev/counted_ptr.h

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