aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceLoader.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-11-21 18:20:00 +0100
committerdec05eba <dec05eba@protonmail.com>2020-11-21 18:20:00 +0100
commit44e66882f6e517b06522cb1e510ed9dea7574273 (patch)
treec25095570a1a9438d267b724236fefd22e68aeed /src/ResourceLoader.cpp
parent9d36cfb599490888fa54110c796e14b542c402df (diff)
Render emoji in text, do not show notification count for cache sync, lazy load 4chan board
Diffstat (limited to 'src/ResourceLoader.cpp')
-rw-r--r--src/ResourceLoader.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/ResourceLoader.cpp b/src/ResourceLoader.cpp
new file mode 100644
index 0000000..9a6060d
--- /dev/null
+++ b/src/ResourceLoader.cpp
@@ -0,0 +1,79 @@
+#include "../include/ResourceLoader.hpp"
+#include <SFML/Graphics/Font.hpp>
+#include <SFML/Graphics/Texture.hpp>
+#include <array>
+#include <unordered_map>
+#include <assert.h>
+
+static std::string resource_root;
+static std::array<std::unique_ptr<sf::Font>, 3> font_cache;
+static std::unordered_map<std::string, std::unique_ptr<sf::Texture>> texture_cache;
+
+namespace QuickMedia {
+ void set_resource_loader_root_path(const char *new_resource_root) {
+ resource_root = new_resource_root;
+ }
+}
+
+namespace QuickMedia::FontLoader {
+ sf::Font* get_font(FontType font_type) {
+ sf::Font *font = font_cache[(size_t)font_type].get();
+ if(!font) {
+ auto new_font = std::make_unique<sf::Font>();
+ std::vector<std::string> noto_directories;
+ std::string font_file_name;
+ switch(font_type) {
+ case FontType::LATIN: {
+ noto_directories.push_back("/usr/share/fonts/noto");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto");
+ font_file_name = "NotoSans-Regular.ttf";
+ break;
+ }
+ case FontType::LATIN_BOLD: {
+ noto_directories.push_back("/usr/share/fonts/noto");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto");
+ font_file_name = "NotoSans-Bold.ttf";
+ break;
+ }
+ case FontType::CJK: {
+ noto_directories.push_back("/usr/share/fonts/noto-cjk");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto-cjk");
+ font_file_name = "NotoSansCJK-Regular.ttc";
+ break;
+ }
+ case FontType::EMOJI: {
+ noto_directories.push_back("/usr/share/fonts/noto");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto");
+ font_file_name = "NotoColorEmoji.ttf";
+ break;
+ }
+ }
+
+ for(const std::string &noto_dir : noto_directories) {
+ if(new_font->loadFromFile(noto_dir + "/" + font_file_name))
+ break;
+ }
+
+ font_cache[(size_t)font_type] = std::move(new_font);
+ font = font_cache[(size_t)font_type].get();
+ }
+ return font;
+ }
+}
+
+namespace QuickMedia::TextureLoader {
+ sf::Texture* get_texture(const char *filepath) {
+ assert(!resource_root.empty());
+ std::string str = filepath;
+ auto it = texture_cache.find(str);
+ if(it != texture_cache.end())
+ return it->second.get();
+
+ auto new_texture = std::make_unique<sf::Texture>();
+ sf::Texture *result = new_texture.get();
+ if(!new_texture->loadFromFile(resource_root + str))
+ fprintf(stderr, "Failed to load image: %s%s\n", resource_root.c_str(), filepath);
+ texture_cache[str] = std::move(new_texture);
+ return result;
+ }
+} \ No newline at end of file