aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceLoader.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-17 09:47:45 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-17 09:59:29 +0100
commit453eac7f1f5ef70390ec51087fc1f190811a7507 (patch)
tree21a32ef6de9a3d7c29562484104b56c12518a6f0 /src/ResourceLoader.cpp
parentfc49d40c0d2f6edbbe9dde1f1b53d6a17e9d9f7d (diff)
Replace sfml with mgl
Diffstat (limited to 'src/ResourceLoader.cpp')
-rw-r--r--src/ResourceLoader.cpp60
1 files changed, 39 insertions, 21 deletions
diff --git a/src/ResourceLoader.cpp b/src/ResourceLoader.cpp
index 2725898..89ae510 100644
--- a/src/ResourceLoader.cpp
+++ b/src/ResourceLoader.cpp
@@ -2,16 +2,19 @@
#include "../include/Program.hpp"
#include "../include/Path.hpp"
#include "../include/StringUtils.hpp"
-#include <SFML/Graphics/Font.hpp>
-#include <SFML/Graphics/Texture.hpp>
+#include <mglpp/graphics/Font.hpp>
+#include <mglpp/graphics/Texture.hpp>
+#include <mglpp/system/MemoryMappedFile.hpp>
#include <array>
#include <unordered_map>
#include <malloc.h>
#include <assert.h>
static std::string resource_root;
-static std::array<std::unique_ptr<sf::Font>, 4> font_cache;
-static std::unordered_map<std::string, std::unique_ptr<sf::Texture>> texture_cache;
+static std::array<std::unique_ptr<mgl::MemoryMappedFile>, 4> font_file_cache;
+// font_cache[(unsigned int)font_type][character_size]
+static std::array<std::unordered_map<unsigned int, std::unique_ptr<mgl::Font>>, 4> font_cache;
+static std::unordered_map<std::string, std::unique_ptr<mgl::Texture>> texture_cache;
static bool use_system_fonts = false;
namespace QuickMedia {
@@ -37,10 +40,12 @@ namespace QuickMedia::FontLoader {
return 0;
}
- 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>();
+ mgl::Font* get_font(FontType font_type, unsigned int character_size) {
+ // Make mgl font size match sfml font size
+ character_size += 5;
+
+ mgl::MemoryMappedFile *mapped_file = font_file_cache[(unsigned int)font_type].get();
+ if(!mapped_file) {
std::vector<std::string> noto_directories;
std::string font_file_name;
switch(font_type) {
@@ -95,41 +100,54 @@ namespace QuickMedia::FontLoader {
}
}
- bool successfully_loaded_font = false;
+ bool successfully_loaded_file = false;
+ auto new_memory_mapped_file = std::make_unique<mgl::MemoryMappedFile>();
for(const std::string &noto_dir : noto_directories) {
- if(new_font->loadFromFile(noto_dir + "/" + font_file_name)) {
- successfully_loaded_font = true;
+ if(new_memory_mapped_file->load((noto_dir + "/" + font_file_name).c_str(), mgl::MemoryMappedFile::LoadOptions{true, false})) {
+ successfully_loaded_file = true;
break;
}
}
- if(!successfully_loaded_font)
- fprintf(stderr, "Warning: Failed to load font: %s\n", font_file_name.c_str());
+ if(!successfully_loaded_file) {
+ fprintf(stderr, "Warning: Failed to load font file: %s\n", font_file_name.c_str());
+ abort();
+ }
+
+ mapped_file = new_memory_mapped_file.get();
+ font_file_cache[(unsigned int)font_type] = std::move(new_memory_mapped_file);
+ }
+
+ mgl::Font *font = font_cache[(unsigned int)font_type][character_size].get();
+ if(!font) {
+ auto new_font = std::make_unique<mgl::Font>();
+ if(!new_font->load_from_file(*mapped_file, character_size)) {
+ fprintf(stderr, "Warning: Failed to load font at character size %u\n", character_size);
+ abort();
+ }
font = new_font.get();
- font_cache[(size_t)font_type] = std::move(new_font);
- malloc_trim(0);
+ font_cache[(unsigned int)font_type][character_size] = std::move(new_font);
}
return font;
}
}
namespace QuickMedia::TextureLoader {
- sf::Texture* get_texture(const char *filepath) {
+ mgl::Texture* get_texture(const char *filepath, bool pixel_coordinates) {
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))
+ auto new_texture = std::make_unique<mgl::Texture>();
+ mgl::Texture *result = new_texture.get();
+ if(!new_texture->load_from_file((resource_root + str).c_str(), mgl::Texture::LoadOptions{ false, pixel_coordinates }))
fprintf(stderr, "Failed to load image: %s%s\n", resource_root.c_str(), filepath);
- new_texture->setSmooth(true);
texture_cache[str] = std::move(new_texture);
malloc_trim(0);
return result;
}
-} \ No newline at end of file
+}