From b2c72ee8ce282bb13b0fbeb2ddb01fdfd6ad1280 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 12:27:59 +0100 Subject: font progress --- src/crepe/api/Config.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/crepe/api/Config.h') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index a9745c3..e4808fe 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -86,6 +86,17 @@ public: */ std::string root_pattern = ".crepe-root"; } asset; + //! Default font options + struct { + /** + * \brief Default font size + * + * using the SDL_ttf library the font size needs to be set when loading the font. + * This config option is the font size at which all fonts will be loaded initially. + * + */ + int font_size = 16; + } font; }; } // namespace crepe -- cgit v1.2.3 From e75e355c3a59d53a1d64fd8fae3331b2234083e2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 22:17:43 +0100 Subject: text component pretty much finished --- src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Config.h | 4 ++-- src/crepe/api/Text.cpp | 6 ++++++ src/crepe/api/Text.h | 8 ++++---- src/crepe/facade/Font.cpp | 10 +++++++--- src/crepe/facade/Font.h | 12 ++++++------ src/crepe/facade/SDLFontContext.cpp | 11 +++++------ src/crepe/facade/SDLFontContext.h | 8 ++++++++ src/example/loadfont.cpp | 23 +++++++++++++++++------ 9 files changed, 57 insertions(+), 27 deletions(-) (limited to 'src/crepe/api/Config.h') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index fb11c8d..90e4d1f 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -20,6 +20,7 @@ target_sources(crepe PUBLIC Button.cpp UIObject.cpp AI.cpp + Text.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -49,4 +50,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Button.h UIObject.h AI.h + Text.h ) diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index def4c49..159be99 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -81,11 +81,11 @@ struct Config final { /** * \brief Default font size * - * using the SDL_ttf library the font size needs to be set when loading the font. + * Using the SDL_ttf library the font size needs to be set when loading the font. * This config option is the font size at which all fonts will be loaded initially. * */ - int font_size = 16; + unsigned int size = 16; } font; //! Audio system settings diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index e69de29..dd44dd9 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -0,0 +1,6 @@ +#include "Text.h" + +using namespace crepe; + +Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font) : UIObject(id,dimensions,offset),source(font){} + diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 9dd275b..d5f47fa 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -11,11 +11,11 @@ namespace crepe{ class Text : public UIObject{ public: - Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font, int font_size); + Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font); //! Label text. std::string text; - //! Label text color + //! Label text color. Color text_color = Color::BLACK; /** * \brief fontsize for text rendering @@ -26,8 +26,8 @@ public: * The default font size that is loaded is set in the Config. * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. */ - int font_size = 16; - + unsigned int font_size = 16; + //! Font asset const Asset source; private: }; diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 2ad176a..b600b01 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,24 +1,28 @@ -#include "font.h" #include "../api/Config.h" + +#include "font.h" + using namespace std; using namespace crepe; void Font::load(unique_ptr res){ const char* font_path = res->get_path(); - int font_size = Config::get_instance().font.font_size; this->font = std::unique_ptr( - TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + TTF_OpenFont(font_path, this->default_font_size), &TTF_CloseFont); if (!font) { throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); } } + Font::Font(const char* src){ this->load(make_unique(src)); } + Font::Font(std::unique_ptr res){ this->load(std::move(res)); } + const TTF_Font& Font::get_font() const{ return this->font; } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index e34ac63..8bf9fc9 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -9,17 +9,17 @@ namespace crepe { /** - * \brief Font resource facade + * \brief Resource for managing font creation and destruction * * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. */ class Font : public Resource{ public: - /**. - * \param src Asset with texture data to load. - * \param mediator use the SDLContext reference to load the image + /** + * \param src Asset with font data to load. + * \param mediator use the SDLContext reference to load text */ - Font(const Asset & src, Mediator & mediator); + Font( src, Mediator & mediator); ~Font() = default @@ -27,7 +27,7 @@ public: private: //! The SDL_ttf font object with custom deleter. std::unique_ptr font; - int default_font_size = Config::get_instance().font.font_size; + unsigned int default_font_size = Config::get_instance().font.size; }; } // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index a4be143..d7a0bff 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -7,16 +7,16 @@ using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } } SDLFontContext::~SDLFontContext(){ - + FcFini(); } unique_ptr SDLFontContext::get_font_asset(const std::string & font_family) { - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (!pattern) { @@ -49,6 +49,5 @@ unique_ptr SDLFontContext::get_font_asset(const std::string & font_family // Convert the file path to a std::string std::string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - FcFini(); return std::move(make_unique(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index cd91383..c890b2d 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -11,6 +11,14 @@ namespace crepe { public: SDLFontContext(); ~SDLFontContext(); + /** + * + * \brief Facade function to convert a font_family into an asset. + * + * This function uses the FontConfig library to convert a font family name (Arial, Inter, Helvetica) and converts it to the font source path. + * + * \param font_family Name of the font family name. + */ std::unique_ptr get_font_asset(const std::string & font_family); private: }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 8931ce3..0002026 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,13 +1,24 @@ #include +#include #include - #include +#include using namespace crepe; -int main(){ - SDLFontContext font_facade; - std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); - std::cout << "path: " << asset->get_path() << std::endl; - return 0; +int main() { + SDLFontContext font_facade; + + // Create a unique pointer to the font asset + std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); + std::cout << "path: " << asset->get_path() << std::endl; + try{ + // Correct way to create a unique pointer for Text + std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), *asset); + }catch (const std::exception& e) { + std::cout << "Standard exception thrown: " << e.what() << std::endl; + } + + + return 0; } -- cgit v1.2.3