Commits

Ivan Vučica committed acb6e09

Added reference counting for textures.

  • Participants
  • Parent commits 12a1d29

Comments (0)

Files changed (11)

                    const double argVY) : texture(TextureManager::getInstance()->getTexture("asteroid.png")),
                                          x(argX), y(argY), vy(argVY)
 {
+    this->texture->retain();
 }
 Asteroid::~Asteroid()
 {
+    this->texture->release();
 }
 void
 Asteroid::paintSelf()
 Bullet::Bullet( double argX, double argY) : texture(TextureManager::getInstance()->getTexture("bullet.png")),
                                             x(argX), y(argY)
 {
+    this->texture->retain();
 }
-
+Bullet::~Bullet()
+{
+    this->texture->release();
+}
 void
 Bullet::updateSelf(const double timeDelta)
 {
     public:
     Bullet( const double x,
             const double y);
-
+    virtual ~Bullet();
 
     virtual void paintSelf(void);
 
                const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
                                     x(argX), y(argY)
 {
+    this->texture->retain();
 }
 Button::~Button()
 {
-    delete this->texture;
+    this->texture->release();
 }
 
 void Button::paintSelf()
                const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
                                     x(argX), y(argY), angle(0)
 {
+    this->texture->retain();
 }
 EnemyShip::~EnemyShip()
 {
-
+    this->texture->release();
 }
 
 void
                        const int argX,
                        const int argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
                                          x(argX), y(argY)
-                       {
-                       }
+{
+    this->texture->retain();
+}
 Layer::~Layer()
 {
-    delete this->texture;
+    this->texture->release();
 }
 void
 Layer::paintSelf()
                        const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
                         x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
 {
+    this->texture->retain();
     shootSound = AudioMgr::getInstance()->loadSound("gun.wav");
 }
 PlayerShip::~PlayerShip()
 {
-    delete this->texture;
+    this->texture->release();
     AudioMgr::getInstance()->unloadSound(shootSound);
 }
 void
 #include <OpenGL/glu.h>
 #endif
 #include "texture.h"
+#include "texture_manager.h"
 
-Texture::Texture(const std::string& textureFile)
+Texture::Texture(const std::string& textureFile) : refCount(0), textureFile(textureFile)
 {
-    this->textureFile = textureFile;
     this->_loadTexture();
 }
 
 {
     glBindTexture(GL_TEXTURE_2D, this->textureId);
 }
+
+void Texture::release()
+{
+    this->refCount--;
+    if(this->refCount >= 1)
+        return;
+    
+    TextureManager::getInstance()->unloadTexture(this->getFilename());
+}
 
     int getW() const { return this->w; }
     int getH() const { return this->h; }
+    const std::string& getFilename() const { return this->textureFile; }
 
+    void retain() { this->refCount++; }
+    void release();
+    
     private:
     std::string textureFile;
     GLuint textureId;
     int w, h;
+    int refCount;
 
     void _loadTexture();
 };

texture_manager.cpp

     }
 
 }
+void TextureManager::unloadTexture(const std::string &filename)
+{
+    std::map<std::string, Texture*>::iterator it = textures.find(filename);
+    if(it == textures.end())
+        return; // already unloaded
+    
+    printf("Unloading %s\n", filename.c_str());
+    delete it->second;
+    textures.erase(it);
+}

texture_manager.h

     Texture* getTexture(std::string filename);
     static void DestroyInstance();
     bool Loaded();
-
-
+    void unloadTexture(const std::string& filename);
 
     private:
     std::map<std::string, Texture*> textures;