images, textures and memory managment...

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
ultramedia
Posts: 175
Joined: Wed Dec 20, 2006 12:04 pm

images, textures and memory managment...

Post by ultramedia »

Hey Guys,

I've been trying for HOURS to figure out images, textures and memory management and I think I need some help.

In my irrlicht apps constructor I have the following code which adds (successfully) an image to the gui:

Code: Select all

video::ITexture* testPNG = driver->getTexture("../content/all/ui/watch.png");
testImage = guienv->addImage(testPNG,core::position2d<s32>(10,10),true);
Then down further I have a method I call every frame with the following snippet in it :

Code: Select all

IImage*   rasterImage = driver->createImageFromData(ECF_R8G8B8,dimension2d<u32>(test_agg_frame_width,test_agg_frame_height),test_agg_buffer);
ITexture* rasterTexture = driver->addTexture("test",rasterImage);
testImage->setImage(rasterTexture);
I know I'm supposed to drop images and textures when I'm done with them but I can't seem to figure out how without getting lots of really annoying errors or memory leaks. Can anyone tell me what the right way to clean up after the block above is?
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: images, textures and memory managment...

Post by Radikalizm »

Can you tell us what you're trying to achieve exactly? Maybe you need to rethink your approach somewhat higher up, because creating an image and building a texture from it every single frame is really not a good idea IMO
ultramedia
Posts: 175
Joined: Wed Dec 20, 2006 12:04 pm

Re: images, textures and memory managment...

Post by ultramedia »

I'm trying to learn how to use AGG to create vector based textures, but I want to be able to animate them...

Here's my method that runs every frame when I hold z down (for learning/testing purposes) :

Code: Select all

//////////////////////
// CedenApp testAGG //
//////////////////////
 
void CedenApp::testAGG(){
 
        // this method runs every frame //
 
        agg::rendering_buffer test_agg_rbuf(test_agg_buffer, test_agg_frame_width, test_agg_frame_height, test_agg_frame_width * test_agg_bytes_per_pixel);
        pixfmt_type                       test_agg_pixf(test_agg_rbuf);
        renbase_type              test_agg_rbase(test_agg_pixf);
 
        // set default fill color (B, G, R - why the frak for when it says rgba I don't know)
        test_agg_rbase.clear(agg::rgba(0.5, 0.05, 0.05));
 
        // rasterizer
        agg::rasterizer_scanline_aa<> agg_ras;
        agg::scanline_u8 agg_sl;
 
        // clear out any vectors in agg_ras from the last texture request
        agg_ras.reset();
 
        // draw test texture (square)
        agg_ras.add_vertex(((rand() % 5) - 1),((rand() % 5) - 1),1);  // 1 = start a new shape 
        agg_ras.add_vertex(10,5,2); // 2 = continue current shape
        agg_ras.add_vertex(10,10,2);
        agg_ras.add_vertex(5,10,2);
        agg_ras.add_vertex(0,0,79); // 79 = close current shape (so x,y don't matter)
 
        // rasterize the vector shapes
        agg::render_scanlines_aa_solid(agg_ras, agg_sl, test_agg_rbase, agg::rgba(0,(rand()%100)/100.0,0,1));
 
        // so now I have a new randomly different (slightly) vector shape rasterised into test_agg_buffer (unsigned char*)
        // but I can't figure out the right way to get it into a GUI image (i.e. for a radar map) or a meshes material :(
}
ultramedia
Posts: 175
Joined: Wed Dec 20, 2006 12:04 pm

Re: images, textures and memory managment...

Post by ultramedia »

The code from my first post will actually update the GUI image with each newly updated vector image, but if I drop the image and the texture I get an unhandled exception. If I don't drop them my app adds 1MB to it's memory usage every second or so :(
ultramedia
Posts: 175
Joined: Wed Dec 20, 2006 12:04 pm

Re: images, textures and memory managment...

Post by ultramedia »

Radikalizm wrote:Can you tell us what you're trying to achieve exactly? Maybe you need to rethink your approach somewhat higher up, because creating an image and building a texture from it every single frame is really not a good idea IMO
Hi Everyone,

Managed to get all my dramas sorted out, as Radicalizm said part of my problem was trying to create an image and texture every frame. I went and spent a couple of hours reading explanations about pointers 100 times over until I understood them properly.

The next problem I was stuck on was copying color values from the AGG buffer to my texture in between a texture lock and unlock. The solution was realizing that even though the AGG code says the color format of the buffer is A8R8B8G8, it's not. AGG writes alpha values into a second seperate buffer, so the buffers actual structure is just R8B8G8.
Post Reply