Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Common.h"
- #include "Level.h"
- #include "ResourceManager.h"
- static void* readEntireFile(const std::string& filename, PHYSFS_uint32& size);
- ResourceManager::ResourceManager()
- {
- }
- ResourceManager::~ResourceManager()
- {
- PHYSFS_deinit();
- }
- void ResourceManager::init(const char *argv0)
- {
- PHYSFS_init(argv0);
- std::string prefix = PHYSFS_getBaseDir();
- std::string path;
- path = prefix + "data";
- PHYSFS_mount(path.c_str(), "/", 1);
- path = prefix + "will.pk3";
- PHYSFS_mount(path.c_str(), "/", 1);
- emptyTexture.create(16, 16);
- emptyFont = sf::Font::getDefaultFont();
- }
- void ResourceManager::finish()
- {
- }
- sf::Texture& ResourceManager::art(const std::string& name)
- {
- ArtMap::iterator it = m_loadedArt.find(name);
- if (it != m_loadedArt.end())
- {
- ArtReference& art = it->second;
- art.ref++;
- return art.tex;
- }
- char *extensions[] = { ".png", ".tga", ".bmp", ".jpg", ".gif", NULL };
- char *basepath = "art/";
- std::string path;
- for (int i = 0; extensions[i] != NULL; i++)
- {
- path = basepath + name + extensions[i];
- if (PHYSFS_exists(path.c_str()))
- {
- ArtReference& art = m_loadedArt[name];
- void* buf; PHYSFS_uint32 size;
- buf = readEntireFile(path, size);
- art.tex.loadFromMemory(buf, size);
- art.tex.setSmooth(false);
- art.ref = 1;
- free(buf);
- return art.tex;
- }
- }
- return emptyTexture;
- }
- void ResourceManager::unrefArt(const std::string& name)
- {
- ArtMap::iterator it = m_loadedArt.find(name);
- if (it == m_loadedArt.end()) return;
- ArtReference& art = it->second;
- art.ref--;
- if (art.ref <= 0)
- {
- m_loadedArt.erase(name);
- }
- }
- sf::Font& ResourceManager::font(const std::string& name)
- {
- FontMap::iterator it = m_loadedFonts.find(name);
- if (it != m_loadedFonts.end())
- {
- FontReference& font = it->second;
- font.ref++;
- return font.font;
- }
- char *basepath = "fonts/";
- std::string path = basepath + name + ".ttf";
- if (PHYSFS_exists(path.c_str()))
- {
- FontReference& fnt = m_loadedFonts[name];
- fnt.data = readEntireFile(path, fnt.size);
- fnt.font.loadFromMemory(fnt.data, fnt.size);
- fnt.ref = 1;
- return fnt.font;
- }
- return emptyFont;
- }
- void ResourceManager::unrefFont(const std::string& name)
- {
- FontMap::iterator it = m_loadedFonts.find(name);
- if (it == m_loadedFonts.end()) return;
- FontReference& fnt = it->second;
- fnt.ref--;
- if (fnt.ref <= 0)
- {
- free(fnt.data);
- m_loadedFonts.erase(name);
- }
- }
- sf::Sound& ResourceManager::sfx(const std::string& name)
- {
- SfxMap::iterator it = m_loadedSfx.find(name);
- if (it != m_loadedSfx.end())
- {
- SfxReference& sfx = it->second;
- sfx.ref++;
- return sfx.sound;
- }
- char *extensions[] = { ".wav", ".ogg", ".mp3", ".xm", ".mod", ".mid", NULL };
- char *basepath = "sfx/";
- std::string path;
- for (int i = 0; extensions[i] != NULL; i++)
- {
- path = basepath + name + extensions[i];
- if (PHYSFS_exists(path.c_str()))
- {
- void *data; PHYSFS_uint32 size;
- data = readEntireFile(path, size);
- SfxReference& sfx = m_loadedSfx[name];
- sfx.buf.loadFromMemory(data, size);
- sfx.sound.setBuffer(sfx.buf);
- sfx.ref = 1;
- free(data);
- return sfx.sound;
- }
- }
- return emptySound;
- }
- void ResourceManager::unrefSfx(const std::string& name)
- {
- SfxMap::iterator it = m_loadedSfx.find(name);
- if (it == m_loadedSfx.end()) return;
- SfxReference& sfx = it->second;
- sfx.ref--;
- if (sfx.ref <= 0)
- {
- m_loadedSfx.erase(name);
- }
- }
- sf::Music& ResourceManager::music(const std::string& name)
- {
- MusicMap::iterator it = m_loadedMusic.find(name);
- if (it != m_loadedMusic.end())
- {
- MusicReference& mus = it->second;
- mus.ref++;
- return mus.music;
- }
- char *extensions[] = { ".ogg", ".mp3", ".wav", ".xm", ".mod", ".mid", NULL };
- char *basepath = "music/";
- std::string path;
- for (int i = 0; extensions[i] != NULL; i++)
- {
- path = basepath + name + extensions[i];
- if (PHYSFS_exists(path.c_str()))
- {
- MusicReference& mus = m_loadedMusic[name];
- mus.data = readEntireFile(path, mus.size);
- mus.music.openFromMemory(mus.data, mus.size);
- mus.ref = 1;
- return mus.music;
- }
- }
- return emptyMusic;
- }
- void ResourceManager::unrefMusic(const std::string& name)
- {
- MusicMap::iterator it = m_loadedMusic.find(name);
- if (it == m_loadedMusic.end()) return;
- MusicReference& mus = it->second;
- mus.ref--;
- if (mus.ref <= 0)
- {
- free(mus.data);
- m_loadedMusic.erase(name);
- }
- }
- Level* ResourceManager::lvl(const std::string& name)
- {
- char *basepath = "maps/";
- std::string path = basepath + name + ".lvl";
- if (PHYSFS_exists(path.c_str()))
- {
- return Level::load(path);
- }
- return nullptr;
- }
- static void* readEntireFile(const std::string& filename, PHYSFS_sint64& size)
- {
- if (!PHYSFS_exists(filename.c_str())) return nullptr;
- PHYSFS_file *infile = PHYSFS_openRead(filename.c_str());
- if (!infile)
- {
- return nullptr;
- }
- size = PHYSFS_fileLength(infile);
- void* buf = malloc((size_t)size);
- if (!buf) return nullptr;
- PHYSFS_read(infile, buf, (PHYSFS_uint32)size, 1);
- PHYSFS_close(infile);
- return buf;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement