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

using namespace std;

namespace dchat
{
    unordered_map<string, sf::Font*> fonts;
    
    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 runtime_error(errMsg);
        }
        
        fonts[filepath] = font;
        return *font;
    }
}