1 #include <Engine/Data/Texture.hpp>
2 #include <Engine/Data/TextureManager.hpp>
4 #include <Core/Utils/Log.hpp>
6 #include <stb/stb_image.h>
12 using namespace Core::Utils;
14 TextureManager::TextureManager() =
default;
16 TextureManager::~TextureManager() {
17 for (
auto& tex : m_textures ) {
21 m_pendingTextures.clear();
22 m_pendingData.clear();
29 texData.
width = width;
33 m_pendingTextures[name] = texData;
35 return m_pendingTextures[name];
39 stbi_set_flip_vertically_on_load(
true );
41 unsigned char* data = stbi_load( texParameters.
name.c_str(),
42 (
int*)( &( texParameters.
width ) ),
43 (
int*)( &( texParameters.
height ) ),
48 LOG( logERROR ) <<
"Something went wrong when loading image \"" << texParameters.
name
56 texParameters.
format = GL_RED;
62 texParameters.
format = GL_RG;
67 texParameters.
format = GL_RGB;
72 texParameters.
format = GL_RGBA;
76 texParameters.
format = GL_RGBA;
81 CORE_ASSERT( data,
"Data is null" );
82 texParameters.
texels = data;
83 texParameters.
type = GL_UNSIGNED_BYTE;
89 bool mustFreeTexels =
false;
90 if ( texParams.
texels ==
nullptr ) {
91 loadTextureImage( texParams );
92 mustFreeTexels =
true;
94 auto ret =
new Texture( texParams );
95 ret->initializeGL( linearize );
97 if ( mustFreeTexels ) {
98 stbi_image_free( ret->getParameters().texels );
99 ret->getParameters().texels =
nullptr;
108 auto it = m_textures.find( texParameters.
name );
109 if ( it != m_textures.end() ) {
return it->second; }
113 auto it = m_pendingTextures.find( texParameters.
name );
114 if ( it != m_pendingTextures.end() ) {
115 auto pendingParams = it->second;
116 auto ret = loadTexture( pendingParams, linearize );
117 m_textures[pendingParams.name] = ret;
118 m_pendingTextures.erase( it );
123 auto ret = loadTexture( texParameters, linearize );
125 m_textures[texParameters.
name] = ret;
130 auto it = m_textures.find( filename );
132 if ( it != m_textures.end() ) {
134 m_textures.erase( it );
139 deleteTexture( texture->
getName() );
143 CORE_ASSERT( m_textures.find( texture ) != m_textures.end(),
144 "Trying to update non existing texture" );
145 m_pendingData[texture] = content;
149 if ( m_pendingData.empty() ) {
return; }
151 for (
auto& data : m_pendingData ) {
152 LOG( logINFO ) <<
"TextureManager::updateTextures \"" << data.first <<
"\".";
153 m_textures[data.first]->updateData( data.second );
155 m_pendingData.clear();
TextureParameters & addTexture(const std::string &name, uint width, uint height, void *data)
Texture * loadTexture(const TextureParameters &texParameters, bool linearize=false)
Texture * getOrLoadTexture(const TextureParameters &texParameters, bool linearize=false)
void deleteTexture(const std::string &filename)
void loadTextureImage(TextureParameters &texParameters)
void updatePendingTextures()
void updateTextureContent(const std::string &texture, void *content)
std::string getName() const
size_t height
height of the texture (t dimension)
GLenum format
Format of the external data.
size_t width
width of the texture (s dimension)
GLenum type
Type of the components in external data.
GLenum internalFormat
OpenGL internal format (WARNING, for Integer textures, must be GL_XXX_INTEGER)
std::string name
Name of the texture.