diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Text.cpp | 7 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 8 | ||||
-rw-r--r-- | src/crepe/facade/Font.cpp | 28 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 16 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 3 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.cpp | 80 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.h | 21 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 34 |
9 files changed, 102 insertions, 99 deletions
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 12d01f1..b30ccf6 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,5 +2,8 @@ using namespace crepe; -Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family) : UIObject(id,dimensions,offset),text(text), font_family(font_family){} - +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, + std::string font_family) + : UIObject(id, dimensions, offset), + text(text), + font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 8436611..ebf413b 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -8,16 +8,17 @@ #include "Color.h" #include "UIObject.h" -namespace crepe{ +namespace crepe { /** * \brief Text UIObject component for displaying text * * This class can be used to display text on screen. By setting the font_family to a font already stored on the current device it will automatically be loaded in. */ -class Text : public UIObject{ +class Text : public UIObject { public: - Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family); + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, + std::string font_family); //! Text data that does not have to be set in the constructor struct Data { /** @@ -40,6 +41,7 @@ public: //! Label text color. Color text_color = Color::BLACK; }; + public: //! font family name such as (Arial,Helvetica,Inter) std::string font_family = ""; diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 37dee3a..85b0e13 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -5,21 +5,21 @@ 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(); +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)); + // 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())); - } + // 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(); -} +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 fbc1b8f..983ef31 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -2,9 +2,9 @@ #include <SDL2/SDL_ttf.h> #include <memory> +#include "../Resource.h" #include "../api/Asset.h" #include "../api/Config.h" -#include "../Resource.h" namespace crepe { @@ -15,15 +15,15 @@ namespace crepe { * 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{ +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); + 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 @@ -31,11 +31,11 @@ public: * * \return The raw TTF_Font object wrapped in a unique pointer. */ - TTF_Font* get_font() const; + TTF_Font * get_font() const; private: - //! The SDL_ttf font object with custom deleter. - std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font; + //! 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 274c14a..cbb0f3b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -32,8 +32,8 @@ using namespace std; SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); if (TTF_Init() == -1) { - throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); - } + 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())); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index d7b8365..76cd99a 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -4,8 +4,8 @@ #include <SDL2/SDL_keycode.h> #include <SDL2/SDL_rect.h> #include <SDL2/SDL_render.h> -#include <SDL2/SDL_video.h> #include <SDL2/SDL_ttf.h> +#include <SDL2/SDL_video.h> #include <cmath> #include <functional> #include <memory> @@ -225,7 +225,6 @@ 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 index 851016b..2bccac4 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -2,54 +2,52 @@ #include "SDLFontContext.h" - using namespace crepe; using namespace std; -SDLFontContext::SDLFontContext(){ +SDLFontContext::SDLFontContext() { if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } + throw runtime_error("Failed to initialize Fontconfig."); + } } -SDLFontContext::~SDLFontContext(){ - FcFini(); -} +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) { + + // 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) { 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 || !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)); + 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 index c890b2d..b9e1f23 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,17 +1,17 @@ #pragma once -#include <memory> #include <SDL2/SDL_ttf.h> #include <fontconfig/fontconfig.h> +#include <memory> #include "../api/Asset.h" namespace crepe { - class SDLFontContext{ - public: - SDLFontContext(); - ~SDLFontContext(); - /** +class SDLFontContext { +public: + SDLFontContext(); + ~SDLFontContext(); + /** * * \brief Facade function to convert a font_family into an asset. * @@ -19,8 +19,9 @@ namespace crepe { * * \param font_family Name of the font family name. */ - std::unique_ptr<Asset> get_font_asset(const std::string & font_family); - private: - }; + std::unique_ptr<Asset> get_font_asset(const std::string & font_family); + +private: +}; -} +} // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 52454a1..efd5a98 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,27 +1,27 @@ -#include <iostream> -#include <exception> -#include <memory> -#include <crepe/facade/SDLFontContext.h> #include <crepe/api/Text.h> #include <crepe/facade/Font.h> -#include <crepe/manager/Mediator.h> #include <crepe/facade/SDLContext.h> +#include <crepe/facade/SDLFontContext.h> +#include <crepe/manager/Mediator.h> +#include <exception> +#include <iostream> +#include <memory> using namespace crepe; int main() { - - SDLFontContext font_facade; + + SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; - try{ - // Correct way to create a unique pointer for Text - std::unique_ptr<Text> label = std::make_unique<Text>(1, vec2(100, 100), vec2(0, 0), "test test","OpenSymbol"); - std::unique_ptr<Asset> asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr<Font> font = std::make_unique<Font>(*asset,mediator); - }catch (const std::exception& e) { - std::cout << "Standard exception thrown: " << e.what() << std::endl; + try { + // Correct way to create a unique pointer for Text + std::unique_ptr<Text> label + = std::make_unique<Text>(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol"); + std::unique_ptr<Asset> asset = font_facade.get_font_asset(label->font_family); + std::cout << "path: " << asset->get_path() << std::endl; + std::unique_ptr<Font> font = std::make_unique<Font>(*asset, mediator); + } catch (const std::exception & e) { + std::cout << "Standard exception thrown: " << e.what() << std::endl; } - - return 0; + return 0; } |