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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…