CppOgl
CppOgl

Reputation: 151

How to delete all textures made at once?

How can I delete all the textures I've made? Suppose I load a few textures:

GLuint tx_wall,tx_floor,tx_tiles;
tx_wall=LoadTexture("tex_wall.raw",512,512),
tx_floor=LoadTexture("tex_floor.raw",512,512),
tx_tiles=LoadTexture("tex_tiles.raw",512,512);

then use them:

glBindTexture(GL_TEXTURE_2D,tx_wall);
  glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(0,   0,  0);
    glTexCoord2f(1, 0); glVertex3f(0,  50,  0);
    glTexCoord2f(1, 1); glVertex3f(0,   0, 14);
    glTexCoord2f(0, 1); glVertex3f(0,  50, 14);
  glEnd();
glBindTexture(GL_TEXTURE_2D,tx_floor);
  glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(0,   0,  0);
    glTexCoord2f(1, 0); glVertex3f(50,  50, 0);
    glTexCoord2f(1, 1); glVertex3f(50,  50, 0);
    glTexCoord2f(0, 1); glVertex3f(0,   0,  0);
  glEnd();
(and so on)

and when the game ends, delete them:

glDeleteTextures(1,&tx_wall);
glDeleteTextures(1,&tx_floor);
glDeleteTextures(1,&tx_tiles);

All works fine but if i have 10 or 20 textures than how will i terminate them all without taking their names?

Upvotes: 0

Views: 1855

Answers (2)

Adam Bowen
Adam Bowen

Reputation: 11240

Perhaps not exactly what you were intending, but RAII would be a sensible option:

class Texture
{
public:
    Texture( const std::string& name, int width, int height ) :
        m_id( LoadTexture(name.c_str(),width,height) )
    {
    }

    ~Texture()
    {
        if(m_id)
            glDeleteTextures(1,&m_id);
        m_id = 0;
    }

    GLuint id() const
    {
        return m_id;
    }

private:
    Texture( const Texture& );
    Texture& operator=( const Texture& );

    GLuint m_id;
};

Usage:

Texture tx_wall("tex_wall.raw",512,512);
Texture tx_floor("tex_floor.raw",512,512);
Texture tx_tiles("tex_tiles.raw",512,512);

and:

glBindTexture(GL_TEXTURE_2D,tx_wall.id());
  glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(0,   0,  0);
    glTexCoord2f(1, 0); glVertex3f(0,  50,  0);
    glTexCoord2f(1, 1); glVertex3f(0,   0, 14);
    glTexCoord2f(0, 1); glVertex3f(0,  50, 14);
  glEnd();
glBindTexture(GL_TEXTURE_2D,tx_floor.id());
  glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(0,   0,  0);
    glTexCoord2f(1, 0); glVertex3f(50,  50, 0);
    glTexCoord2f(1, 1); glVertex3f(50,  50, 0);
    glTexCoord2f(0, 1); glVertex3f(0,   0,  0);
  glEnd();

The benefit is that you can retain textures only for as long as you need them, the code is exception safe and texture release is automatic. The downside is that you can't copy Texture objects around without some kind of reference counting (for which I would recommend using boost::shared_ptr rather than rolling your own, because reference counting in multi-threaded environments is tricky). The latter is the reason for the private copy constructor and assignment operator.

Upvotes: 4

Sander De Dycker
Sander De Dycker

Reputation: 16243

If you put all texture identifiers in an array, you can delete them all in one call using glDeleteTextures (just like you can generate them all in one call using glGenTextures).

GLuint textures[3];
glGenTextures(3, textures);

/* ... */

glDeleteTextures(3, textures);

Upvotes: 6

Related Questions