blob: b56a99dfc80d8835ea289f709f820573e4cfcf72 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include "../include/ResourceCache.hpp"
#include "../include/Gif.hpp"
#include "../include/StaticImage.hpp"
#include "../include/WebPagePreview.hpp"
#include <unordered_map>
using namespace std;
namespace dchat
{
static unordered_map<string, sf::Font*> fonts;
static unordered_map<string, sf::Texture*> textures;
static unordered_map<string, sf::Shader*> 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;
}
}
|