#include "TextureManager.hpp" #include #include namespace renderer { TextureManager::TextureManager () : mBasePath(), mTextures(), mCurrentCategory(), mCurrentSubcategory(), mCurrentTextureName(), mCurrentTexture( nullptr ), mCurrentTextureId( 0 ), mTextureIdToPath(), mNextTextureId( 1 ) { } void TextureManager::scanDirectory ( const std::string& basePath ) { mBasePath = basePath; mTextures.clear(); if( !std::filesystem::exists( basePath ) ) { std::cerr << "TextureManager: Base path does not exist: " << basePath << std::endl; return; } for( const auto& categoryEntry : std::filesystem::directory_iterator( basePath ) ) { if( !categoryEntry.is_directory() ) continue; std::string categoryName = categoryEntry.path().filename().string(); for( const auto& subcategoryEntry : std::filesystem::directory_iterator( categoryEntry.path() ) ) { if( !subcategoryEntry.is_directory() ) continue; std::string subcategoryName = subcategoryEntry.path().filename().string(); for( const auto& fileEntry : std::filesystem::directory_iterator( subcategoryEntry.path() ) ) { if( !fileEntry.is_regular_file() ) continue; std::string fileName = fileEntry.path().filename().string(); std::string extension = fileEntry.path().extension().string(); if( extension != ".png" && extension != ".jpg" && extension != ".jpeg" && extension != ".bmp" && extension != ".tga" ) continue; TextureEntry entry; entry.mFullPath = fileEntry.path().string(); entry.mLoaded = false; mTextures[categoryName][subcategoryName][fileName] = entry; int textureId = mNextTextureId++; mTextureIdToPath[textureId] = std::make_tuple( categoryName, subcategoryName, fileName ); } } } std::cout << "TextureManager: Scanned " << mTextures.size() << " categories" << std::endl; } std::vector TextureManager::getCategories () const { std::vector categories; for( const auto& [category, _] : mTextures ) { categories.push_back( category ); } std::sort( categories.begin(), categories.end() ); return categories; } std::vector TextureManager::getSubcategories ( const std::string& category ) const { std::vector subcategories; auto catIt = mTextures.find( category ); if( catIt != mTextures.end() ) { for( const auto& [subcategory, _] : catIt->second ) { subcategories.push_back( subcategory ); } std::sort( subcategories.begin(), subcategories.end() ); } return subcategories; } std::vector TextureManager::getTextureNames ( const std::string& category, const std::string& subcategory ) const { std::vector textureNames; auto catIt = mTextures.find( category ); if( catIt != mTextures.end() ) { auto subIt = catIt->second.find( subcategory ); if( subIt != catIt->second.end() ) { for( const auto& [textureName, _] : subIt->second ) { textureNames.push_back( textureName ); } std::sort( textureNames.begin(), textureNames.end() ); } } return textureNames; } Texture* TextureManager::getTexture ( const std::string& category, const std::string& subcategory, const std::string& textureName ) { auto catIt = mTextures.find( category ); if( catIt == mTextures.end() ) return nullptr; auto subIt = catIt->second.find( subcategory ); if( subIt == catIt->second.end() ) return nullptr; auto texIt = subIt->second.find( textureName ); if( texIt == subIt->second.end() ) return nullptr; TextureEntry& entry = texIt->second; if( !entry.mLoaded ) { if( entry.mTexture.loadFromFile( entry.mFullPath ) ) { entry.mLoaded = true; } else { std::cerr << "TextureManager: Failed to load texture: " << entry.mFullPath << std::endl; return nullptr; } } return &entry.mTexture; } Texture* TextureManager::getCurrentTexture () { if( mCurrentTexture ) return mCurrentTexture; if( !mCurrentCategory.empty() && !mCurrentSubcategory.empty() && !mCurrentTextureName.empty() ) { mCurrentTexture = getTexture( mCurrentCategory, mCurrentSubcategory, mCurrentTextureName ); } return mCurrentTexture; } void TextureManager::setCurrentTexture ( const std::string& category, const std::string& subcategory, const std::string& textureName ) { mCurrentCategory = category; mCurrentSubcategory = subcategory; mCurrentTextureName = textureName; mCurrentTexture = getTexture( category, subcategory, textureName ); for( const auto& [id, path] : mTextureIdToPath ) { if( std::get<0>( path ) == category && std::get<1>( path ) == subcategory && std::get<2>( path ) == textureName ) { mCurrentTextureId = id; break; } } } int TextureManager::getCurrentTextureId () const { return mCurrentTextureId; } Texture* TextureManager::getTextureById ( int textureId ) { auto it = mTextureIdToPath.find( textureId ); if( it == mTextureIdToPath.end() ) return nullptr; const auto& [category, subcategory, textureName] = it->second; return getTexture( category, subcategory, textureName ); } } // namespace renderer