Sand25

ResourceManager.cpp

Apr 8th, 2017
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ResourceManager.h"
  2. #include <iostream>
  3. //#include "SFGE\Log.h"
  4.  
  5. sfge::ResourceManager* sfge::ResourceManager::mgr = 0;
  6.  
  7. namespace sfge
  8. {
  9.     ResourceManager::ResourceManager() : textureDir(""), audioDir(""), fontDir("")
  10.     {
  11.         //ctor
  12.         //LOG("ResourceManager::ctor()",Logger::Info);
  13.     }
  14.  
  15.     ResourceManager::~ResourceManager()
  16.     {
  17.         //dtor
  18.         //LOG("ResourceManager::dtor()", Logger::Info);
  19.     }
  20.  
  21.     ResourceManager* ResourceManager::instance()
  22.     {
  23.         if(mgr == NULL)
  24.             mgr = new ResourceManager;
  25.  
  26.         return mgr;
  27.     }
  28.  
  29.     sf::Texture& ResourceManager::getTexture(const std::string& name)
  30.     {
  31.         if (name == "")
  32.         {
  33.             if(textures.find("pixel.png") != textures.end())
  34.                 return(*textures["pixel.png"]);
  35.             else
  36.             {
  37.                 sf::Texture* texture = new sf::Texture;
  38.  
  39.                 if (!texture->loadFromFile(textureDir + "pixel.png"))
  40.                     //std::cout << "Could not load image " << textureDir + "pixel.png" << std::endl;
  41.                     //LOG("Could not load image (" + textureDir + name + ")", Logger::Warning);
  42.                 else
  43.                 {
  44.                     textures["pixel.png"] = texture;
  45.                 }
  46.                 return *texture;
  47.             }
  48.         }
  49.  
  50.         if(textures.find(name) != textures.end())
  51.             return(*textures[name]);
  52.         else
  53.         {
  54.             sf::Texture* texture = new sf::Texture;
  55.  
  56.             if (!texture->loadFromFile(textureDir + name))
  57.             {
  58.                 //std::cout << "Could not load image " << textureDir + name << std::endl;
  59.                 //LOG("Could not load image (" + textureDir + name + ")", Logger::Warning);
  60.                 return(getTexture(""));
  61.             }
  62.             else
  63.             {
  64.                 textures[name] = texture;
  65.             }
  66.             return *texture;
  67.         }
  68.     }
  69.  
  70.     sf::SoundBuffer& ResourceManager::getSoundBuffer(const std::string& name)
  71.     {
  72.         if(soundBuffers.find(name) != soundBuffers.end())
  73.             return(*soundBuffers[name]);
  74.         else
  75.         {
  76.             sf::SoundBuffer* sound = new sf::SoundBuffer;
  77.  
  78.             if (!sound->loadFromFile(audioDir + name))
  79.                 //std::cout << "Could not load audio file " << audioDir + name << std::endl;
  80.                 //LOG("Could not load audio file (" + audioDir + name + ")", Logger::Warning);
  81.             else
  82.                 soundBuffers[name] = sound;
  83.             return *sound;
  84.         }
  85.     }
  86.  
  87.     void ResourceManager::releaseAllResources()
  88.     {
  89.         for(auto itr = textures.begin(); itr != textures.end(); itr++)
  90.         {
  91.             //delete the resource pointer
  92.             delete (*itr).second;
  93.             itr = textures.erase(itr);
  94.         }
  95.  
  96.         for(auto itr = soundBuffers.begin(); itr != soundBuffers.end(); itr++)
  97.         {
  98.             //delete the resource pointer
  99.             delete (*itr).second;
  100.             itr = soundBuffers.erase(itr);
  101.         }
  102.  
  103.         for(auto itr = fonts.begin(); itr != fonts.end(); itr++)
  104.         {
  105.             //delete the resource pointer
  106.             delete (*itr).second;
  107.             itr = fonts.erase(itr);
  108.         }
  109.     }
  110.  
  111.     void ResourceManager::releaseResource(const std::string & name)
  112.     {
  113.         if (textures.find(name) != textures.end())
  114.         {
  115.             //delete the resource pointer
  116.             //auto itr = textures[name];
  117.             delete textures[name];
  118.             textures.erase(name);
  119.         }
  120.  
  121.         if (soundBuffers.find(name) != soundBuffers.end())
  122.         {
  123.             //delete the resource pointer
  124.             //auto itr = textures[name];
  125.             delete soundBuffers[name];
  126.             soundBuffers.erase(name);
  127.         }
  128.  
  129.         if (fonts.find(name) != fonts.end())
  130.         {
  131.             //delete the resource pointer
  132.             //auto itr = textures[name];
  133.             delete fonts[name];
  134.             fonts.erase(name);
  135.         }
  136.  
  137.     }
  138.  
  139.     sf::Font& ResourceManager::getFont(const std::string& name)
  140.     {
  141.         if (fonts.find(name) != fonts.end())
  142.             return(*fonts[name]);
  143.         else
  144.         {
  145.             sf::Font* font = new sf::Font;
  146.  
  147.             if (!font->loadFromFile(fontDir + name))
  148.                 //std::cout << "Could not load font file " << fontDir + name;
  149.                 //LOG("Could not load font (" + fontDir + name + ")", Logger::Warning);
  150.             else
  151.                 fonts[name] = font;
  152.             return *font;
  153.         }
  154.     }
  155.  
  156.     void ResourceManager::setTextureDir(const std::string& dir)
  157.     {
  158.         textureDir = dir;
  159.     }
  160.  
  161.     void ResourceManager::setFontDir(const std::string& dir)
  162.     {
  163.         fontDir = dir;
  164.     }
  165.  
  166.     void ResourceManager::setAudioDir(const std::string& dir)
  167.     {
  168.         audioDir = dir;
  169.     }
  170. }
RAW Paste Data