NWengine 0.9
Loading...
Searching...
No Matches
Asset.h
1#pragma once
2
3#define NW_DECL_RES_LIST(key,AssetClass) static std::unordered_map<key, AssetClass> resList;
4#define NW_IMPL_RES_LIST(key,AssetClass) std::unordered_map<key, AssetClass> AssetClass::resList;
5
9class Asset {
10public:
11 int _usageCounter = 0;
12
18 virtual Asset* GetFromCache(void* identifier);
19
27 virtual Asset* LoadFromFileOrGetFromCache(void* identifier, const char* path, void* data);
28
35 virtual Asset* LoadFromFile(const char* path, void* data = nullptr);
36
43 virtual Asset* LoadFromBuffer(void* buffer, void* data);
44
52 virtual Asset* LoadFromBufferOrGetFromCache(void* identifier, void* buffer, void* data);
53
57 virtual void Clean();
58};
59
60template<typename T>
61class Loader {
62private:
63 T _asset;
64public:
70 T* GetFromCache(void* identifier) {
71 return (T*)_asset.GetFromCache();
72 }
73
81 T* LoadFromFileOrGetFromCache(void* identifier, const char* path, void* data) {
82 return (T*)_asset.LoadFromFileOrGetFromCache(identifier, path, data);
83 }
84
91 T* LoadFromFile(const char* path, void* data = nullptr) {
92 return (T*)_asset.LoadFromFile(path, data);
93 }
94
101 T* LoadFromBuffer(void* buffer, void* data) {
102 return (T*)_asset.LoadFromBuffer(buffer, data);
103 }
104
112 T* LoadFromBufferOrGetFromCache(void* identifier, void* buffer, void* data) {
113 return (T*)_asset.LoadFromBufferOrGetFromCache(identifier, buffer, data);
114 }
115};
116
117template<typename T, typename T_Identifier>
118T_Identifier GetIDWithAsset(T assetChildInstancePtr) {
119 auto list = assetChildInstancePtr->resList;
120 auto iter = list.begin();
121 for (iter; iter != list.end(); ++iter) {
122 if (&iter->second == assetChildInstancePtr) {
123 return iter->first;
124 }
125 }
126 return {"", 0};
127};
128
129template<typename ResListType, typename T>
130void EraseRes(const T& identifier) {
131 ResListType::resList.erase(identifier);
132}
133
134template<typename T, typename IDType>
135T* InsertRes(IDType id) {
136 T* temp = (T*)T::GetResFromCache(id);
137 if (temp != nullptr) {
138 temp->Clean();
139 }
140 T::resList.emplace(id, T());
141}
142
143template<typename T>
144T LoadAssetFromFile(const char* path, void* data = nullptr) {
145 T loader l;
146 return = (T*)l.LoadFromFile(path, data);
147}
148
149template <typename T>
150void hashCombine(size_t& s, const T& v) {
151 std::hash<T> h;
152 s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
153};
The base class for all assets.
Definition Asset.h:9
virtual Asset * GetFromCache(void *identifier)
Get the asset from the cache based on the identifier.
Definition Asset.cpp:3
virtual Asset * LoadFromFile(const char *path, void *data=nullptr)
Load the asset from file based on the file path.
Definition Asset.cpp:22
virtual Asset * LoadFromFileOrGetFromCache(void *identifier, const char *path, void *data)
Load the asset from file or get it from the cache based on the identifier and file path.
Definition Asset.cpp:4
virtual Asset * LoadFromBuffer(void *buffer, void *data)
Load the asset from a buffer.
Definition Asset.cpp:23
virtual void Clean()
Clean up the asset.
Definition Asset.cpp:24
virtual Asset * LoadFromBufferOrGetFromCache(void *identifier, void *buffer, void *data)
Load the asset from a buffer or get it from the cache based on the identifier.
Definition Asset.cpp:15
Definition Asset.h:61
T * GetFromCache(void *identifier)
Get the asset from the cache based on the identifier.
Definition Asset.h:70
T * LoadFromBuffer(void *buffer, void *data)
Load the asset from a buffer.
Definition Asset.h:101
T * LoadFromFileOrGetFromCache(void *identifier, const char *path, void *data)
Load the asset from file or get it from the cache based on the identifier and file path.
Definition Asset.h:81
T * LoadFromBufferOrGetFromCache(void *identifier, void *buffer, void *data)
Load the asset from a buffer or get it from the cache based on the identifier.
Definition Asset.h:112
T * LoadFromFile(const char *path, void *data=nullptr)
Load the asset from file based on the file path.
Definition Asset.h:91