[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(); |
---|
| 55 | std::string fileName; |
---|
[7195] | 56 | |
---|
[7229] | 57 | unsigned int referenceCount; //!< How many times this Resource has been loaded. |
---|
| 58 | ClassID type; //!< ResourceType of this Resource. |
---|
| 59 | ResourcePriority prio; //!< The Priority of this resource. (can only be increased, so noone else will delete this) |
---|
[7195] | 60 | |
---|
[7229] | 61 | MultiType param[3]; //!< The Parameters given to this Resource. |
---|
| 62 | }; |
---|
[1853] | 63 | |
---|
| 64 | |
---|
[7229] | 65 | public: |
---|
| 66 | Resource(const std::string& fileName = "", ResourcePriority prio = RP_LEVEL); |
---|
| 67 | virtual ~Resource(); |
---|
[3245] | 68 | |
---|
[7229] | 69 | // looks if the resource was already loaded and (if not) loads it. |
---|
| 70 | virtual bool load(std::string& fileName = "", |
---|
| 71 | ResourcePriority prio = RP_LEVEL, |
---|
| 72 | const MultiType& param0 = MT_NULL, |
---|
| 73 | const MultiType& param1 = MT_NULL, |
---|
| 74 | const MultiType& param2 = MT_NULL); |
---|
| 75 | // reloads the resource. |
---|
| 76 | virtual bool reload() { }; |
---|
| 77 | // unloads the sharedResource (from this Resource) |
---|
| 78 | virtual bool unload(); |
---|
[3245] | 79 | |
---|
[7229] | 80 | // checks if a resource was already loaded. |
---|
| 81 | bool isLoaded(std::string& fileName, |
---|
| 82 | const MultiType& param0 = MT_NULL, |
---|
| 83 | const MultiType& param1 = MT_NULL, |
---|
| 84 | const MultiType& param2 = MT_NULL); |
---|
[7195] | 85 | |
---|
[7229] | 86 | // raises the priority can only be raised |
---|
| 87 | void raisePriority(ResourcePriority prio); |
---|
| 88 | /** @returns the referenceCount of the shared resource behind this resource or 0 if no internal Resource is stored */ |
---|
| 89 | unsigned int referenceCount() const { return (this->resource)? this->resource->referenceCount : 0; }; |
---|
[1853] | 90 | |
---|
[7229] | 91 | protected: |
---|
| 92 | const SharedResource* findResource(std::string& fileName, |
---|
| 93 | const MultiType& param0 = MT_NULL, |
---|
| 94 | const MultiType& param1 = MT_NULL, |
---|
| 95 | const MultiType& param2 = MT_NULL); |
---|
| 96 | |
---|
| 97 | protected: |
---|
| 98 | SharedResource* resource; //!< The internal Resource, this Resource is keeping (at the moment). |
---|
| 99 | |
---|
| 100 | static std::map<ClassID, std::vector<SharedResource> > storedResources; |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | } |
---|
[7193] | 104 | #endif /* _RESOURCE_H */ |
---|