aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceCache.cpp
blob: 3f731890c3bee343c96e5fd1f4516fb74ba0255d (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
#include "../include/ResourceCache.hpp"
#include <unordered_map>

using namespace std;

namespace dchat
{
    unordered_map<string, sf::Font*> fonts;
    unordered_map<string, sf::Texture*> textures;
    
    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;
    }
}