holesome/src/sprites/texture_manager.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

#include "texture_manager.hpp"
#include "../logging/easylogging++.h"
std::shared_ptr<TextureManager> TextureManager::getInstance()
{
if (TextureManager::singleton == nullptr)
{
TextureManager::singleton = std::make_shared<TextureManager>();
}
return TextureManager::singleton;
}
bool TextureManager::loadTexture(const std::string& textureName, const std::string& texturePath)
{
if (this->doesTextureExist(textureName))
{
LOG(WARNING) << "Texture " << textureName << " already loaded";
return false;
}
sf::Texture texture;
if (!texture.loadFromFile(texturePath))
{
LOG(ERROR) << "Could not load texture " << textureName << " from " << texturePath;
LOG(ERROR) << "Error: " << strerror(errno);
return false;
}
LOG(INFO) << "Loaded texture " << textureName << " from " << texturePath;
this->textures[textureName] = std::make_shared<sf::Texture>(texture);
return true;
}
bool TextureManager::doesTextureExist(const std::string& textureName)
{
return this->textures.find(textureName) != this->textures.end();
}
TextureManager::~TextureManager()
{
// Unload all textures
// Shared pointers will take care of the rest
textures.clear();
}
std::shared_ptr<sf::Texture> TextureManager::getTexture(const std::string &textureName)
{
if (!this->doesTextureExist(textureName))
{
return nullptr;
}
return this->textures[textureName];
}