Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Asset.h
Go to the documentation of this file.
1#pragma once
3#include <optional>
4#include <cstdint>
5#include <entt/entt.hpp>
6#include <unordered_map>
8
13namespace Asset
14{
21 enum CpuLoadingState : uint8_t {
40 };
41
50 {
51 public:
52 explicit AssetBase(bool initializeDefaultEntity = false) :
53 loadingState( initializeDefaultEntity ? LOADING : UNLOADED )
54 , mainRegistry( Ecs::RegistryManager::get() )
55 , data( initializeDefaultEntity ? mainRegistry.create() : entt::null )
56 {
57 };
58 virtual ~AssetBase();
59
67 [[nodiscard]] CpuLoadingState getLoadingState();
68
75 void requestLoad();
76
83 virtual void unload();
84
89 entt::entity getEntity() const;
90
91 protected:
92 virtual void updateLoadingState()
93 {
94 }
96
97 entt::registry& mainRegistry;
98 entt::entity data;
99 };
100
102 {
104 }
105
111
113 {
115 data = mainRegistry.create();
116 }
117
118 inline void AssetBase::unload()
119 {
120 if (data != entt::null)
121 mainRegistry.destroy(data);
123 }
124
125 inline entt::entity AssetBase::getEntity() const
126 {
127 return data;
128 }
129
140 template <typename Key, typename AssetClass>
142 public:
143 AssetManager() = default;
144 virtual ~AssetManager() = default;
145
155 template <typename... Args>
156 void declare( Key path, Args &&... args );
165 void add(Key path, AssetClass* asset);
166
172 bool exists(Key path);
173
182 bool isDeclared(Key path);
183
203 std::optional<AssetClass*> getAsset(const Key& path);
204
211 virtual void clear();
212
221 template<typename Func>
222 void forEachAsset(Func&& func) const {
223 for (const auto& [key, asset] : assets) {
224 if (asset != nullptr) {
225 func(key, asset);
226 }
227 }
228 }
229
230 private:
231 std::unordered_map<Key, AssetClass*> assets;
232 };
233
234 template<typename Key, typename AssetClass>
235 template<typename... Args>
236 void AssetManager<Key, AssetClass>::declare(Key path, Args&&... args) {
237 assets[path] = new AssetClass(std::forward<Args>(args)...);
238 }
239
240 template<typename Key, typename AssetClass>
241 void AssetManager<Key, AssetClass>::add(Key path, AssetClass *asset) {
242 TRACY_ZONE_SCOPED_NAMED( "Added Asset to Asset Manager" );
243 assert(asset != nullptr); // use declare for that
244 assets[path] = asset;
245 }
246
247 template<typename Key, typename AssetClass>
249 return assets.contains(path) && assets[path] != nullptr;
250 }
251
252 template <typename Key, typename AssetClass>
254 {
255 return assets.contains(path);
256 }
257
258 template <typename Key, typename AssetClass>
260 if (!assets.contains(path)) return UNLOADED;
261 if (assets.contains(path) && assets[path] == nullptr) return LOADING;
262 return assets[path]->getLoadingState();
263 }
264
265 template<typename Key, typename AssetClass>
266 std::optional<AssetClass *> AssetManager<Key, AssetClass>::getAsset(const Key& path) {
267 TRACY_ZONE_SCOPED_NAMED("Retrieve Asset");
268 if (!assets.contains(path)) return std::nullopt;
269 return assets[path];
270 }
271
272 template <typename Key, typename AssetClass>
274 {
275 TRACY_ZONE_SCOPED_NAMED( "Unloading data" );
276 for (auto& asset : assets)
277 {
278 TRACY_ZONE_SCOPED_NAMED("Deleting loaded data");
279 delete asset.second;
280 asset.second = nullptr;
281 }
282 assets.clear();
283 }
284} // namespace Asset
#define TRACY_ZONE_SCOPED_NAMED(name)
void requestLoad()
If this asset is in the UNLOADED state it will get added to the assets to load.
Definition Asset.h:112
virtual ~AssetBase()
Definition Asset.h:101
virtual void unload()
Base function for unloading the ecs data.
Definition Asset.h:118
entt::entity data
Definition Asset.h:98
entt::entity getEntity() const
Getter for data.
Definition Asset.h:125
AssetBase(bool initializeDefaultEntity=false)
Definition Asset.h:52
virtual void updateLoadingState()
Definition Asset.h:92
CpuLoadingState loadingState
Definition Asset.h:95
CpuLoadingState getLoadingState()
Gets the loading state of this asset.
Definition Asset.h:106
entt::registry & mainRegistry
Definition Asset.h:97
std::unordered_map< Key, AssetClass * > assets
Definition Asset.h:231
void declare(Key path, Args &&... args)
Adds an asset to the list in its unloaded state.
Definition Asset.h:236
CpuLoadingState getAssetLoadingState(const Key &path)
Gets a loading state for an asset. If the asset is not yet declared it will return that it is UNLOADE...
Definition Asset.h:259
std::optional< AssetClass * > getAsset(const Key &path)
Try's to get an asset. If the asset does not exist (can be checked with exists) it will return a std:...
Definition Asset.h:266
bool isDeclared(Key path)
Checks if an asset has been declared. If it exists it is also declared.
Definition Asset.h:253
void forEachAsset(Func &&func) const
Iterates over all assets and calls the provided callback for each.
Definition Asset.h:222
bool exists(Key path)
Checks if an asset already exists. This means it is declared and there is content.
Definition Asset.h:248
AssetManager()=default
virtual void clear()
Clears out all resources.
Definition Asset.h:273
void add(Key path, AssetClass *asset)
Adds or overwrites a previously declared asset at a key.
Definition Asset.h:241
virtual ~AssetManager()=default
Classes which are related to asset loading are mostly stored in this namespace.
CpuLoadingState
State for assets in the asset loading process.
Definition Asset.h:21
@ LOADED
All components of the asset have been loaded, and it is ready to be used.
Definition Asset.h:39
@ REQUESTED_LOAD
An asset has been requested to load and is waiting for an asset pipeline to start working on it.
Definition Asset.h:31
@ LOADING
While the asset is currently being processed in the asset pipeline.
Definition Asset.h:35
@ UNLOADED
When the asset is not loaded yet but the frame is already existing for the heavy data to be loaded in...
Definition Asset.h:26
Data structs for the Entity Component System.