aboutsummaryrefslogtreecommitdiff
path: root/apps/openmb/renderer/TextureManager.cpp
blob: bfd7e15ffc46445fca5de889f694ed6493e743f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "TextureManager.hpp"

#include <algorithm>
#include <iostream>

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<std::string> TextureManager::getCategories () const {
    std::vector<std::string> categories;
    for( const auto& [category, _] : mTextures ) {
        categories.push_back( category );
    }
    std::sort( categories.begin(), categories.end() );
    return categories;
}

std::vector<std::string> TextureManager::getSubcategories ( const std::string& category ) const {
    std::vector<std::string> 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<std::string> TextureManager::getTextureNames ( const std::string& category,
                                                           const std::string& subcategory ) const {
    std::vector<std::string> 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