Elevate Engine 1
Loading...
Searching...
No Matches
Elevate::TextureManager Class Reference

#include <TextureManager.h>

Static Public Member Functions

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 TexturePtr LoadTextureAsync (const std::string &path, TextureType usage=TextureType::Diffuse)
 
static TexturePtr GetDefaultTexture ()
 
static bool IsAllLoaded ()
 

Static Protected Member Functions

static void UpdateLoadingTextures ()
 

Friends

class Application
 

Detailed Description

Definition at line 17 of file TextureManager.h.

Member Function Documentation

◆ GetDefaultTexture()

static TexturePtr Elevate::TextureManager::GetDefaultTexture ( )
inlinestatic

Definition at line 25 of file TextureManager.h.

25{ return instance().m_defaultTexture; }

◆ GetTexture()

TexturePtr Elevate::TextureManager::GetTexture ( const std::string &  path)
static

FUNCTION TO LOAD AND GET A TEXTURE SYNCED WITH THE CURRENT THREAD.

Definition at line 58 of file TextureManager.cpp.

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 }

◆ IsAllLoaded()

static bool Elevate::TextureManager::IsAllLoaded ( )
inlinestatic

Definition at line 27 of file TextureManager.h.

27{ return instance().m_loadingTextures.empty(); }

◆ LoadTextureAsync()

TexturePtr Elevate::TextureManager::LoadTextureAsync ( const std::string &  path,
TextureType  usage = TextureType::Diffuse 
)
static

Definition at line 64 of file TextureManager.cpp.

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
90 TextureMetadata meta = TextureMetadataBuilder()
91 .Name(fsPath.filename().string())
92 .Path(absPath)
93 .size(0, 0)
95 .Usage(usage)
96 .Source(TextureSource::File)
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
105 TextureLoadResult res;
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);
114 res.meta = TextureMetadataBuilder(res.meta)
115 .size(static_cast<uint32_t>(width), static_cast<uint32_t>(height))
116 .Format((TextureFormat) channels)
117 .State((res.data) ? TextureState::Loaded : TextureState::Failed)
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 }
static std::string Resolve(const std::string &virtualPath)
static TexturePtr GetTexture(const std::string &path)
FUNCTION TO LOAD AND GET A TEXTURE SYNCED WITH THE CURRENT THREAD.
static TexturePtr CreateFromData(unsigned char *data, TextureMetadata &meta)
Definition Texture.cpp:87
TextureState
Definition Texture.h:40
TextureFormat
Definition Texture.h:21
std::shared_ptr< Texture > TexturePtr
Definition Texture.h:13

◆ RegisterTexture()

TexturePtr Elevate::TextureManager::RegisterTexture ( TexturePtr  texture)
static

Definition at line 20 of file TextureManager.cpp.

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 }

◆ UpdateLoadingTextures()

void Elevate::TextureManager::UpdateLoadingTextures ( )
staticprotected

Definition at line 128 of file TextureManager.cpp.

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 }

Friends And Related Symbol Documentation

◆ Application

friend class Application
friend

Definition at line 29 of file TextureManager.h.


The documentation for this class was generated from the following files: