#include "../include/ResourceCache.hpp" #include "../include/Gif.hpp" #include "../include/StaticImage.hpp" #include "../include/WebPagePreview.hpp" #include using namespace std; namespace dchat { static unordered_map fonts; static unordered_map textures; static unordered_map shaders; static Cache *cache = nullptr; const sf::Font* ResourceCache::getFont(const string &filepath) { auto it = fonts.find(filepath); if(it != fonts.end()) return it->second; sf::Font *font = new sf::Font(); if(!font->loadFromFile(filepath)) { delete font; string errMsg = "Failed to load font: "; errMsg += filepath; throw FailedToLoadResourceException(errMsg); } fonts[filepath] = font; return font; } sf::Shader* ResourceCache::getShader(const std::string &filepath, sf::Shader::Type shaderType) { auto it = shaders.find(filepath); if(it != shaders.end()) return it->second; sf::Shader *shader = new sf::Shader(); if(!shader->loadFromFile(filepath, shaderType)) { delete shader; string errMsg = "Failed to load shader: "; errMsg += filepath; throw FailedToLoadResourceException(errMsg); } shaders[filepath] = shader; return shader; } Cache* ResourceCache::getCache() { if(!cache) { CreateGifFunc createGifFunc = [](StringView fileContent) { return new SfmlGif(fileContent); }; CreateStaticImageFunc createStaticImageFunc = [](const boost::filesystem::path &filepath) { return new SfmlStaticImage(filepath); }; CreateWebPagePreviewFunc createWebPagePreview = [](const std::string &title, const std::string &description) { return new SfmlWebPagePreview(title, description); }; cache = new Cache(std::move(createGifFunc), std::move(createStaticImageFunc), std::move(createWebPagePreview)); } return cache; } }