从函数角度看 ID 到文件路径的内部与外部映射 [英] Internal vs External Mapping of IDs to file paths from function perspective

查看:21
本文介绍了从函数角度看 ID 到文件路径的内部与外部映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆