对SFML资源管理器使用C ++模板 [英] Using C++ templating for SFML Resource Manager

查看:185
本文介绍了对SFML资源管理器使用C ++模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在想办法为我的SFML游戏创建一个资源管理器。两者都需要模板(我是模板化的新手),但我更喜欢这些解决方案之一,即使我不知道如何使它工作。

I'm thinking of two solutions to create a resource manager for my SFML-Game. Both require templating (I'm new to templating), but I prefer one of those solutions, even if I don't really know how to make it work.

我'd喜欢这样做:

std::map<string, class> resources;

template<class T> T getResource(std::string name){
    return (T) resources.find(name);
}

换句话说,我想将所有资源存储在一个类,并且只有一个函数来获取资源,这个函数应该总是返回资源的类型 - 所以,为了加载资源,我可以做:

In other words, I'd like to have all my resources stored in one class, and that there is only one function to get a resource, and this function should always return the type of the resource - So that, in order to load a resource, I can just do:

Texture texture = resourceManager.getResource("mytexture.png");

但我不知道如何让这个工作。

But I can't figure out how to get this working.

我的第二种方法是对一个类进行模板化,然后实例化多个资源管理器,每个资源类型一个。

My second approach would be to template a class, and then instantiate multiple resource managers, one for each type of resource.

template<class Res> class ResourceManager {
    std::map<string, Res> resources;
    ...
    Res get(std::string name){
        return resources.find(name); //Just to visualize. Of course there would be null checks etc in the real method
    }
}

在我看来,第二个解决方案看起来更清洁的代码,但第一个将更容易使用;我可以只是使方法静态,所以我不必担心解耦。

In my opinion, the second "solution" looks way cleaner code-wise, but the first one would be easier to use; And I could just make the methods static so I won't have to worry about decoupling.

我真的不知道应该把管理器的实例第二种方法。可能我会使实例静态,并将它们放在我的主类中,但这似乎是正确的。

I really don't know where I should put the instances of the managers from the second approach. Probably I would make the instances static and put them in my main class, but that just doesnt seem right.

推荐答案

它会变得很杂乱,因为你不能在同一个地图中有不同的资源类型。他们没有共同的基类。

While nice, it will get really messy because you cannot have different resource types in the same map. They don't have a common base class. You don't have that many different resources though, so I'd suggest you just spell them out.

我个人认为字符串是标识符,因为编译器不能找到拼写错误,因此我使用的是枚举:

Personally, I hate strings as identifiers because the compiler cannot find typos, so I'm using an enum:

enum ResourceIdentifier
{
    // TEXTURES
    LoadingScreenBackground,
    FireAnimation,
    SmokeAnimation,
    FloorTile,
    // ...

    // FONTS
    MainFont
};

class ResourceManager
{
private:
    std::map<ResourceIdentifier, std::shared_ptr<sf::Texture>> m_Textures;
    std::map<ResourceIdentifier, std::shared_ptr<sf::Font>> m_Fonts;

public:
    std::shared_ptr<sf::Texture> LoadTexture(ResourceIdentifier id, const sf::String& file);
    std::shared_ptr<sf::Font> LoadFont(ResourceIdentifier id, const sf::String& file);

    std::shared_ptr<sf::Texture> GetTexture(ResourceIdentifier id) const;
    std::shared_ptr<sf::Font> GetFont(ResourceIdentifier id) const;
};

这篇关于对SFML资源管理器使用C ++模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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