From 725ea566a2b6a12e0a02e4f570b6e99102e2d21b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 8 Apr 2019 21:04:12 +0200 Subject: Refactor, remove a lot of code and use dchat core instead --- src/ResourceCache.cpp | 81 +++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 48 deletions(-) (limited to 'src/ResourceCache.cpp') 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 -#include using namespace std; namespace dchat { - unordered_map fonts; - unordered_map textures; - unordered_map shaders; + static unordered_map fonts; + static unordered_map textures; + static unordered_map 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; + } } -- cgit v1.2.3