aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceCache.cpp
blob: f6f8fadd1764e5b647cca3984d5d4e2096e60abb (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "../include/ResourceCache.hpp"
#include <unordered_map>
#include <gd.h>

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;

        gdImagePtr imgPtr = gdImageCreateFromFile(filepath.c_str());
        if(!imgPtr)
        {
            string errMsg = "Failed to load texture with gd: ";
            errMsg += filepath;
            throw FailedToLoadResourceException(errMsg);
        }

        gdImageSetInterpolationMethod(imgPtr, GD_BILINEAR_FIXED);
        gdImagePtr newImgPtr = gdImageScale(imgPtr, 100, 100);
        if(!newImgPtr)
        {
            gdImageDestroy(imgPtr);
            string errMsg = "Failed to scale image with gd: ";
            errMsg += filepath;
            throw FailedToLoadResourceException(errMsg);
        }

        gdImageFile(newImgPtr, filepath.c_str());

        gdImageDestroy(imgPtr);
        gdImageDestroy(newImgPtr);
        
        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;
    }
}