1 | /*! |
---|
2 | * @file resource.h |
---|
3 | * @brief Definition of a Resource. |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _RESOURCE_H |
---|
7 | #define _RESOURCE_H |
---|
8 | |
---|
9 | #include "base_object.h" |
---|
10 | #include "multi_type.h" |
---|
11 | #include <string> |
---|
12 | #include <map> |
---|
13 | #include <vector> |
---|
14 | |
---|
15 | |
---|
16 | namespace Loading |
---|
17 | { |
---|
18 | // FORWARD DECLARATION |
---|
19 | |
---|
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 | }; |
---|
34 | |
---|
35 | |
---|
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 | bool reload(); |
---|
56 | std::string fileName; |
---|
57 | |
---|
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) |
---|
61 | |
---|
62 | MultiType param[3]; //!< The Parameters given to this Resource. |
---|
63 | }; |
---|
64 | |
---|
65 | |
---|
66 | public: |
---|
67 | Resource(const std::string& fileName = "", ResourcePriority prio = RP_LEVEL); |
---|
68 | virtual ~Resource(); |
---|
69 | |
---|
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); |
---|
76 | |
---|
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 | |
---|
84 | // reloads the resource. |
---|
85 | virtual bool reload(); |
---|
86 | // unloads the sharedResource (from this Resource) |
---|
87 | virtual bool unload(); |
---|
88 | |
---|
89 | // checks if a resource was already loaded. |
---|
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); |
---|
94 | |
---|
95 | // raises the priority can only be raised |
---|
96 | void raisePriority(ResourcePriority prio); |
---|
97 | |
---|
98 | /** @returns the referenceCount of the shared resource behind this resource or 0 if no internal Resource is stored */ |
---|
99 | unsigned int referenceCount() const { return (this->resource)? this->resource->referenceCount : 0; }; |
---|
100 | |
---|
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(); |
---|
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 | |
---|
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 | |
---|
122 | protected: |
---|
123 | SharedResource* resource; //!< The internal Resource, this Resource is keeping (at the moment). |
---|
124 | |
---|
125 | /** A Map of all stored Resources. */ |
---|
126 | static std::map<ClassID, std::vector<SharedResource> > storedResources; |
---|
127 | }; |
---|
128 | |
---|
129 | } |
---|
130 | #endif /* _RESOURCE_H */ |
---|