aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceCache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ResourceCache.cpp')
-rw-r--r--src/ResourceCache.cpp81
1 files changed, 33 insertions, 48 deletions
diff --git a/src/ResourceCache.cpp b/src/ResourceCache.cpp
index f6f8fad..b56a99d 100644
--- a/src/ResourceCache.cpp
+++ b/src/ResourceCache.cpp
@@ -1,14 +1,18 @@
#include "../include/ResourceCache.hpp"
+#include "../include/Gif.hpp"
+#include "../include/StaticImage.hpp"
+#include "../include/WebPagePreview.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;
+ 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)
{
@@ -29,50 +33,6 @@ namespace dchat
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);
@@ -91,4 +51,29 @@ namespace dchat
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;
+ }
}