blob: 4664bf012f2ff88fc87a241d9f2aa86b21d857a8 (
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
|
#include "../include/ResourceCache.hpp"
#include <unordered_map>
using namespace std;
namespace dchat
{
unordered_map<string, sf::Font*> fonts;
unordered_map<string, sf::Texture*> textures;
unordered_map<string, sf::Shader*> shaders;
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::Texture* ResourceCache::getTexture(const string &filepath)
{
auto it = textures.find(filepath);
if(it != textures.end())
return it->second;
sf::Texture *texture = new sf::Texture();
if(!texture->loadFromFile(filepath))
{
delete texture;
string errMsg = "Failed to load texture: ";
errMsg += filepath;
throw FailedToLoadResourceException(errMsg);
}
texture->setSmooth(true);
texture->generateMipmap();
textures[filepath] = texture;
return texture;
}
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;
}
}
|