I'm creating lots of textures that I will be able to blit to and from, using them like SDL_Surface. I'm using ITexture instead of IImage because ITexture will use hardware for blitting.
For some reason when creating lots of textures and removing them, I get a sort of texture corruption. To illustrate, I wrote the following example -
Code: Select all
#include "irrlicht.h"
#include <string>
using namespace irr;
using namespace video;
using namespace gui;
using namespace core;
using namespace io;
void DrawRect(IVideoDriver *Driver, ITexture *Texture, int x, int y, int w, int h, int r, int g, int b, int a)
{
Driver->setRenderTarget(Texture, false);
//clear view matrix before drawing - in OpenGL, will not draw correctly unless do so.
matrix4 mat;
mat.makeIdentity();
Driver->setTransform(video::ETS_VIEW,mat);
SColor scolor(a, r, g, b);
Driver->draw2DRectangle(scolor, rect<s32>(x, y, x + w, y + h));
Driver->setRenderTarget(ERT_FRAME_BUFFER, false);
}
int main(int argc, char *argv[])
{
IrrlichtDevice *Device = createDevice(EDT_OPENGL, dimension2d<u32>(1600, 1024), 32, true, false, false, NULL);
IVideoDriver *Driver = Device->getVideoDriver();
//initial setup
ITexture *t1 = Driver->addRenderTargetTexture(dimension2d<u32>(1600,1024), "1", ECF_A8R8G8B8);
ITexture *t2 = Driver->addRenderTargetTexture(dimension2d<u32>(1600,1024), "2", ECF_A8R8G8B8);
ITexture *t3 = Driver->addRenderTargetTexture(dimension2d<u32>(760,970), "3", ECF_A8R8G8B8);
ITexture *t4 = Driver->addRenderTargetTexture(dimension2d<u32>(780,990), "4", ECF_A8R8G8B8);
ITexture *t5 = Driver->addRenderTargetTexture(dimension2d<u32>(780,990), "5", ECF_A8R8G8B8);
ITexture *t6 = Driver->addRenderTargetTexture(dimension2d<u32>(780,990), "6", ECF_A8R8G8B8);
ITexture *t7 = Driver->addRenderTargetTexture(dimension2d<u32>(780,990), "7", ECF_A8R8G8B8);
//confirming correct size created
/*
char buffer[10];
itoa(t7->getSize().Width, buffer, 10);
fprintf(stderr, buffer);
itoa(t7->getSize().Height, buffer, 10);
fprintf(stderr, buffer);
*/
//clear t7 with black
Device->run();
Driver->beginScene();
DrawRect(Driver, t7, 0, 0, 780, 990, 0, 0, 0, 255);
Driver->endScene();
//Destroy t3
Driver->removeTexture(t3);
//create t8
ITexture *t8 = Driver->addRenderTargetTexture(dimension2d<u32>(740,950), "8", ECF_A8R8G8B8);
while (Device->run())
{
Driver->beginScene();
//draw a red square on T7
DrawRect(Driver, t7, 100, 100, 100, 100, 255, 0, 0, 255);
Driver->setRenderTarget(ERT_FRAME_BUFFER, false);
//clear view matrix before drawing - in OpenGL, will not draw correctly unless do so.
matrix4 mat;
mat.makeIdentity();
Driver->setTransform(video::ETS_VIEW,mat);
Driver->draw2DImage(t7, position2d<s32> (0, 0),
rect<s32>(0, 0, t7->getSize().Width, t7->getSize().Height), 0, SColor(255,255,255,255), true);
Driver->endScene();
}
}
The background "corruption" is different depending on what has been blitted or created before, like the texture is invalid and is displaying some random area of memory.
I've tried stuff like moving the addRenderTargetTexture and removeTexture calls in between the beginScene() and endScene() calls, but it doesn't work. However, if I remove the call to create t8 or destroy t3 it works. Also it works if I create fewer textures (t1-t6).
I'm using OpenGL as I want it to be portable between platforms. When I run using software renderer I just get a blank screen. The program is compiled on Dev-C.
Any help would be appreciated Maybe some other people could compile this and see if they get the same issue? Irrlicht doesn't seem to be really designed for 2D so this is a bit of a hack, not sure if this is OK or not.