Files
amethyst/include/amethyst/assets.hpp
tobid7 078af99ae5 Upload all my tests
- Add Count<> to AssetManager
- Initialize pCode in Shader
- Rename linearAllocator to LinearAllocator
- Add Construct and destroy to linear allocator
- Add Shader unloader (fixes atexit crash i guess)
-
2025-11-30 21:57:01 +01:00

66 lines
1.5 KiB
C++
Executable File

#pragma once
#include <amethyst/id.hpp>
#include <amethyst/texture.hpp>
#include <amethyst/types.hpp>
namespace Amy {
class AssetMgr {
public:
AssetMgr() = default;
~AssetMgr() = default;
void AutoLoad(const ID& name, ksr path);
void Add(const ID& id, Asset* v) {
if (pAssets.count(id)) {
throw std::runtime_error("[amy]: assets: " + id.GetName() +
" already exists!");
}
pAssets[id] = v;
}
void Remove(const ID& id) {
if (pAssets.count(id)) {
pAssets.erase(id);
}
}
template <typename T>
T* Get(const ID& id) {
auto r = pAssets.find(id);
if (r == pAssets.end()) {
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
}
if (auto v = dynamic_cast<T*>(r->second)) {
return v;
} else {
throw std::runtime_error(id.GetName() + " is not of type " +
typeid(T).name());
}
}
template <typename T>
bool IsType(const ID& id) {
auto r = pAssets.find(id);
if (r == pAssets.end()) {
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
}
return dynamic_cast<T*>(r->second) != nullptr;
}
template <typename T>
size_t Count() const {
size_t ret = 0;
for (auto& it : pAssets) {
if (dynamic_cast<T*>(it.second)) {
ret++;
}
}
return ret;
}
private:
std::map<ID, Asset*> pAssets;
};
} // namespace Amy