Created
March 16, 2016 02:47
-
-
Save dotxnc/25c2b0d84c7891a366d8 to your computer and use it in GitHub Desktop.
Resource Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ResourceLoader.h" | |
bool ResourceLoader::m_done = false; | |
std::string ResourceLoader::datafolder = "data"; | |
std::map<std::string, sf::Texture> ResourceLoader::m_textures; | |
std::map<std::string, sf::Font> ResourceLoader::m_fonts; | |
std::map<std::string, sf::SoundBuffer> ResourceLoader::m_sounds; | |
void ResourceLoader::m_load() | |
{ | |
sf::Clock timer; | |
auto flag = -1; | |
auto data = fs::system_complete(datafolder); | |
std::cout << data << std::endl; | |
fs::directory_iterator it(data), eod; | |
BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod)) | |
{ | |
if (fs::is_regular_file(p)) | |
{ | |
std::ifstream ifs(p.c_str(), std::ios::binary | std::ios::ate); | |
auto pos = ifs.tellg(); | |
int length = pos; | |
auto * pChars = new char[length]; | |
ifs.seekg(0, std::ios::beg); | |
ifs.read(pChars, length); | |
ifs.close(); | |
/// Image | |
if (boost::starts_with(pChars, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")) | |
flag = 1;// printf("png"); | |
else if (boost::starts_with(pChars, "\xFF\xD8\xFF")) | |
flag = 1;// printf("jpg"); | |
else if (boost::starts_with(pChars, "BM")) | |
flag = 1;// printf("bmp"); | |
/// Audio | |
else if (boost::starts_with(pChars, "RIFF")) | |
flag = 2;// printf("wav"); | |
else if (boost::starts_with(pChars, "OggS")) | |
flag = 2;// printf("ogg"); | |
/// Font? | |
else if (boost::starts_with(pChars, "info face")) | |
flag = 3; | |
else if (boost::ends_with(p.c_str(), ".ttf")) | |
flag = 3; | |
else | |
flag = -1; | |
std::vector<fs::path> parts; | |
for (const auto& part : p) | |
parts.emplace_back(part); | |
std::string filename = datafolder + "/" + parts[parts.size() - 1].string(); | |
// remove file extension | |
std::string namewe(filename); | |
for (int i = 0; i < filename.size(); i++) | |
if (filename[i] == '.') | |
namewe = namewe.erase(i, filename.size() - i); | |
sf::Texture _t; | |
sf::Font _f; | |
sf::SoundBuffer _s; | |
switch (flag) | |
{ | |
case 1: // image | |
std::cout << "LOADING TEXTURE: " << filename << std::endl; | |
_t.loadFromFile(filename); | |
m_textures.emplace(namewe, _t); | |
break; | |
case 2: // audio | |
std::cout << "LOADING AUDIO: " << filename << std::endl; | |
_s.loadFromFile(filename); | |
m_sounds.emplace(namewe, _s); | |
break; | |
case 3: // font | |
std::cout << "LOADING FONT: " << filename << std::endl; | |
_f.loadFromFile(filename); | |
m_fonts.emplace(namewe, _f); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
std::cout << "loading everything in " << timer.restart().asSeconds() << "s" << std::endl; | |
m_done = true; | |
} | |
ResourceLoader::ResourceLoader() : | |
m_workerthread(nullptr) | |
{ | |
} | |
ResourceLoader::~ResourceLoader() | |
{ | |
m_workerthread->detach(); | |
delete m_workerthread; | |
} | |
void ResourceLoader::loadResources(std::string datafolder) | |
{ | |
this->datafolder = datafolder; | |
m_workerthread = new std::thread(&m_load); | |
} | |
sf::Texture ResourceLoader::getTexture(std::string name) | |
{ | |
if (m_textures.find(name) != m_textures.end()) | |
return m_textures[name]; | |
return sf::Texture(); | |
} | |
sf::Font ResourceLoader::getFont(std::string name) | |
{ | |
if (m_fonts.find(name) != m_fonts.end()) | |
return m_fonts[name]; | |
return sf::Font(); | |
} | |
sf::SoundBuffer ResourceLoader::getSound(std::string name) | |
{ | |
if (m_sounds.find(name) != m_sounds.end()) | |
return m_sounds[name]; | |
return sf::SoundBuffer(); | |
} | |
bool ResourceLoader::isDone() | |
{ | |
return m_done; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define BOOST_FILESYSTEM_NO_DEPRECATED | |
#define BOOST_FILESYSTEM_NO_LIB | |
#include <iostream> | |
#include <map> | |
#include <SFML/Graphics.hpp> | |
#include <SFML/Audio.hpp> | |
#include <boost/filesystem.hpp> | |
#include <boost/foreach.hpp> | |
#include <boost/algorithm/string.hpp> | |
#include <boost/format.hpp> | |
#include <string> | |
#include <sstream> | |
#include <iomanip> | |
#include <thread> | |
namespace fs = boost::filesystem; | |
using boost::str; | |
using boost::format; | |
struct stringbuilder | |
{ | |
std::stringstream ss; | |
template<typename T> | |
stringbuilder & operator << (const T &data) | |
{ | |
ss << data; | |
return *this; | |
} | |
operator const char*() const { return ss.str().c_str(); } | |
}; | |
class ResourceLoader | |
{ | |
private: | |
static std::map<std::string, sf::Texture> m_textures; | |
static std::map<std::string, sf::Font> m_fonts; | |
static std::map<std::string, sf::SoundBuffer> m_sounds; | |
static bool m_done; | |
static std::string datafolder; | |
static void m_load(); | |
std::thread* m_workerthread; | |
public: | |
ResourceLoader(); | |
~ResourceLoader(); | |
void loadResources(std::string = "data"); | |
sf::Texture getTexture(std::string); | |
sf::Font getFont(std::string); | |
sf::SoundBuffer getSound(std::string); | |
bool isDone(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment