[4838] | 1 | /*! |
---|
[7193] | 2 | * @file resource.h |
---|
[7195] | 3 | * @brief Definition of a Resource. |
---|
[3245] | 4 | */ |
---|
[1853] | 5 | |
---|
[7193] | 6 | #ifndef _RESOURCE_H |
---|
| 7 | #define _RESOURCE_H |
---|
[1853] | 8 | |
---|
[3543] | 9 | #include "base_object.h" |
---|
[7193] | 10 | #include "multi_type.h" |
---|
[7195] | 11 | #include <string> |
---|
[7229] | 12 | #include <map> |
---|
| 13 | #include <vector> |
---|
[1853] | 14 | |
---|
[3543] | 15 | |
---|
[7229] | 16 | namespace Loading |
---|
| 17 | { |
---|
| 18 | // FORWARD DECLARATION |
---|
[3543] | 19 | |
---|
[7229] | 20 | //! An enumerator for different (UN)LOAD-types. |
---|
| 21 | /** |
---|
| 22 | * RP_NO: will be unloaded on request |
---|
| 23 | * RP_LEVEL: will be unloaded at the end of a Level |
---|
| 24 | * RP_CAMPAIGN: will be unloaded at the end of a Campaign |
---|
| 25 | * RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox) |
---|
| 26 | */ |
---|
| 27 | typedef enum ResourcePriority |
---|
| 28 | { |
---|
| 29 | RP_NO = 0, |
---|
| 30 | RP_LEVEL = 1, |
---|
| 31 | RP_CAMPAIGN = 2, |
---|
| 32 | RP_GAME = 3 |
---|
| 33 | }; |
---|
[2036] | 34 | |
---|
[7195] | 35 | |
---|
[7229] | 36 | //! A Resource is an Object, that can be loaded from Disk |
---|
| 37 | /** |
---|
| 38 | * A Resource can cahnge the internal type, meaning it is a |
---|
| 39 | * SmartPointer, that keeps track of the InternalCount of the |
---|
| 40 | * Resource (SharedResource). |
---|
| 41 | */ |
---|
| 42 | class Resource : virtual public BaseObject |
---|
| 43 | { |
---|
| 44 | protected: |
---|
| 45 | /** |
---|
| 46 | * This is a Class for the internal handling of the Resource. |
---|
| 47 | * Any Resource keeps exactly one of these Classes, so it can |
---|
| 48 | * reference and move it around, and keep track of how many |
---|
| 49 | * references of it are stored. |
---|
| 50 | */ |
---|
| 51 | class SharedResource |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | SharedResource(); |
---|
[7240] | 55 | bool reload(); |
---|
[7229] | 56 | std::string fileName; |
---|
[7195] | 57 | |
---|
[7229] | 58 | unsigned int referenceCount; //!< How many times this Resource has been loaded. |
---|
| 59 | ClassID type; //!< ResourceType of this Resource. |
---|
| 60 | ResourcePriority prio; //!< The Priority of this resource. (can only be increased, so noone else will delete this) |
---|
[7195] | 61 | |
---|
[7229] | 62 | MultiType param[3]; //!< The Parameters given to this Resource. |
---|
| 63 | }; |
---|
[1853] | 64 | |
---|
| 65 | |
---|
[7229] | 66 | public: |
---|
| 67 | Resource(const std::string& fileName = "", ResourcePriority prio = RP_LEVEL); |
---|
[7240] | 68 | virtual ~Resource(); |
---|
[3245] | 69 | |
---|
[7229] | 70 | // looks if the resource was already loaded and (if not) loads it. |
---|
| 71 | virtual bool load(std::string& fileName = "", |
---|
| 72 | ResourcePriority prio = RP_LEVEL, |
---|
| 73 | const MultiType& param0 = MT_NULL, |
---|
| 74 | const MultiType& param1 = MT_NULL, |
---|
| 75 | const MultiType& param2 = MT_NULL); |
---|
[7240] | 76 | |
---|
[7237] | 77 | virtual bool cache(std::string& fileName = "", |
---|
| 78 | ResourcePriority prio = RP_LEVEL, |
---|
| 79 | const MultiType& param0 = MT_NULL, |
---|
| 80 | const MultiType& param1 = MT_NULL, |
---|
| 81 | const MultiType& param2 = MT_NULL); |
---|
| 82 | |
---|
| 83 | |
---|
[7229] | 84 | // reloads the resource. |
---|
[7240] | 85 | virtual bool reload(); |
---|
[7229] | 86 | // unloads the sharedResource (from this Resource) |
---|
| 87 | virtual bool unload(); |
---|
[3245] | 88 | |
---|
[7229] | 89 | // checks if a resource was already loaded. |
---|
[7240] | 90 | static bool isLoaded(std::string& fileName, |
---|
| 91 | const MultiType& param0 = MT_NULL, |
---|
| 92 | const MultiType& param1 = MT_NULL, |
---|
| 93 | const MultiType& param2 = MT_NULL); |
---|
[7195] | 94 | |
---|
[7229] | 95 | // raises the priority can only be raised |
---|
| 96 | void raisePriority(ResourcePriority prio); |
---|
[7237] | 97 | |
---|
[7229] | 98 | /** @returns the referenceCount of the shared resource behind this resource or 0 if no internal Resource is stored */ |
---|
[7240] | 99 | unsigned int referenceCount() const { return (this->resource)? this->resource->referenceCount : 0; }; |
---|
[1853] | 100 | |
---|
[7240] | 101 | static bool reloadResourcesByType(ClassID ResourceType); |
---|
| 102 | static bool reloadAllResources(); |
---|
| 103 | |
---|
| 104 | static bool unloadResourcesByType(ResourcePriority prio); |
---|
| 105 | static bool unloadAllResources(); |
---|
| 106 | |
---|
| 107 | static void debug(); |
---|
[7229] | 108 | protected: |
---|
| 109 | const SharedResource* findResource(std::string& fileName, |
---|
| 110 | const MultiType& param0 = MT_NULL, |
---|
| 111 | const MultiType& param1 = MT_NULL, |
---|
| 112 | const MultiType& param2 = MT_NULL); |
---|
| 113 | |
---|
[7237] | 114 | private: |
---|
| 115 | static SharedResource* locateResourceByPointer(const void* sharedResource); |
---|
| 116 | |
---|
| 117 | static SharedResource* locateResourceByInfo(std::string& fileName, |
---|
| 118 | const MultiType& param0 = MT_NULL, |
---|
| 119 | const MultiType& param1 = MT_NULL, |
---|
| 120 | const MultiType& param2 = MT_NULL); |
---|
| 121 | |
---|
[7229] | 122 | protected: |
---|
[7237] | 123 | SharedResource* resource; //!< The internal Resource, this Resource is keeping (at the moment). |
---|
[7229] | 124 | |
---|
[7237] | 125 | /** A Map of all stored Resources. */ |
---|
| 126 | static std::map<ClassID, std::vector<SharedResource> > storedResources; |
---|
[7229] | 127 | }; |
---|
| 128 | |
---|
| 129 | } |
---|
[7193] | 130 | #endif /* _RESOURCE_H */ |
---|