Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

c++ - Should a map or unordered_map be used within an asset manager?

I'm learning about game development and SFML with the help of this book, SFML Essentials, which shows how to build an asset manager for handling resources. Here is the code from the book:

AssetManager.h

#ifndef ASSET_MANAGER_H
#define ASSET_MANAGER_H

#include <map>
#include "SFML/Graphics.hpp"

class AssetManager
{
public:
    AssetManager();

    static sf::Texture& GetTexture(std::string const& filename);

private:
    std::map<std::string, sf::Texture> m_Textures;

    // AssetManager is a singleton, so only one instance can exist at a time
    // sInstance holds a static pointer to the single manager instance
    static AssetManager* sInstance;
};

#endif // !ASSET_MANAGER_H

AssetManager.cpp

#include "AssetsManager.h"
#include <assert.h>

AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager()
{
    // Only allow one AssetManager to exist
    // Otherwise throw an exception
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::GetTexture(std::string const& filename)
{
    auto& texMap = sInstance->m_Textures;

    // See if the texture is already loaded
    auto pairFound = texMap.find(filename);
    // If loaded, return the texture
    if (pairFound != texMap.end())
    {
        return pairFound->second;
    }
    else // Else, load the texture and return it
    {
        // Create an element in the texture map
        auto& texture = texMap[filename];
        texture.loadFromFile(filename);
        return texture;
    }
}

The author uses a map to cache textures, but wouldn't it be better to use an unordered_map to help improve lookup times? What would be the advantages/disadvantages of each in a full scale game?

question from:https://stackoverflow.com/questions/65661918/should-a-map-or-unordered-map-be-used-within-an-asset-manager

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...