如何解决应该在资产管理器中使用地图还是 unordered_map?
我在本书 SFML Essentials 的帮助下学习游戏开发和 SFML,该书展示了如何构建用于处理资源的资产管理器。这是书中的代码:
资产管理器.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;
}
}
作者使用map 来缓存纹理,但使用unordered_map 来帮助改进查找时间不是更好吗?在正式游戏中,每种方法的优点/缺点是什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。