Advertisement
Guest User

ResourceManager.cpp

a guest
Apr 28th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include "Common.h"
  2. #include "Level.h"
  3. #include "ResourceManager.h"
  4.  
  5. static void* readEntireFile(const std::string& filename, PHYSFS_uint32& size);
  6.  
  7. ResourceManager::ResourceManager()
  8. {
  9.    
  10. }
  11.  
  12. ResourceManager::~ResourceManager()
  13. {
  14.     PHYSFS_deinit();
  15. }
  16.  
  17. void ResourceManager::init(const char *argv0)
  18. {
  19.     PHYSFS_init(argv0);
  20.    
  21.     std::string prefix = PHYSFS_getBaseDir();
  22.     std::string path;
  23.  
  24.     path = prefix + "data";
  25.     PHYSFS_mount(path.c_str(), "/", 1);
  26.  
  27.     path = prefix + "will.pk3";
  28.     PHYSFS_mount(path.c_str(), "/", 1);
  29.  
  30.     emptyTexture.create(16, 16);
  31.     emptyFont = sf::Font::getDefaultFont();
  32. }
  33.  
  34. void ResourceManager::finish()
  35. {
  36.    
  37. }
  38.  
  39. sf::Texture& ResourceManager::art(const std::string& name)
  40. {
  41.     ArtMap::iterator it = m_loadedArt.find(name);
  42.     if (it != m_loadedArt.end())
  43.     {
  44.         ArtReference& art = it->second;
  45.         art.ref++;
  46.         return art.tex;
  47.     }
  48.  
  49.     char *extensions[] = { ".png", ".tga", ".bmp", ".jpg", ".gif", NULL };
  50.     char *basepath = "art/";
  51.     std::string path;
  52.  
  53.     for (int i = 0; extensions[i] != NULL; i++)
  54.     {
  55.         path = basepath + name + extensions[i];
  56.         if (PHYSFS_exists(path.c_str()))
  57.         {
  58.             ArtReference& art = m_loadedArt[name];
  59.             void* buf; PHYSFS_uint32 size;
  60.  
  61.             buf = readEntireFile(path, size);
  62.            
  63.             art.tex.loadFromMemory(buf, size);
  64.             art.tex.setSmooth(false);          
  65.             art.ref = 1;
  66.  
  67.             free(buf);
  68.             return art.tex;
  69.         }
  70.     }
  71.  
  72.     return emptyTexture;
  73. }
  74.  
  75. void ResourceManager::unrefArt(const std::string& name)
  76. {
  77.     ArtMap::iterator it = m_loadedArt.find(name);
  78.     if (it == m_loadedArt.end()) return;
  79.  
  80.     ArtReference& art = it->second;
  81.     art.ref--;
  82.     if (art.ref <= 0)
  83.     {
  84.         m_loadedArt.erase(name);
  85.     }
  86. }
  87.  
  88. sf::Font& ResourceManager::font(const std::string& name)
  89. {
  90.     FontMap::iterator it = m_loadedFonts.find(name);
  91.     if (it != m_loadedFonts.end())
  92.     {
  93.         FontReference& font = it->second;
  94.         font.ref++;
  95.         return font.font;
  96.     }
  97.  
  98.     char *basepath = "fonts/";
  99.     std::string path = basepath + name + ".ttf";
  100.     if (PHYSFS_exists(path.c_str()))
  101.     {
  102.         FontReference& fnt = m_loadedFonts[name];
  103.         fnt.data = readEntireFile(path, fnt.size);
  104.         fnt.font.loadFromMemory(fnt.data, fnt.size);
  105.         fnt.ref = 1;
  106.         return fnt.font;
  107.     }
  108.  
  109.     return emptyFont;
  110. }
  111.  
  112. void ResourceManager::unrefFont(const std::string& name)
  113. {
  114.     FontMap::iterator it = m_loadedFonts.find(name);
  115.     if (it == m_loadedFonts.end()) return;
  116.  
  117.     FontReference& fnt = it->second;
  118.     fnt.ref--;
  119.     if (fnt.ref <= 0)
  120.     {
  121.         free(fnt.data);
  122.         m_loadedFonts.erase(name);
  123.     }
  124. }
  125.  
  126. sf::Sound& ResourceManager::sfx(const std::string& name)
  127. {
  128.     SfxMap::iterator it = m_loadedSfx.find(name);
  129.     if (it != m_loadedSfx.end())
  130.     {
  131.         SfxReference& sfx = it->second;
  132.         sfx.ref++;
  133.         return sfx.sound;
  134.     }
  135.  
  136.     char *extensions[] = { ".wav", ".ogg", ".mp3", ".xm", ".mod", ".mid", NULL };
  137.     char *basepath = "sfx/";
  138.     std::string path;
  139.  
  140.     for (int i = 0; extensions[i] != NULL; i++)
  141.     {
  142.         path = basepath + name + extensions[i];
  143.         if (PHYSFS_exists(path.c_str()))
  144.         {
  145.             void *data; PHYSFS_uint32 size;
  146.  
  147.             data = readEntireFile(path, size);
  148.  
  149.             SfxReference& sfx = m_loadedSfx[name];
  150.             sfx.buf.loadFromMemory(data, size);
  151.             sfx.sound.setBuffer(sfx.buf);
  152.             sfx.ref = 1;
  153.  
  154.             free(data);
  155.             return sfx.sound;
  156.         }
  157.     }
  158.  
  159.     return emptySound;
  160. }
  161.  
  162. void ResourceManager::unrefSfx(const std::string& name)
  163. {
  164.     SfxMap::iterator it = m_loadedSfx.find(name);
  165.     if (it == m_loadedSfx.end()) return;
  166.  
  167.     SfxReference& sfx = it->second;
  168.     sfx.ref--;
  169.     if (sfx.ref <= 0)
  170.     {
  171.         m_loadedSfx.erase(name);
  172.     }
  173. }
  174.  
  175. sf::Music& ResourceManager::music(const std::string& name)
  176. {
  177.     MusicMap::iterator it = m_loadedMusic.find(name);
  178.     if (it != m_loadedMusic.end())
  179.     {
  180.         MusicReference& mus = it->second;
  181.         mus.ref++;
  182.         return mus.music;
  183.     }
  184.  
  185.     char *extensions[] =  { ".ogg", ".mp3", ".wav", ".xm", ".mod", ".mid", NULL };
  186.     char *basepath = "music/";
  187.     std::string path;
  188.  
  189.     for (int i = 0; extensions[i] != NULL; i++)
  190.     {
  191.         path = basepath + name + extensions[i];
  192.         if (PHYSFS_exists(path.c_str()))
  193.         {
  194.             MusicReference& mus = m_loadedMusic[name];
  195.             mus.data = readEntireFile(path, mus.size);
  196.             mus.music.openFromMemory(mus.data, mus.size);
  197.             mus.ref = 1;
  198.  
  199.             return mus.music;
  200.         }
  201.     }
  202.  
  203.     return emptyMusic;
  204. }
  205.  
  206. void ResourceManager::unrefMusic(const std::string& name)
  207. {
  208.     MusicMap::iterator it = m_loadedMusic.find(name);
  209.     if (it == m_loadedMusic.end()) return;
  210.  
  211.     MusicReference& mus = it->second;
  212.     mus.ref--;
  213.     if (mus.ref <= 0)
  214.     {
  215.         free(mus.data);
  216.         m_loadedMusic.erase(name);
  217.     }
  218. }
  219.  
  220. Level* ResourceManager::lvl(const std::string& name)
  221. {
  222.     char *basepath = "maps/";
  223.     std::string path = basepath + name + ".lvl";
  224.  
  225.     if (PHYSFS_exists(path.c_str()))
  226.     {
  227.         return Level::load(path);
  228.     }
  229.  
  230.     return nullptr;
  231. }
  232.  
  233. static void* readEntireFile(const std::string& filename, PHYSFS_sint64& size)
  234. {
  235.     if (!PHYSFS_exists(filename.c_str())) return nullptr;
  236.  
  237.     PHYSFS_file *infile = PHYSFS_openRead(filename.c_str());
  238.     if (!infile)
  239.     {
  240.         return nullptr;
  241.     }
  242.  
  243.     size = PHYSFS_fileLength(infile);
  244.     void* buf = malloc((size_t)size);
  245.     if (!buf) return nullptr;
  246.  
  247.     PHYSFS_read(infile, buf, (PHYSFS_uint32)size, 1);
  248.     PHYSFS_close(infile);
  249.     return buf;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement