我应该在哪里存储游戏的纹理?

时间:2018-11-10 16:20:48

标签: c++ sfml game-development

我目前正在使用C ++在SFML中创建游戏,我想知道:纹理位置的最佳做法是什么?我应该将其存储在我的项目中吗?还是我的可执行文件?甚至在“文档”文件夹中?从理论上讲,当游戏发行时,最有效的是什么,因为它将不仅包含项目,还包含其编译和构建版本?

1 个答案:

答案 0 :(得分:1)

大多数常见游戏发行版的纹理都在Media文件夹或类似文件夹内。 在该文件夹内还放置了声音,音乐和其他内容,通常放置在单独的文件夹中。

它们不能成为可执行文件的一部分(据我所知)。更重要的是如何在代码中管理那些纹理,这应该是一种有效的方法。 如果您有兴趣,我已经添加了有关如何执行此操作的说明。

TL DR

根据我自己制作一些小型电子游戏的经验,我发现最好使用资源持有者。这是任何重载资源(纹理,音乐,声音甚至字体)的通用容器。

其背后的主要思想是拥有一个将键(ID)与资源相关联的映射。 您可能想存储各种资源,所以最好创建一个通用类。

基本实现:

template <typename Resource, typename Identifier>
class ResourceHolder
{
public:
    void load(Identifier id, const std::string& filename){
        // Create and load resource
        std::unique_ptr<Resource> resource(new Resource());
        if (!resource->loadFromFile(filename))
            throw std::runtime_error("ResourceHolder::load - Failed to load " + filename);

        // If loading successful, insert resource to map
        insertResource(id, std::move(resource));
    }

    Resource& get(Identifier id){
        auto found = mResourceMap.find(id);
        assert(found != mResourceMap.end());

        return *found->second;
    }

    const Resource& get(Identifier id) const {
        auto found = mResourceMap.find(id);
        assert(found != mResourceMap.end());

        return *found->second;
    }


protected:
    void insertResource(Identifier id, std::unique_ptr<Resource> resource){
        // Insert and check success
        auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
        assert(inserted.second);
    }


protected:
    std::map<Identifier, std::unique_ptr<Resource>> mResourceMap;
};

我通常更喜欢将.hpp.cpp分开保存,但是我合并了它们以避免发布更长(甚至更长)的帖子。

为使内容整洁有用,最好使用 Resource Identifier 标头文件,您可以在其中声明资源持有者的类型以及资源标识符。

// Forward declaration of SFML classes
namespace sf
{
    class Texture;
    // If you need, you can use other SFML classes into your holders the same way
    //class Font;
    //class SoundBuffer;
}

namespace Textures
{
    enum ID
    {
        TitleScreen,
        LoadingScreen,
        GameOverScreen,
        Title,
        Controls,
        GUI,
        TileMap,
        Player,
        Enemy,
        Key,
        PlayerMods
    };
}

// Forward declaration and a few type definitions
template <typename Resource, typename Identifier>
class ResourceHolder;

typedef ResourceHolder<sf::Texture, Textures::ID>   TextureHolder;
//typedef ResourceHolder<sf::Font, Fonts::ID>           FontHolder;
//typedef ResourceHolder<sf::SoundBuffer, Sounds::ID>   SoundHolder;

作为使用示例,如果您有一个类似Game类的类(只要您的应用程序正在运行,该类就会被加载),您可以这样做:

class Game {
public:
    Game() :
        _window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Game")
    {
        // EXAMPLES
        //_fonts.load(Fonts::Main, FONTS_FOLDER + "font.ttf");
        //_musics.load(Musics::Game, MUSIC_FOLDER + "main.ogg");
        //_musics.get(Musics::Game).setLoop(true);
        //_sounds.load(Sounds::Key, SOUNDS_FOLDER + "key.wav");


        _textures.load(Textures::TitleScreen, TEXTURES_FOLDER + "titlescreen.png");

        // More code ...
    }

    void run(){
        // Your game loop: process inputs, update and render until you close
    }
private:
    void update(sf::Time dt){
        // ...
    }

    void processInput(){
        // ...
    }

    void render(){
        _window.clear(sf::Color::Black);

        // Here you can use your resources to draw
        sf::Sprite sp(_textures.get(Textures::TitleScreen));
        _window.draw(sp);

        _window.display();
    }

    sf::RenderWindow _window;
    TextureHolder _textures;
    //FontHolder _fonts;
    //SoundHolder _sounds;
};

此方法的关键是使持有人处于始终加载的类中,并将您的持有人作为指针或引用传递。做到这一点的另一种好方法是拥有一个Context类,该类将这些指针保存并分组为一个类,并将该上下文用作所有类的参数(即使是复制,因为它是一个轻量级的类)这将需要资源:

struct Context
{
    Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts, MusicHolder& musics, SoundHolder& sounds);

    sf::RenderWindow*   window;
    TextureHolder*      textures;
    FontHolder*         fonts;
    MusicHolder*        musics;
    SoundHolder*        sounds;
};

您可以在此处找到有关此信息的更多信息:SFML Game Development,此实现的来源。

相关问题