changing textures?

Blitz3D Forums/Blitz3D Programming/changing textures?

Pongo(Posted 2004) [#1]
Sorry this sounds like such a stupid question, but I have not had to do this bit before.

ok,... so if I assign a texture to an entity like this.
box=createcube()
boxtex=loadtexture("texturename.jpg")
entitytexture box,boxtex

but later want the user to be able to select a different texture, what is the best way to do this?

freetexture only clears up the texture, but not what has been applied already, so is there a problem with piling this on top of the previous code?
freetexture boxtex
boxtex=loadtexture("texturename2.jpg")
entitytexture box,boxtex

What happens to the original texture? is it overwritten? if not how do I delete it? Or do I need to freeentity and re-texture?

Edit: one thing I thought of was to simply load the new texture as an image, and copy it to the texture, but that seems clunky, and also brings up the problem of different texture resolutions.

Thanks.


DrakeX(Posted 2004) [#2]
you see, blitz has something under the hood called reference counting. basically this means that each resource, such as meshes, textures, and so on, keeps track of how many things are currently pointing to it.

when you load a texture, its refcount is set to 1. then you texture an object with it, and that bumps the refcount to 2. now, you retexture the object with a new texture, and the old texture's refcount is dropped to 1 - but it's still in memory. when you call freetexture, it drops the refcount again - and when the refcount reaches 0, it is truly deleted from memory.

you, as the blitz programmer, don't have to worry about any of this. all you have to do is just use EntityTexture again on the entity, and blitz will take care of the rest. believe me - it will not hurt to have two textures for your box in memory. if you truly don't need the original texture, you can free it.


Pongo(Posted 2004) [#3]
ok,... here's a bit of a followup question then. My biggest concern is having an essential memory leak with textures taking up resources even though they are not used.

suppose I have another texture overlaid on top of the first one, but the second one is a reflection using additive mode. Now when I apply the new texture, it covers not only the original texture, but also the secondary reflection texture. I hope you can see my problem here. The old textures will still be there on the entity, correct? (even though at this point they are covered over, they are still using resources)

Ultimately, this is only being used in an editor and only 1 texture and 1 reflection will be saved out, so I don't think it will be much of an issue, but I'm trying to do it the "correct way" if there is such a thing.

edit: just re-read your post again,... if I understand you correctly, then the old textures will be removed once 1) the texture has been freeTexture'd, and 2) a new texture has been applied?


DrakeX(Posted 2004) [#4]
your edit is correct. after you free a texture and retexture any objects that use that texture, it will be freed from memory.

just to clarify - when you apply a new texture to an object, the old one has nothing to do with the object anymore - it's just sitting in memory (unless, of course, its refcount is 0, in which case it's destroyed). so, unless you explicitly free a texture with FreeTexture, it will remain in memory until the end of the program. if you're doing an editor where you'll only have one texture at a time, then yes, it's a good idea to free the old texture. if you're reusing the texture handle for the new texture, then it's necessary to delete the old texture before loading a new one into the handle, or else you will in fact get a leak.

i wasn't sure what your intentions were with your first post, which is why i was somewhat dubious about the texture freeing.. but now that i know that it's a not-so-performance-critical editor, freeing and reloading is fine :)