Commits

Nikola Cvetko committed 085f57d

Added TextureManager.

Comments (0)

Files changed (9)

 		<Unit filename="redenemyship.h" />
 		<Unit filename="texture.cpp" />
 		<Unit filename="texture.h" />
+		<Unit filename="texture_manager.cpp" />
+		<Unit filename="texture_manager.h" />
 		<Unit filename="yellowenemyship.cpp" />
 		<Unit filename="yellowenemyship.h" />
 		<Extensions>
 #include "asteroid.h"
 #include "texture.h"
+#include "texture_manager.h"
 
 Asteroid::Asteroid(const double argX,
                    const double argY,
-                   const double argVY) : texture(new Texture("asteroid.png")),  x(argX), y(argY), vy(argVY)
+                   const double argVY) : texture(TextureManager::getInstance()->getTexture("asteroid.png")),
+                                         x(argX), y(argY), vy(argVY)
 {
 }
 Asteroid::~Asteroid()
 #include "texture.h"
 #include "playership.h"
 #include "gm_level.h"
+#include "texture_manager.h"
 
-Bullet::Bullet( double argX, double argY) : x(argX), y(argY), texture(new Texture("bullet.png"))
+Bullet::Bullet( double argX, double argY) : texture(TextureManager::getInstance()->getTexture("bullet.png")),
+                                            x(argX), y(argY)
 {
 }
 
     for(std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
     {
         GameObject * currentObject = *it;
-        
+
         if(this == currentObject)
             continue;
-        
+
         EnemyShip * currentEnemyShip = dynamic_cast<EnemyShip*>(currentObject);
         if(currentEnemyShip) // if(currentEnemyShip != NULL)
         {
 
     glEnd();
     glPopMatrix();
-    
+
 }
 #include "gm_credits.h"
 #include "gm_mainmenu.h"
 #include "gamemode.h"
+#include "texture_manager.h"
 
 extern bool gRunning;
 
 Button::Button(const std::string& textureFile,
                const double argX,
-               const double argY) : texture(new Texture(textureFile)), x(argX), y(argY)
+               const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                    x(argX), y(argY)
 {
 }
 Button::~Button()
 #include "EnemyShip.h"
 #include "texture.h"
+#include "texture_manager.h"
 #include <math.h>
 
 bool gDrawCollisionRects;
 
 EnemyShip::EnemyShip(const std::string& textureFile,
                const double argX,
-               const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0)
+               const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                    x(argX), y(argY), angle(0)
 {
 }
-
 EnemyShip::~EnemyShip()
 {
+
 }
 
 void
 {
     if(this->getShouldDie())
         glColor4f(1, 0, 0, 1);
-    
+
     this->texture->bind();
     glEnable(GL_TEXTURE_2D);
 
 
 
     glColor4f(1, 1, 1, 1);
-    
+
     if(gDrawCollisionRects)
     {
         glDisable(GL_TEXTURE_2D);
 #include "layer.h"
 #include "texture.h"
+#include "texture_manager.h"
 
 
 
 Layer::Layer(const std::string& textureFile,
                        const int argX,
-                       const int argY) : texture(new Texture(textureFile)), x(argX), y(argY)
+                       const int argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                         x(argX), y(argY)
                        {
                        }
 Layer::~Layer()
 #include "gm_level.h"
 #include "gamemode.h"
 #include "audiomgr.h"
+#include "texture_manager.h"
 
 static const double playerShipAngleV = 180;
 static const double playerShipYV = 380;
 
 PlayerShip::PlayerShip(const std::string& textureFile,
                        const double argX,
-                       const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
+                       const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                        x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
 {
     shootSound = AudioMgr::getInstance()->loadSound("gun.wav");
 }
     angle += angleV * timeDelta;
     y += yv * timeDelta;
     timeUntilNextFiring -= timeDelta;
-    
+
     static bool prevJoyState = 0;
     bool currentJoyState = (gJoystick && SDL_JoystickGetButton(gJoystick, 0));
-    
+
     if(SDL_GetKeyState(NULL)[SDLK_SPACE] ||
        currentJoyState)
     {
             timeUntilNextFiring = 0;
         fire();
     }
-    
+
     if(gJoystick)
     {
         Sint16 axisYInt = SDL_JoystickGetAxis(gJoystick, 1);
         double axisY = (axisYInt / 32767.);
-        
+
         y += axisY * playerShipYV * timeDelta;
     }
-    
+
     if(y < -240) y = -240;
     if(y > 240) y = 240;
     prevJoyState = currentJoyState;
         case SDLK_DOWN:
         yv =  playerShipYV;
         break;
-            
+
         case SDLK_SPACE:
         timeUntilNextFiring = 0;
         break;
 {
     if(this->timeUntilNextFiring > 0)
         return;
-    
+
     Bullet * b = new Bullet(this->x, this->y);
     GM_Level * currentLevel = (GM_Level*)gGame;
-    
+
     AudioMgr::getInstance()->playSound(shootSound);
     currentLevel->addGameObject(b);
-    
+
     this->timeUntilNextFiring = this->getFiringRate();
 }
 

texture_manager.cpp

+#include "texture_manager.h"
+#include "texture.h"
+
+TextureManager* TextureManager::instance;
+
+TextureManager::TextureManager()
+{
+    bLoaded = false;
+}
+
+TextureManager::~TextureManager()
+{
+
+}
+
+TextureManager* TextureManager::getInstance()
+{
+    if(!instance)
+        instance = new TextureManager();
+    return instance;
+}
+
+void
+TextureManager::DestroyInstance()
+{
+    delete instance;
+    instance = NULL;
+}
+
+bool
+TextureManager::Loaded()
+{
+    return this->bLoaded;
+}
+
+Texture* TextureManager::getTexture(std::string filename)
+{
+    if(textures.count(filename) == 0)
+    {
+        Texture * texture = new Texture(filename);
+        std::map<std::string, Texture*>::iterator it = textures.end();
+        textures.insert(it, std::pair<std::string, Texture*>(filename, texture));
+        return texture;
+
+    }
+    else
+    {
+        return textures.find(filename)->second;
+    }
+
+}

texture_manager.h

+#ifndef PUCALJKA_TEXTURE_MANAGER_H
+#define PUCALJKA_TEXTURE_MANAGER_H
+
+#include <map>
+#include <string>
+class Texture;
+
+
+class TextureManager
+{
+    public:
+    TextureManager();
+    ~TextureManager();
+
+    static TextureManager* getInstance();
+    Texture* getTexture(std::string filename);
+    static void DestroyInstance();
+    bool Loaded();
+
+
+
+    private:
+    std::map<std::string, Texture*> textures;
+    static TextureManager* instance;
+    bool bLoaded;
+
+
+
+};
+
+
+#endif