从功能的角度看,ID到文件路径的内部和外部映射 [英] Internal vs External Mapping of IDs to file paths from function perspective
问题描述
一个sf::Sprite
可以与多个sf::Sprite
对象共享同一个sf::Texture
对象,因此我想将 Texture Manager 设计为多次不具有相同的纹理.
An sf::Sprite
can share the same sf::Texture
object with multiple sf::Sprite
objects, so I want to design a Texture Manager to not the same texture multiple times.
我正在使用一个标识符来引用纹理. TextureID
将纹理映射到与包含该纹理的文件相对应的文件路径:
I'm using an identifier for referring to textures. A TextureID
maps a texture to the file path corresponding to the file that contains the texture:
std::filesystem::path mapIdToFilepath(TextureID id);
在TextureManager
中,我想要一个成员函数loadTexture()
,我在想:
In TextureManager
I want to have a member function loadTexture()
, I'm thinking either:
-
具有
loadTexture()
接受TextureID
:
sf::Texture& TextureManager::loadTexture(TextureID id);
然后,该函数必须通过调用mapIdToFilepath()
在内部内部将标识符TextureID
转换为纹理文件路径.
The function must then internally translate the identifier TextureID
to the texture file path by calling mapIdToFilepath()
.
或接受纹理的文件路径作为参数:
or accepting the texture's file path as argument:
sf::Texture& TextureManager::loadTexture(std::filesystem::path filepath);
调用此函数的客户端代码必须将标识符TextureID
转换为纹理文件路径.但是,此函数不必知道mapIdToFilepath
的存在,因为映射是外部完成的.
The client code that calls this function must translate the identifier TextureID
to the texture file path. However, this function doesn't have to know about the existence of mapIdToFilepath
because the mapping is done externally.
我认为第一种方法更方便,但是它将TextureManager
耦合到mapIdToFilepath()
,因为它在内部进行TextureID
到文件路径的转换.
I think the first approach is more convenient but it couples TextureManager
to mapIdToFilepath()
because it internally does the TextureID
to file path translation.
从建筑角度看,在考虑这两种不同方法时我应该牢记哪些方面?
From an architectural perspective, what are the aspects I should keep in mind when considering these two different approaches?
推荐答案
我认为,从广义上讲,您正在寻找类似Asset Manager之类的东西来仅加载一次您的资产(无论是纹理,声音还是字体)并在任何地方使用它们.您可以使用我很久以前使用过的东西.制作一个资产管理器类,该类将在地图中包含您的纹理.像这样:
I assume, in a broad aspect, you're looking for something like an Asset Manager to load your assets (be it textures, sounds or fonts) only once and use them everywhere. You can use something I used long ago. Make an Asset Manager class which will have your textures in a map. Something like this:
class TexManager
{
public:
TexManager* tex() //to access your assets without creating multiple instances of this class
{
static Texmanager texMan;
return &texMan;
}
void loadTexture(std::string name, std::string filepath) //to load textures
{
sf::Texture tmp;
tmp.loadFromFile(filepath);
texMap.insert(std::make_pair(name, tmp));
}
sf::Texture& getTexture(st::string name) //to retrieve textures
{
return texMap.at("name");
}
private:
std::map<std::string, sf::Texture> texMap;
}
请不要按原样使用代码,因为您需要为加载失败或地图的键值不正确添加条件.如果您还有其他需要,或者我的回答有误,请告诉我.谢谢你. :)
Don't use the code as it is since you'll need to add conditionals for failed loading or incorrect key value for the map. Please let me know if you need something else or if I have answered incorrectly. Thank you. :)
这篇关于从功能的角度看,ID到文件路径的内部和外部映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!