Sign in to follow this  
Goober King

Freeing textures.

Recommended Posts

Goober King    553
Is there some slick way to release all of the textures you've created? Totaly wiping out every texture, or do you always have to manualy do it for each one. I'm just wondering if there is some cool quick way to wipe the slate clean without any risk of missing one, there by waisting memory.

Share this post


Link to post
Share on other sites
dopo    122
im sure that if your textures are in one array then you can use the function above to delete them in one time with on waste memory.

Share this post


Link to post
Share on other sites
Goober King    553
Quote:
Original post by _the_phantom_
glDeleteTextures()


I am aware of the function, but thats not what I was looking for. I'm talking about something that deletes any and all textures without any need or regard for their ID value. The equivalent of droping a bomb on a town to make sure alllll the buildings are down at once instead of walking in with a bulldozer and wrecking ball to take them out one at a time.

I don't think such a thing exists, but I wanted find out for sure, since it would be handy.

edit...
Quote:
Original post by dopo
im sure that if your textures are in one array then you can use the function above to delete them in one time with on waste memory.


Their not. Thier in groups mostly, but there are sets which are way more complicated, and the amount can very wildly.

Share this post


Link to post
Share on other sites
tolaris    288
Quote:
Original post by Goober King
I am aware of the function, but thats not what I was looking for. I'm talking about something that deletes any and all textures without any need or regard for their ID value. The equivalent of droping a bomb on a town to make sure alllll the buildings are down at once instead of walking in with a bulldozer and wrecking ball to take them out one at a time.

Well, destroying the render context should work... as long as it's not shared, and it'd destroy more than just the textures but you did say equivalent of dropping a bomb... -.^

edit: d'oh, too slow >>;

Share this post


Link to post
Share on other sites
Goober King    553
Quote:
Original post by tolaris
Quote:
Original post by Goober King
I am aware of the function, but thats not what I was looking for. I'm talking about something that deletes any and all textures without any need or regard for their ID value. The equivalent of droping a bomb on a town to make sure alllll the buildings are down at once instead of walking in with a bulldozer and wrecking ball to take them out one at a time.

Well, destroying the render context should work... as long as it's not shared, and it'd destroy more than just the textures but you did say equivalent of dropping a bomb... -.^

edit: d'oh, too slow >>;


That would unforutantly be too big a bomb. The deal is for the fighting game Lore in my sig. When I go from game play, back to the character select screen I have to kill off a whole mess of textures. I don't want to kill off the rendering context which I'm guessing would be a really slow and just bad way to program.

Most of the textures are in arrays. Sets reserved for the stages, option screen, title screen. Those can all be easily be wiped out at once. The animation frames however get tricky. Seeing as how all the ID are all wrapped up with all the animation information. It recently occured to me I hadn't been freeing up those texures. Just replacing them with more of the same. Since I will have to go through all the sturctures and such to figure out how to correctly remove them all, I was hoping for an easy way to wipe them all out since I wont need a single one of the textures once I leave the gameplay mode and go back to the character select screen.


Share this post


Link to post
Share on other sites
baldurk    1028
If you are using a class or similar to encapsulate your data, you can put any freeing in their destructor. That way you can just delete them, or let them go out of scope, rather than explicitely calling the gl functions.

Share this post


Link to post
Share on other sites
Goober King    553
Quote:
Original post by baldurk
If you are using a class or similar to encapsulate your data, you can put any freeing in their destructor. That way you can just delete them, or let them go out of scope, rather than explicitely calling the gl functions.


Wouldn't that just wipe out the ID value? The actual texute data is never given to you by OGL, just a reference value. I would think you MUST use gl functions to free a texture up.

Share this post


Link to post
Share on other sites
_the_phantom_    11250
yes, you are right, you must free up the textures via OpenGL functions, however I belive the point baldurk was driving at was that you could place the glDeleteTextures() call in the destructor of the class and use that to free the texture on release.

The alternative is that if you have a resource manager of some type on creation you request a handle to a texture from the texture manager and then when you destroy the object it just tells the texture manager it doesnt require it any more, then before you pull up your character selection screen you ask the texture manager to flush all unused textures thus freeing them in one lump (either via driect calls to the glDeleteTextures() function or by destorying objects which do the same job).

Really the best way depends on how your game engine is arranged.

Share this post


Link to post
Share on other sites
Goober King    553
Quote:
Original post by _the_phantom_
yes, you are right, you must free up the textures via OpenGL functions, however I belive the point baldurk was driving at was that you could place the glDeleteTextures() call in the destructor of the class and use that to free the texture on release.

The alternative is that if you have a resource manager of some type on creation you request a handle to a texture from the texture manager and then when you destroy the object it just tells the texture manager it doesnt require it any more, then before you pull up your character selection screen you ask the texture manager to flush all unused textures thus freeing them in one lump (either via driect calls to the glDeleteTextures() function or by destorying objects which do the same job).

Really the best way depends on how your game engine is arranged.


Ahh. Well I'm just going to have to look at how I set it up. Its not like its complicated or anything. Its just that there are a few layers to dig through and I wrote the animation code like a year ago. There are animations, which are made of frames(of corse), which are built from sprites. So I will have to find a solid way of diging down do the sprite information in each frame and freeing every single texture one at a time. It just would have been nice if there was a function like KillThemAll_KillThemAll_In_a_GloryousAndMightyDestructoFunction();

Share this post


Link to post
Share on other sites
silvermace    634
i have a better idea, use a texture manager


namespace texman
{
enum ETexturePool {
TP_MENUS,
TP_GAME,
TP_MAX_POOLS
};

std::vector<GLuint> textures[TP_MAX_POOLS];

void deleteAllTextures( ETexturePool which_pool ) {
std::vector<GLuint>& t = textures[(int)which_pool];
std::vector<GLuint>::iterator i = t.begin();
for(; i != t.end(); i++ ) {
glDeleteTextures( 1, &(*i) );
}
t.resize(0);
}

void bindTexture( GLenum target, int local_id, ETexturePool pool = TP_GAME ) {
glBindTexture( target, textures[(int)pool][local_id] );
}

int createTexture( ETexturePool pool ) {
textures[(int)pool].push_back(0);
glGenTexture(1, &textures[(int)pool].back());
return textures[(int)pool].size()-1;
}

};


thats just to give you some ideas, whipped up off the top of my head
so theres bound to be some syntax issues. (plus its 2:30am here in NZ)

basic usage wouldnt differ much from current gl usage
int myTexture = texman::createTexture( TP_GAME );
texman::bindTexture( GL_TEXTURE_2D, myTexture );
texman::deleteAllTextures( TP_GAME ); // only delete the game textures


should be quick-simple "replace-all" job in any half decent IDE.

Share this post


Link to post
Share on other sites
Gorax    202
My image list (basically a linked list with a counter for the images which should be deleted when there are no references) is an extended class from a more general object list, which takes two functions as arguments (one to load the objects into memory, using only the filename, and one to unload the object from memory, using the reference returned by the loading function). The major up-side to this: compatibility (hence the image list extending my object list), the down-side: a little less friendly for specific tasks. Unloading all of the textures is fairly simple, just call the removeAll() function, or delete the list. The image list itself has the default image unloading function I use, which is this:


GLvoid unloadSingleTexture(GLuint texture){
if (glIsTexture(texture)) glDeleteTextures(1,&texture);
}



From there, it's not hard to iterate over any list you use (an array, or a vector-based one like silvermace's, or a linked list like mine) and delete all of the textures.

Share this post


Link to post
Share on other sites
ViperG    206
I basicly do the same thing. I don't use a linked list, I use a vector to store the id value. But I actually load textures on the fly off of game data files, and I store the filename into the vector as well. If I ever need to change or load a texture, I make sure it's not already in the vector, then I add it and send it to opengl. If the texture comes up again for whatever unusual reason, it will see if it's already loaded into opengl, and if it is, it passes the reference number that opengl gave it originally. I also can erase textures and it kills the vector node and then free's the texture from opengl.

Good rule of thumb, is if your going to have data units that will exist more than one time that are the same and differen't, you need some sort of memory manager.

Also consider changing your game. You can make a mini in game menu, to select characters, during in game, instead of going all the way back to the main menu, and reloading textures over and over again...

Share this post


Link to post
Share on other sites
Gorax    202
Quote:
Original post by ViperG
But I actually load textures on the fly off of game data files, and I store the filename into the vector as well. If I ever need to change or load a texture, I make sure it's not already in the vector, then I add it and send it to opengl. If the texture comes up again for whatever unusual reason, it will see if it's already loaded into opengl, and if it is, it passes the reference number that opengl gave it originally. I also can erase textures and it kills the vector node and then free's the texture from opengl.


That's what mine does, but I figured since my rant was already long enough, I shouldn't add to it (I also thought most of this was implied with the 'counter' I mentioned, but apparantly not...). ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this