-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResourceManager.cpp
234 lines (187 loc) · 5.73 KB
/
ResourceManager.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "pch.h"
#include "ResourceManager.h"
#include <iostream>
#include <filesystem>
namespace LeaderEngine
{
ResourceManager& ResourceManager::GetInstance()
{
static ResourceManager instance;
return instance;
}
ResourceManager::ResourceManager()
{
}
ResourceManager::~ResourceManager()
{
_textures.clear();
}
void ResourceManager::LoadResource(const std::string path)
{
YAML::Node resource = YAML::LoadFile(path);
if (resource["textures"])
LoadTextures(resource["textures"]);
if (resource["animations"])
LoadAnimation(resource["animations"]);
// close yaml file
resource.reset();
}
const sf::Texture& ResourceManager::getTexture(const std::string& id) const
{
//if (!_textures.contains(id))
//{
// std::cerr << "Texture with id " << id << " does not exist" << std::endl;
//}
sf::Texture placeholderTexture;
auto it = _textures.find(id);
if (it != _textures.end()) {
return it->second;
}
std::cout << "Fail to find texture with id " << id << std::endl;
return placeholderTexture;
//return _textures.at(id);
}
const std::vector<AnimationFrame>& ResourceManager::GetAnimation(const std::string& id) const
{
auto it = _animations.find(id);
if (it != _animations.end())
{
return it->second;
}
return std::vector<AnimationFrame>(); // return empty vector if not found
}
const std::string& ResourceManager::GetTextureName(const sf::Texture& texture) const
{
auto it = std::find_if(_textures.begin(), _textures.end(), [&texture](const std::pair<std::string, sf::Texture>& pair) // Find the texture with the same address
{
return &pair.second == &texture;
});
return it->first;
}
const std::string& ResourceManager::GetTextureNameFromAnimationName(const std::string& animationName) const
{
const auto& animation = GetAnimation(animationName);
if (!animation.empty())
{
return animation[0].textureId;
}
return "";
}
const std::string& ResourceManager::GetAnimationName(const std::vector<AnimationFrame>& animation) const
{
auto it = std::find_if(_animations.begin(), _animations.end(), [&animation](const std::pair<std::string, std::vector<AnimationFrame>>& pair) // Find the animation with the same address
{
return &pair.second == &animation;
});
return it->first;
}
unsigned int ResourceManager::GetTextureWidth(const std::string& textureName) const
{
const auto it = _textures.find(textureName);
if (it != _textures.end())
{
return it->second.getSize().x;
}
return 0;
}
unsigned int ResourceManager::GetTextureHeight(const std::string& textureName) const
{
const auto it = _textures.find(textureName);
if (it != _textures.end())
{
return it->second.getSize().y;
}
return 0;
}
sf::Vector2u ResourceManager::GetTextureSize(const std::string& textureName) const
{
const auto it = _textures.find(textureName);
if (it != _textures.end())
{
return it->second.getSize();
}
return sf::Vector2u(0, 0);
}
void ResourceManager::LoadTextures(const YAML::Node& textureNode)
{
for (const YAML::Node& texture : textureNode)
{
std::string id = texture["id"].as<std::string>();
std::string path = texture["path"].as<std::string>();
std::cout << "Loading texture " << id << " from " << path << std::endl;
if (!_textures[id].loadFromFile(path))
{
std::cerr << "Failed to load texture: " << path << std::endl;
}
//_textures[id] = std::move(tempTexture);
}
}
void ResourceManager::LoadAnimation(const YAML::Node& animationNode)
{
for (const YAML::Node& animation : animationNode)
{
std::string id = animation["id"].as<std::string>();
std::string textureId = animation["textureId"].as<std::string>();
std::vector<AnimationFrame> frames;
std::cout << "Loading animation " << id << " with texture " << textureId << std::endl;
for (const YAML::Node& frame : animation["frames"])
{
AnimationFrame frameData;
frameData.texture = &_textures[textureId];
frameData.textureRect = sf::IntRect(frame["x"].as<int>(), frame["y"].as<int>(), frame["width"].as<int>(), frame["height"].as<int>());
frameData.duration = frame["duration"].as<float>();
frameData.textureId = textureId;
frames.push_back(frameData);
}
/* if (!_animations[id].loadFromTexture(getTexture(textureId), frames))
{
std::cerr << "Failed to load animation: " << id << std::endl;
}*/
_animations[id] = std::move(frames);
}
}
//sf::Sprite& ResourceManager::LoadSprite(const std::string id)
//{
// _sprites[id] = sf::Sprite(_textures[id]);
//}
/*void ResourceManager::LoadShaders(const YAML::Node& shaderNode)
{
for (const YAML::Node& shader : shaderNode)
{
std::string name = shader["name"].as<std::string>();
std::string path = shader["path"].as<std::string>();
_resources["shaders"][name] = path;
}
}
void ResourceManager::LoadModels(const YAML::Node& modelNode)
{
for (const YAML::Node& model : modelNode)
{
std::string name = model["name"].as<std::string>();
std::string path = model["path"].as<std::string>();
_resources["models"][name] = path;
}
}
void ResourceManager::LoadMaterials(const YAML::Node& materialNode)
{
for (const YAML::Node& material : materialNode)
{
std::string name = material["name"].as<std::string>();
std::string path = material["path"].as<std::string>();
_resources["materials"][name] = path;
}
}
void ResourceManager::LoadResources(const std::string path)
{
YAML::Node resources = YAML::LoadFile(path);
LoadTextures(resources["textures"]);
LoadShaders(resources["shaders"]);
LoadModels(resources["models"]);
LoadMaterials(resources["materials"]);
}
std::string ResourceManager::GetResourcePath(const std::string type, const std::string name)
{
return _resources[type][name].as<std::string>();
}
}*/
}