Files
simian/src/TextureManager.cpp
T
nick d7add3964d
CI / build-and-test (push) Successful in 2m37s
Sync Docs to Gitea Wiki / Sync docs to Gitea wiki (push) Successful in 10s
feat: additonal components, updated docs
2026-03-09 17:37:29 +13:00

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");
}
}