From aa1f1f9460fa713e00fd1830b08be743395110ce Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 14:37:55 +0100 Subject: sdl_ttf to submodules + feedback changes --- src/CMakeLists.txt | 4 ++-- src/crepe/api/Text.cpp | 3 ++- src/crepe/api/Text.h | 4 ++-- src/crepe/facade/Font.cpp | 17 ++++++----------- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLFontContext.cpp | 4 ++-- src/crepe/facade/SDLFontContext.h | 6 +++++- src/example/loadfont.cpp | 8 ++++---- 8 files changed, 24 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a525c74..696856c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,7 +14,7 @@ find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) find_package(whereami REQUIRED) find_library(BERKELEY_DB db) -find_package(Fontconfig REQUIRED) +find_library(FONTCONFIG_LIB fontconfig) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -30,7 +30,7 @@ target_link_libraries(crepe PUBLIC SDL2_image PUBLIC ${BERKELEY_DB} PUBLIC whereami - PUBLIC ${Fontconfig_LIBRARIES} + PUBLIC ${FONTCONFIG_LIB} ) add_subdirectory(crepe) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index c072ee7..9de9038 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -3,7 +3,8 @@ using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, - const std::string & font_family) + const std::string & font_family,const Data & data) : UIObject(id, dimensions, offset), text(text), + data(data), font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 2ad1db3..96e1265 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -15,8 +15,6 @@ namespace crepe { */ class Text : public UIObject { public: - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, - const std::string & font_family); //! Text data that does not have to be set in the constructor struct Data { /** @@ -41,6 +39,8 @@ public: }; public: + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, + const std::string & font_family, const Data& data); //! font family name such as (Arial,Helvetica,Inter) std::string font_family = ""; //! Label text. 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 font; + std::unique_ptr> 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 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(font_family.c_str())); @@ -49,5 +49,5 @@ unique_ptr SDLFontContext::get_font_asset(const string font_family) { // Convert the file path to a string string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - return move(make_unique(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 get_font_asset(const std::string font_family); + Asset get_font_asset(const std::string font_family); private: }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 3cbe559..a52e7f0 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -15,10 +15,10 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label - = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol"); - std::unique_ptr asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr font = std::make_unique(*asset, mediator); + = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol",Text::Data{}); + Asset asset = font_facade.get_font_asset(label->font_family); + std::cout << "path: " << asset.get_path() << std::endl; + std::unique_ptr font = std::make_unique(asset, mediator); // Get the TTF_Font from the Font object TTF_Font* ttf_font = font->get_font(); -- cgit v1.2.3