Okay this function definitely annoyed the crap out of me while I was trying to get it to work. So I fixed the problem in my implementation, which used to look the same as the last version of this code that was posted. So to any who has been having problems with this code, use the following:
Code: Select all
ITexture* ScaleTexture(IVideoDriver* Driver, ITexture* SrcTexture, vector2di destSize)
{
static u32 counter = 0;
IImage* SrcImage;
IImage* DestImage;
ITexture* IntTexture;
stringc SrcName = "NewTexture";
SrcName += counter;
IntTexture = Driver->addTexture(destSize, "IntermediateTexture");
SrcImage = Driver->createImageFromData(SrcTexture->getColorFormat(), SrcTexture->getSize(),
SrcTexture->lock());
DestImage = Driver->createImageFromData(SrcTexture->getColorFormat(), destSize,
IntTexture->lock());
IntTexture->unlock();
SrcImage->copyToScaling(DestImage);
SrcTexture->unlock();
Driver->removeTexture(IntTexture);
IntTexture = Driver->addTexture(SrcName.c_str(), DestImage);
SrcImage->drop();
DestImage->drop();
++counter;
return IntTexture;
}
Example:
Code: Select all
ITexture* scaledTexture = ScaleTexture(driver, driver->getTexture("media/testTexture.png"), vector2di(512, 512));
Simple to use, and it actually works!
FIX1: The problem with all the code above that was provided for this was the fact that a texture was trying to be added with the same name, which meant the texture cache wasn't loading it because it thought it was already there. So the solution was to simply remove the texture from the cache and then create the new scaled texture.
FIX2: This function also fixes the fact that the other two scale functions kept one unused intermediate texture in the texture cache upon every call to the function, which if you are dealing with big textures would eat up a whole lot of memory. That has been fixed with this function too.
FIX3: Also another problem was the fact that the scaled texture was being put into a texture with a static named, which means that upon two calls to ScaleTexture() you would only get the same texture back. So I fixed that by simply putting an incremental counter in there.
NOTE: This implementation provides so that your scalee (

) texture won't be deleted, so just the scaled texture will be returned, and your scalee will be kept in memory. You will have to remove that yourself if you want.
Overall, this has been tested, and proven to withstand, and maintain 100 scale calls without crashing. Tell me about any problems you have.