@@ -42,6 +42,7 @@ Handle::TextureHandle TextureAllocator::loadTextureDDS(const std::vector<uint8_t
4242 m_Allocator.getElement (h).textureFormat = bgfx::TextureFormat::Unknown;
4343 m_Allocator.getElement (h).imageData = data;
4444 // m_Allocator.getElement(h).m_TextureHandle = bth;
45+ m_Allocator.getElement (h).m_TextureHandle .idx = bgfx::kInvalidHandle ;
4546 m_Allocator.getElement (h).m_TextureName = name;
4647
4748 ZenLoad::DDSURFACEDESC2 desc = ZenLoad::getSurfaceDesc (data);
@@ -80,6 +81,7 @@ Handle::TextureHandle TextureAllocator::loadTextureRGBA8(const std::vector<uint8
8081 m_Allocator.getElement (h).textureFormat = bgfx::TextureFormat::RGBA8;
8182 m_Allocator.getElement (h).imageData = data;
8283 // m_Allocator.getElement(h).m_TextureHandle = bth;
84+ m_Allocator.getElement (h).m_TextureHandle .idx = bgfx::kInvalidHandle ;
8385 m_Allocator.getElement (h).m_TextureName = name;
8486 m_Allocator.getElement (h).m_Width = width;
8587 m_Allocator.getElement (h).m_Height = height;
@@ -185,6 +187,12 @@ bool TextureAllocator::finalizeLoad(Handle::TextureHandle h)
185187{
186188 Texture& tx = m_Allocator.getElement (h);
187189
190+ // if there's already a texture loaded for the object, destroy it
191+ if (tx.m_TextureHandle .idx != bgfx::kInvalidHandle ) {
192+ bgfx::destroy (tx.m_TextureHandle );
193+ tx.m_TextureHandle .idx = bgfx::kInvalidHandle ;
194+ }
195+
188196 std::uint32_t textureFlags = BGFX_TEXTURE_NONE;
189197
190198 if (this ->m_Engine .getEngineArgs ().noTextureFiltering )
@@ -204,8 +212,10 @@ bool TextureAllocator::finalizeLoad(Handle::TextureHandle h)
204212 // stbi_image_free(out);
205213
206214 // Couldn't load this one?
207- if (bth.idx == bgfx::kInvalidHandle )
215+ if (bth.idx == bgfx::kInvalidHandle ) {
216+ LogWarn () << " Could not finalize texture load" ;
208217 return false ;
218+ }
209219
210220 tx.m_TextureHandle = bth;
211221 }
0 commit comments