I'm trying to allow creating a new texture, which is taking one texture and copying another texture on top of it.
Then, I want to either save that texture in case I use it again, or drop it and remove it entirely from memory (when the object using it is destroyed, ofc).
However, I'm having troubles managing the memory for this.
Code: Select all
int texId = 0;
void copyTo(string fromImg, string toImg)
{
// Grab the texture to place over the other texture
ITexture* thisTex = driver->getTexture(fromImg.c_str());
if (!thisTex)
return;
// Grab the texture to place over
ITexture* sourceTex = driver->getTexture(toImg.c_str());
if (!sourceTex)
return;
// Create a new image from the base texture
IImage* sourceImg = driver->createImageFromData (
sourceTex->getColorFormat(),
sourceTex->getSize(),
sourceTex->lock(ETLM_READ_ONLY),
true
);
sourceTex->unlock();
// Create a new image from the texture we're going to place over the previous texture
IImage* newImg = driver->createImageFromData (
thisTex->getColorFormat(),
dimension2d<u32>(100, 100),
thisTex->lock(ETLM_READ_ONLY),
false // CRASH in COpenGLCoreTexture if this is set to true
);
thisTex->unlock();
// Copy on top of other texture
sourceImg->copyToWithAlpha(newImg, vector2d<int>(0,0), rect<s32>(0,0,100,100), SColor(255,255,255,255));
sourceImg->drop();
// Store the texture as, e.g. "MyTexture_2" so later I can getTexture "MyTexture_2" with the new image copied over it
ITexture* newTex = driver->addTexture((fromImg + "_" + to_string(texId++)).c_str(), newImg);
newImg->drop();
}