Freeing textures.

Started by
13 comments, last by Gorax 18 years, 10 months ago
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();

------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
Advertisement
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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.
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...
Black Sky A Star Control 2/Elite like game
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...). ;)

This topic is closed to new replies.

Advertisement