summaryrefslogtreecommitdiff
path: root/lib/assetManager.cpp
blob: 9c60f9c95ac8111eabaeb830ca97e984c3ae7383 (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
#include "assetManager.hpp"

#include <filesystem>
#include <iostream>

assetManager::assetManager() = default;

assetManager::~assetManager()
{
    cleanup();
}

void assetManager::init(const std::string& dirPath)
{
    if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath)) {
        std::cerr << "assetmanager: directory not found: " << dirPath << std::endl;
        return;
    }

    for (const auto& entry : std::filesystem::recursive_directory_iterator(dirPath)) {
        if (entry.is_regular_file()) {
            std::string ext = entry.path().extension().string();

            if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") {
                std::string key = entry.path().stem().string();
                std::string fullPath = entry.path().string();

                Texture2D tex = LoadTexture(fullPath.c_str());

                if (tex.id != 0) {
                    textures[key] = tex;
                    std::cout << "success loading texture: " << key << std::endl;
                } else {
                    std::cerr << "failed to load texture: " << key << std::endl;
                }
            }
        }
    }
}

Texture2D assetManager::load(const std::string& texName)
{
    const auto tex = textures.find(texName);
    if (tex != textures.end()) {
        return tex->second;
    }

    std::cerr << "asset not found: " << texName << std::endl;
    return Texture2D{0};
}

void assetManager::cleanup()
{
    for (auto& pair : textures) {
        if (pair.second.id != 0) {
            UnloadTexture(pair.second);
        }
    }
    textures.clear();
}