diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/facade/Font.cpp | 25 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 41 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.cpp | 53 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.h | 26 |
7 files changed, 156 insertions, 1 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 0598e16..e61b680 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -4,6 +4,8 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp + SDLFontContext.cpp + Font.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,5 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h + SDLFontContext.h + Font.h ) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp new file mode 100644 index 0000000..37dee3a --- /dev/null +++ b/src/crepe/facade/Font.cpp @@ -0,0 +1,25 @@ +#include "../api/Config.h" + +#include "Font.h" + +using namespace std; +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())); + } +} + +TTF_Font* Font::get_font() const { + return this->font.get(); +} diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h new file mode 100644 index 0000000..fbc1b8f --- /dev/null +++ b/src/crepe/facade/Font.h @@ -0,0 +1,41 @@ +#pragma once +#include <SDL2/SDL_ttf.h> +#include <memory> + +#include "../api/Asset.h" +#include "../api/Config.h" +#include "../Resource.h" + +namespace crepe { + +/** + * \brief Resource for managing font creation and destruction + * + * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. + * It loads a font from an Asset and manages its lifecycle. The font is automatically unloaded + * when this object is destroyed. + */ +class Font : public Resource{ +public: + /** + * \param src The Asset containing the font file path and metadata to load the font. + * \param mediator The Mediator object used for managing the SDL context or related systems. + */ + Font(const Asset & src, Mediator & mediator); + + /** + * \brief Gets the underlying TTF_Font resource. + * + * This function returns the raw pointer to the SDL_ttf TTF_Font object that represents + * the loaded font. This can be used with SDL_ttf functions to render text. + * + * \return The raw TTF_Font object wrapped in a unique pointer. + */ + TTF_Font* get_font() const; + +private: + //! The SDL_ttf font object with custom deleter. + std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font; +}; + +} // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 20bb030..274c14a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -31,6 +31,9 @@ using namespace std; SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); + if (TTF_Init() == -1) { + throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); + } if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -73,6 +76,7 @@ SDLContext::~SDLContext() { // before. IMG_Quit(); SDL_Quit(); + TTF_Quit(); } Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index bcadf87..554880f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -5,6 +5,7 @@ #include <SDL2/SDL_rect.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_video.h> +#include <SDL2/SDL_ttf.h> #include <cmath> #include <functional> #include <memory> @@ -15,9 +16,9 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" - #include "types.h" + namespace crepe { class Texture; @@ -225,6 +226,7 @@ public: void set_color_texture(const Texture & texture, const Color & color); private: + //! sdl Window std::unique_ptr<SDL_Window, std::function<void(SDL_Window *)>> game_window; diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp new file mode 100644 index 0000000..e7f04cc --- /dev/null +++ b/src/crepe/facade/SDLFontContext.cpp @@ -0,0 +1,53 @@ +#include <stdexcept> + +#include "SDLFontContext.h" + + +using namespace crepe; +using namespace std; + +SDLFontContext::SDLFontContext(){ + if (!FcInit()) { + throw runtime_error("Failed to initialize Fontconfig."); + } +} + +SDLFontContext::~SDLFontContext(){ + FcFini(); +} +unique_ptr<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())); + if (!pattern) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig* config = FcConfigGetCurrent(); + if (!config) { + FcPatternDestroy(pattern); + throw runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern* matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (!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 || !file_path) { + FcPatternDestroy(matched_pattern); + throw runtime_error("Failed to get font file path."); + } + + // 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)); +} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h new file mode 100644 index 0000000..c890b2d --- /dev/null +++ b/src/crepe/facade/SDLFontContext.h @@ -0,0 +1,26 @@ +#pragma once + +#include <memory> +#include <SDL2/SDL_ttf.h> +#include <fontconfig/fontconfig.h> + +#include "../api/Asset.h" + +namespace crepe { + class SDLFontContext{ + 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<Asset> get_font_asset(const std::string & font_family); + private: + }; + +} |