aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/Font.cpp17
-rw-r--r--src/crepe/facade/Font.h2
-rw-r--r--src/crepe/facade/SDLFontContext.cpp4
-rw-r--r--src/crepe/facade/SDLFontContext.h6
4 files changed, 14 insertions, 15 deletions
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
index a39af75..f111af9 100644
--- a/src/crepe/facade/Font.cpp
+++ b/src/crepe/facade/Font.cpp
@@ -8,17 +8,12 @@ using namespace crepe;
Font::Font(const Asset & src, Mediator & mediator)
: Resource(src, mediator),
font(nullptr, TTF_CloseFont) {
- // Get the font file path from the Asset
- const std::string font_path = src.get_path();
-
- // Attempt to load the font
- this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size));
-
- // Check if font loading failed
- if (!this->font) {
- throw runtime_error(format("Failed to load font from path: {}. SDL_ttf error: {}",
- font_path, TTF_GetError()));
- }
+ Config & config = Config::get_instance();
+ const std::string FONT_PATH = src.get_path();
+ TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size);
+ if (font == NULL)
+ throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH));
+ this->font = { font, [] (TTF_Font * font) { TTF_CloseFont(font); } };
}
TTF_Font * Font::get_font() const { return this->font.get(); }
diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h
index 983ef31..f7d5b50 100644
--- a/src/crepe/facade/Font.h
+++ b/src/crepe/facade/Font.h
@@ -35,7 +35,7 @@ public:
private:
//! The SDL_ttf font object with custom deleter.
- std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font;
+ std::unique_ptr<TTF_Font, std::function<void(TTF_Font*)>> font;
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp
index 1037ac4..5123b3b 100644
--- a/src/crepe/facade/SDLFontContext.cpp
+++ b/src/crepe/facade/SDLFontContext.cpp
@@ -13,7 +13,7 @@ SDLFontContext::SDLFontContext() {
SDLFontContext::~SDLFontContext() { FcFini(); }
-unique_ptr<Asset> SDLFontContext::get_font_asset(const string font_family) {
+Asset SDLFontContext::get_font_asset(const string font_family) {
// Create a pattern to search for the font family
FcPattern * pattern = FcNameParse(reinterpret_cast<const FcChar8 *>(font_family.c_str()));
@@ -49,5 +49,5 @@ unique_ptr<Asset> SDLFontContext::get_font_asset(const string font_family) {
// Convert the file path to a string
string font_file_path(reinterpret_cast<const char *>(file_path));
FcPatternDestroy(matched_pattern);
- return move(make_unique<Asset>(font_file_path));
+ return Asset(font_file_path);
}
diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h
index efeaa33..cb3ca9d 100644
--- a/src/crepe/facade/SDLFontContext.h
+++ b/src/crepe/facade/SDLFontContext.h
@@ -11,6 +11,10 @@ class SDLFontContext {
public:
SDLFontContext();
~SDLFontContext();
+ SDLFontContext(const SDLFontContext &) = delete;
+ SDLFontContext(SDLFontContext &&) = delete;
+ SDLFontContext & operator=(const SDLFontContext &) = delete;
+ SDLFontContext & operator=(SDLFontContext &&) = delete;
/**
*
* \brief Facade function to convert a font_family into an asset.
@@ -19,7 +23,7 @@ public:
*
* \param font_family Name of the font family name.
*/
- std::unique_ptr<Asset> get_font_asset(const std::string font_family);
+ Asset get_font_asset(const std::string font_family);
private:
};