Elevate Engine 1
Loading...
Searching...
No Matches
TextureManager.cpp
Go to the documentation of this file.
1#include "TextureManager.h"
2
3#include <filesystem>
4#include <thread>
5
6#include <stb/stb_image.h>
8
11
12namespace Elevate {
13 TextureManager::TextureManager()
14 {
15 //TODO : UNCOMMENT AND IMPL
16 m_defaultTexture = Texture::CreateFromColor({ 1.0,1.0,1.0,1.0 }, "default");
17 EE_CORE_INFO("Texture Manager Initialized.");
18 }
19
21 {
22 std::string key;
23
24 // TODO FIN A WAY TO GET THE ABOSLUTE PATH AND TO NORMALIZE IT
25 if (texture->GetMetadata().Source == TextureSource::File)
26 {
27 if (instance().m_Textures.count(texture->GetPath()) > 0)
28 {
29 return instance().m_Textures[texture->GetPath()];
30 }
31 else
32 {
33 instance().m_Textures[texture->GetPath()] = texture;
34 return texture;
35 }
36 }
37 else return texture;
38 }
39
40 // TODO REMOVE OR IMPL. BUT WE NEED TO DO SOMETHING WITH IT
42 //TexturePtr TextureManager::RegisterTexture(std::string& path)
43 //{
44 // int width, height, channels;
45 // unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha);
46
47 // if (!data)
48 // {
49 // EE_CORE_ERROR("Failed to load texture {}", path);
50 // return nullptr;
51 // }
52
53 // auto texture = AddOrGetTexture(Texture::CreateFromData(data, width, height, (TextureFormat) channels, path));
54 // stbi_image_free(data);
55 // return texture;
56 //}
57
58 TexturePtr TextureManager::GetTexture(const std::string& path)
59 {
60 std::filesystem::path fsPath = std::filesystem::absolute(path);
61 return (instance().m_Textures.count(fsPath.string()) > 0) ? instance().m_Textures[fsPath.string()] : nullptr;
62 }
63
65 {
66 std::string resolvedPath = PathResolver::Resolve(path);
67 std::filesystem::path fsPath = std::filesystem::absolute(resolvedPath);
68 std::string absPath = fsPath.string();
69
70 EE_CORE_TRACE("{}", absPath);
71
72 // If the texture is already loading, or already loaded, return and cancel
73 TexturePtr tex = GetTexture(absPath);
74 if (!tex)
75 {
76 for (TextureLoadResult& res : instance().m_loadingTextures) {
77 if (res.meta.Path == absPath)
78 {
79 // todo: change, should return an empty texture placeholder
80 return nullptr;
81 }
82 }
83 }
84 else
85 {
86 return tex;
87 }
88
89 // Creation of a blank texture
91 .Name(fsPath.filename().string())
92 .Path(absPath)
93 .size(0, 0)
95 .Usage(usage)
98 .Build();
99
100 tex = Texture::CreateFromData(nullptr, meta);
101
102 // Add the texture to the list of textures
103 instance().m_Textures[absPath] = tex;
104
106 res.meta = meta;
107
108 // Get the texture data async with stbi_load
109 std::thread([res]() mutable {
110 int width = 0;
111 int height = 0;
112 int channels = 0;
113 res.data = stbi_load(res.meta.Path.c_str(), &width, &height, &channels, 0);
115 .size(static_cast<uint32_t>(width), static_cast<uint32_t>(height))
116 .Format((TextureFormat) channels)
118 .Build();
119
120 std::lock_guard<std::mutex> lock(instance().m_textureMutex);
121 instance().m_loadingTextures.push_back(res);
122 }
123 ).detach();
124
125 return tex;
126 }
127
129 {
130 TextureManager& manager = instance();
131 std::lock_guard<std::mutex> lock(manager.m_textureMutex);
132
133 auto it = manager.m_loadingTextures.begin();
134 while (it != manager.m_loadingTextures.end())
135 {
136 switch (it->meta.State)
137 {
139 if (manager.m_Textures.count(it->meta.Path))
140 {
141 EE_CORE_INFO("Loaded texture : {}, {}x{}", it->meta.Path, it->meta.Width, it->meta.Height);
142 manager.m_Textures[it->meta.Path]->SetData(it->data, it->meta);
143 stbi_image_free(it->data);
144 it->data = nullptr;
145 }
146 it = manager.m_loadingTextures.erase(it);
147 break;
149 if (manager.m_Textures.count(it->meta.Path))
150 {
151 EE_CORE_ERROR("Failed to load texture : {}", it->meta.Path);
152 manager.m_Textures[it->meta.Path]->SetData(it->data, it->meta);
153 if (it->data)
154 {
155 stbi_image_free(it->data);
156 }
157 it->data = nullptr;
158 }
159 it = manager.m_loadingTextures.erase(it);
160 break;
161 default:
162 ++it;
163 break;
164 }
165 }
166 }
167}
static std::string Resolve(const std::string &virtualPath)
static TexturePtr RegisterTexture(TexturePtr texture)
static TexturePtr GetTexture(const std::string &path)
FUNCTION TO LOAD AND GET A TEXTURE SYNCED WITH THE CURRENT THREAD.
static void UpdateLoadingTextures()
static TexturePtr LoadTextureAsync(const std::string &path, TextureType usage=TextureType::Diffuse)
static TexturePtr CreateFromData(unsigned char *data, TextureMetadata &meta)
Definition Texture.cpp:87
static TexturePtr CreateFromColor(const glm::vec3 &color, const std::string &name, uint32_t width=1, uint32_t height=1)
Definition Texture.cpp:27
TextureFormat
Definition Texture.h:21
TextureType
Definition Texture.h:29
std::shared_ptr< Texture > TexturePtr
Definition Texture.h:13
TextureMetadataBuilder & Format(const TextureFormat fmt)
Definition Texture.h:82
TextureMetadata Build()
Definition Texture.h:90
TextureMetadataBuilder & Source(const TextureSource src)
Definition Texture.h:84
TextureMetadataBuilder & Usage(const TextureType type)
Definition Texture.h:83
TextureMetadataBuilder & Name(const std::string name)
Definition Texture.h:79
TextureMetadataBuilder & size(const uint32_t w, const uint32_t h)
Definition Texture.h:81
TextureMetadataBuilder & State(const TextureState state)
Definition Texture.h:85
TextureMetadataBuilder & Path(const std::string &path)
Definition Texture.h:80