75 lines
1.6 KiB
C++
75 lines
1.6 KiB
C++
#include "TextureManager.h"
|
|
#include "log/log.h"
|
|
|
|
TextureManager::TextureManager() : nextId(1), unloaded(false)
|
|
{
|
|
}
|
|
|
|
TextureManager::~TextureManager()
|
|
{
|
|
if (!unloaded) {
|
|
for (auto& pair : textures) {
|
|
UnloadTexture(pair.second);
|
|
}
|
|
textures.clear();
|
|
textureSources.clear();
|
|
}
|
|
}
|
|
|
|
unsigned int TextureManager::Load(const std::string& path)
|
|
{
|
|
Texture2D texture = LoadTexture(path.c_str());
|
|
if (texture.id == 0) {
|
|
log_warn("Failed to load texture: %s", path.c_str());
|
|
return 0;
|
|
}
|
|
|
|
unsigned int id = nextId++;
|
|
textures[id] = texture;
|
|
textureSources[id] = TextureSource{path};
|
|
log_info("Loaded texture id=%u from %s", id, path.c_str());
|
|
return id;
|
|
}
|
|
|
|
void TextureManager::Unload(unsigned int id)
|
|
{
|
|
auto it = textures.find(id);
|
|
if (it != textures.end()) {
|
|
UnloadTexture(it->second);
|
|
textures.erase(it);
|
|
textureSources.erase(id);
|
|
log_info("Unloaded texture id=%u", id);
|
|
}
|
|
}
|
|
|
|
Texture2D* TextureManager::Get(unsigned int id)
|
|
{
|
|
auto it = textures.find(id);
|
|
if (it != textures.end()) {
|
|
return &it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const TextureSource* TextureManager::GetSource(unsigned int id) const
|
|
{
|
|
auto it = textureSources.find(id);
|
|
if (it != textureSources.end()) {
|
|
return &it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void TextureManager::UnloadAll()
|
|
{
|
|
if (!unloaded) {
|
|
for (auto& pair : textures) {
|
|
UnloadTexture(pair.second);
|
|
}
|
|
textures.clear();
|
|
textureSources.clear();
|
|
unloaded = true;
|
|
log_info("Unloaded all textures");
|
|
}
|
|
}
|