mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-05-28 16:43:29 +00:00
cache system for textures
This commit is contained in:
@@ -9,40 +9,33 @@
|
||||
|
||||
namespace RAY {
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<::Texture>> Texture::_textureCache;
|
||||
|
||||
Texture::Texture(const std::string &filename):
|
||||
_texture(LoadTexture(filename.c_str())),
|
||||
_texture(fetchTextureInCache(filename)),
|
||||
_resourcePath(filename)
|
||||
{
|
||||
}
|
||||
|
||||
Texture::Texture(const Texture &texture):
|
||||
_texture(LoadTexture(texture._resourcePath.c_str())),
|
||||
_resourcePath(texture._resourcePath)
|
||||
{
|
||||
}
|
||||
|
||||
Texture::Texture(const Image &image):
|
||||
_texture(LoadTextureFromImage(image)),
|
||||
_texture(std::make_shared<::Texture>(LoadTextureFromImage(image))),
|
||||
_resourcePath()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Texture &Texture::operator=(const Texture &other)
|
||||
{
|
||||
UnloadTexture(this->_texture);
|
||||
this->_resourcePath = other._resourcePath;
|
||||
this->_texture = LoadTexture(this->_resourcePath.c_str());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
UnloadTexture(this->_texture);
|
||||
}
|
||||
|
||||
Texture::operator ::Texture() const
|
||||
{
|
||||
return this->_texture;
|
||||
return *this->_texture;
|
||||
}
|
||||
|
||||
std::shared_ptr<::Texture> Texture::fetchTextureInCache(const std::string &path)
|
||||
{
|
||||
if (Texture::_textureCache.find(path) == Texture::_textureCache.end())
|
||||
Texture::_textureCache.emplace(path, std::shared_ptr<::Texture>(
|
||||
new ::Texture(LoadTexture(path.c_str())), [](::Texture *p) {
|
||||
UnloadTexture(*p);
|
||||
delete p;
|
||||
}));
|
||||
return _textureCache[path];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user