-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.hpp
More file actions
101 lines (87 loc) · 3.26 KB
/
ResourceManager.hpp
File metadata and controls
101 lines (87 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <string>
#include <unordered_map>
#include <cstdint>
#include <memory>
#include <filesystem>
#include "Texture.hpp"
#include "Model.hpp"
class ResourceManager{
public:
static ResourceManager& get_instance(){
static ResourceManager instance;
return instance;
}
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
[[nodiscard]] std::shared_ptr<Texture> GetImageTexture(const std::string& path, bool gammaCorrection = false, const glm::vec3& colorScale = glm::vec3(1), bool invert = false);//maybe get image?
template <typename T, typename... Args>
requires std::is_base_of_v<Texture, T>
[[nodiscard]] std::shared_ptr<Texture> GetTexture(const std::string& name, Args&&... args){
if(name.empty())return nullptr;
auto it = texture_cache.find(name);
if(it != texture_cache.end()){
return it->second;
}
return texture_cache.try_emplace(name, std::make_shared<T>(std::forward<Args>(args)...)).first->second;
}
template <typename... Args>
[[nodiscard]] std::shared_ptr<Mesh> getMesh(Args&&... args){
std::shared_ptr<Mesh> newMesh = std::make_shared<Mesh>(std::forward<Args>(args)...);
for(auto&& ptr : meshCache){
if(*ptr == *newMesh){
return ptr;
}
}
meshCache.push_back(newMesh);
return newMesh;
}
template <typename T, typename... Args>
requires std::is_base_of_v<BVHBase<GeometricPrimitive>, T>
std::shared_ptr<Model> CacheModel(const std::string& name, const std::string& path, Args&&... args){
auto it = modelCache.find(name);
if(it != modelCache.end()){
return it->second;
}
std::shared_ptr<Model> ptr = std::make_shared<Model>(path);
ptr->BuildBlas<T>(std::forward<Args>(args)...);
return modelCache.try_emplace(name, ptr).first->second;
}
template <typename T, typename... Args>
requires std::is_base_of_v<BVHBase<GeometricPrimitive>, T>
std::shared_ptr<Model> CacheOverwriteModel(const std::string& name, const std::string& path, Args&&... args){
auto it = modelCache.find(name);
if(it != modelCache.end()){
modelCache.erase(it);
}
std::shared_ptr<Model> ptr = std::make_shared<Model>(path);
ptr->BuildBlas<T>(std::forward<Args>(args)...);
return modelCache.try_emplace(name, ptr).first->second;
}
[[nodiscard]] std::shared_ptr<Model> GetModel(const std::string& name){
auto it = modelCache.find(name);
if(it != modelCache.end()){
return it->second;
}
return nullptr;
}
void releaseTextureCache(){
texture_cache.clear();
}
void releaseModelCache(){
modelCache.clear();
}
void releaseMeshCache(){
meshCache.clear();
}
private:
ResourceManager() = default;
~ResourceManager() {
releaseTextureCache();
releaseMeshCache();
releaseModelCache();
}
std::vector<std::shared_ptr<Mesh>> meshCache;
std::unordered_map<std::string, std::shared_ptr<Model>> modelCache;
std::unordered_map<std::string, std::shared_ptr<Texture>> texture_cache;
};