Archived

This topic is now archived and is closed to further replies.

GoldMage

Making a texture manager (C++) (OpenGL)

Recommended Posts

Okay first off this is my first post here . Next, I''m thinking about making a texture manager for my Pong-Type game, in C++ using OpenGL. I''m looking for the best way to make one. I''m using many of the same textures over. But right now I have all my textures implanted into my class as in each block has it''s own texture. This is probably very inefficient. Now I want to make a texture manager but currently all my classes have Texture objects would Create say like 3 different textures at run-time and then assigning those to the classes'' texture object speed it up or not? Say something like this //Init the textures TextureOne.Load("texture1.bmp"); TextureTwo.Load("texture2.bmp"); TextureThree.Load("texture3.bmp"); //Assign the textures Block[1].Texture = TextureOne; Block[2].Texture = TextureOne; //and so on I haven''t studied much optimizing so I don''t know how I can make a efficient one, so please help. -GoldMage

Share this post


Link to post
Share on other sites
I use a fairly simple texture manager that uses an std::map to map texture filenames to OpenGL texture handles. Any time an object requests a texture to be loaded, the handler first checks to see if it has already been loaded, and returns the already loaded handle for use if so. If not, it loads the texture, creates a new map entry, and returns the handle. Each entry also tracks a reference count of the number of objects that have requested it, decrementing this count when it is released. When a texture''s reference count hits 0, that texture is unloaded.

With a system like this, it is easy to iterate through your map at program exit to see if any textures have not been properly released. Filenames can be printed to indicate which textures were not properly released, then the textures can be deleted to avoid memory leaks.


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites