diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Config.h | 11 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.cpp | 19 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.h | 5 | ||||
-rw-r--r-- | src/crepe/facade/font.cpp | 21 | ||||
-rw-r--r-- | src/crepe/facade/font.h | 47 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 13 |
7 files changed, 108 insertions, 9 deletions
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 diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 6ce5efd..a4be143 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -1,19 +1,22 @@ -#include "SDLFontContext.h" #include <stdexcept> +#include "SDLFontContext.h" + + using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + } SDLFontContext::~SDLFontContext(){ - FcFini(); + } -Asset SDLFontContext::get_font_asset(const std::string & font_family) { +unique_ptr<Asset> 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<const FcChar8*>(font_family.c_str())); if (!pattern) { @@ -46,6 +49,6 @@ Asset SDLFontContext::get_font_asset(const std::string & font_family) { // Convert the file path to a std::string std::string font_file_path(reinterpret_cast<const char*>(file_path)); FcPatternDestroy(matched_pattern); - - return Asset(font_file_path); + FcFini(); + return std::move(make_unique<Asset>(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index 3b2e162..cd91383 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,14 +1,17 @@ #pragma once +#include <memory> #include <SDL2/SDL_ttf.h> #include <fontconfig/fontconfig.h> + #include "../api/Asset.h" + namespace crepe { class SDLFontContext{ public: SDLFontContext(); ~SDLFontContext(); - Asset get_font_asset(const std::string & font_family); + std::unique_ptr<Asset> get_font_asset(const std::string & font_family); private: }; diff --git a/src/crepe/facade/font.cpp b/src/crepe/facade/font.cpp new file mode 100644 index 0000000..259a6cd --- /dev/null +++ b/src/crepe/facade/font.cpp @@ -0,0 +1,21 @@ +#include "font.h" +#include "../api/Config.h" +using namespace std; +using namespace crepe; + +void Font::load(unique_ptr<Asset> res){ + const char* font_path = res->get_path(); + int font_size = Config::get_instance().font.font_size; + this->font = std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)>( + TTF_OpenFont(font_path, 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<Asset>(src)); +} +Font::Font(std::unique_ptr<Asset> res){ + this->load(std::move(res)); +} diff --git a/src/crepe/facade/font.h b/src/crepe/facade/font.h new file mode 100644 index 0000000..f5c7785 --- /dev/null +++ b/src/crepe/facade/font.h @@ -0,0 +1,47 @@ +#pragma once + +#include <memory> +#include <SDL2/SDL_ttf.h> + +#include "../api/Asset.h" + +namespace crepe { + +/** + * \brief Font resource facade + * + * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. + */ +class Font { +public: + /** + * \param src The file path to the font file. + */ + Font(const char* src); + + /** + * \param res A unique pointer to an Asset holding the font resource. + */ + Font(std::unique_ptr<Asset> res); + + /** + * \brief Destructor to clean up font resources. + */ + ~Font() = default; + + void draw(const vec2& pos, const vec2&) +private: + /** + * \brief Load the font from the given resource. + * + * This method is used by both constructors to load the font resource. + * + * \param res A unique pointer to an Asset holding the font resource. + */ + void load(std::unique_ptr<Asset> res); + +private: + std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font; ///< The SDL_ttf font object with custom deleter. +}; + +} // namespace crepe diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8ef71bb..7fc81d7 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -21,3 +21,4 @@ add_example(savemgr) add_example(rendering_particle) add_example(game) add_example(button) +add_example(loadfont) diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp new file mode 100644 index 0000000..8931ce3 --- /dev/null +++ b/src/example/loadfont.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <memory> + +#include <crepe/facade/SDLFontContext.h> + +using namespace crepe; + +int main(){ + SDLFontContext font_facade; + std::unique_ptr<Asset> asset = font_facade.get_font_asset("OpenSymbol"); + std::cout << "path: " << asset->get_path() << std::endl; + return 0; +} |