diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-17 13:22:34 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-17 13:22:34 +0100 |
commit | b99f5fc0f52fdd4ec96be844e643060503a8860b (patch) | |
tree | dbf0efa56ad98debbb2aa288866ec4a1fbf80d55 /src/crepe/facade | |
parent | 3b5b5258b0f46a3492a7fd777908dfb01e15417b (diff) |
text now working with optional
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/Font.cpp | 3 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 10 | ||||
-rw-r--r-- | src/crepe/facade/FontFacade.cpp | 11 | ||||
-rw-r--r-- | src/crepe/facade/FontFacade.h | 12 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 5 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 15 |
6 files changed, 39 insertions, 17 deletions
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 5af943d..558641f 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -9,8 +9,7 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator), - font(nullptr, TTF_CloseFont) { + : Resource(src, mediator){ Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 16f8cb6..b208d96 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -24,14 +24,6 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); - Font(const Font &) = delete; - Font &operator=(const Font &) = delete; - - // Default move constructor and move assignment operator - Font(Font &&) noexcept = delete; - Font &operator=(Font &&) noexcept = delete; - - ~Font() = default; /** * \brief Gets the underlying TTF_Font resource. * @@ -44,7 +36,7 @@ public: private: //! The SDL_ttf font object with custom deleter. - std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font; + std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font = nullptr; }; } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index d447b6d..0a8ba5f 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,5 @@ #include <fontconfig/fontconfig.h> +#include <iostream> #include <stdexcept> #include "FontFacade.h" @@ -6,10 +7,16 @@ using namespace crepe; using namespace std; -Asset FontFacade::get_font_asset(const string& font_family) { +FontFacade::FontFacade(){ if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } +} +FontFacade::~FontFacade(){ + FcFini(); +} +Asset FontFacade::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())); if (pattern == NULL) { @@ -32,7 +39,6 @@ Asset FontFacade::get_font_asset(const string& font_family) { FcPatternDestroy(matched_pattern); throw runtime_error("No matching font found."); } - // Extract the file path FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch @@ -44,6 +50,5 @@ Asset FontFacade::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); - FcFini(); return Asset(font_file_path); } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index fc200d6..2e08f3f 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -13,16 +13,22 @@ namespace crepe { */ class FontFacade { public: + FontFacade(); + ~FontFacade(); + FontFacade(const FontFacade & other) = delete; + FontFacade & operator=(const FontFacade & other) = delete; + FontFacade(FontFacade && other) noexcept = delete; + FontFacade & operator=(FontFacade && other) noexcept = delete; /** * * \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. - * This function is static so the member function can be used without create a FontFacade object. This way it can be used in a constructor as FontFacade::get_font_asset(). + * This function returns a default font path if the font_family name doesnt exist or cant be found * \param font_family Name of the font family name. - * \return Asset with filepath to the font. + * \return Asset with filepath to the corresponding font. */ - static Asset get_font_asset(const std::string& font_family); + Asset get_font_asset(const std::string& font_family); }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d1d109c..8ffaad9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -434,7 +434,12 @@ std::vector<SDLContext::EventData> SDLContext::get_events() { } return event_list; } + void SDLContext::set_color_texture(const Texture & texture, const Color & color) { SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); SDL_SetTextureAlphaMod(texture.get_img(), color.a); } + +Asset SDLContext::get_font_from_name(const std::string& font_family){ + return this->font_facade.get_font_asset(font_family); +} diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index efdd6fe..ffa3cc0 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -17,6 +17,7 @@ #include "api/Transform.h" #include "types.h" +#include "FontFacade.h" namespace crepe { class Texture; @@ -239,6 +240,20 @@ private: * - this is defined in this class because get_events() needs this information aswell */ CameraAuxiliaryData cam_aux_data; +private: + //! instance of the font_facade + FontFacade font_facade{}; +public: + /** + * \brief Function to Get asset from font_family + * + * This function uses the FontFacade function to convert a font_family to an asset. + * + * \param font_family name of the font style that needs to be used (will return an asset with default font path of the font_family doesnt exist) + * + * \return asset with the font style absolute path + */ + Asset get_font_from_name(const std::string& font_family); }; } // namespace crepe |