From 29f2d4537cf4d4d4c74c16447a45a5d7034161f4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 09:47:46 +0100 Subject: sdlfontcontext --- src/crepe/facade/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/crepe/facade/CMakeLists.txt') diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 4cc53bc..9b73323 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp + SDLFontContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -10,5 +11,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h + SDLFontContext.h ) -- cgit v1.2.3 From 3ed4e51e23b5093a44a166b2f11ff66164e5cff1 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:09:55 +0100 Subject: font working --- src/CMakeLists.txt | 2 + src/crepe/facade/CMakeLists.txt | 2 + src/crepe/facade/Font.cpp | 31 +++--- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLContext.cpp | 206 +++++++++++++++------------------------- src/crepe/facade/SDLContext.h | 1 + src/example/loadfont.cpp | 6 +- 7 files changed, 100 insertions(+), 150 deletions(-) (limited to 'src/crepe/facade/CMakeLists.txt') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b8873f..a525c74 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ project(crepe C CXX) find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) +find_package(SDL2_ttf REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) find_package(whereami REQUIRED) @@ -25,6 +26,7 @@ target_include_directories(crepe target_link_libraries(crepe PRIVATE soloud PUBLIC SDL2 + PUBLIC SDL2_ttf PUBLIC SDL2_image PUBLIC ${BERKELEY_DB} PUBLIC whereami diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index f4d71ff..e61b680 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC SDLContext.cpp DB.cpp SDLFontContext.cpp + Font.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SDLContext.h DB.h SDLFontContext.h + Font.h ) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 66835ee..37dee3a 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,28 +1,25 @@ #include "../api/Config.h" -#include "font.h" +#include "Font.h" using namespace std; using namespace crepe; -void Font::load(unique_ptr res){ - const char* font_path = res->get_path(); - this->font = std::unique_ptr( - TTF_OpenFont(font_path, this->default_font_size), &TTF_CloseFont); +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(); - if (!font) { - throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); - } -} + // Attempt to load the font + this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size)); -Font::Font(const char* src){ - this->load(make_unique(src)); -} - -Font::Font(std::unique_ptr res){ - this->load(std::move(res)); + // 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 dd1cebf..a27676b 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -5,7 +5,7 @@ #include "../api/Asset.h" #include "../api/Config.h" - +#include "../Resource.h" namespace crepe { /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8f6c02c..38d12f6 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -18,23 +18,21 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" -#include "../api/Texture.h" #include "../util/Log.h" +#include "manager/Mediator.h" #include "SDLContext.h" +#include "Texture.h" #include "types.h" using namespace crepe; using namespace std; -SDLContext & SDLContext::get_instance() { - static SDLContext instance; - return instance; -} - -SDLContext::SDLContext() { +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())); } @@ -62,6 +60,8 @@ SDLContext::SDLContext() { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + + mediator.sdl_context = *this; } SDLContext::~SDLContext() { @@ -75,14 +75,14 @@ SDLContext::~SDLContext() { // before. IMG_Quit(); SDL_Quit(); + TTF_Quit(); } -Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { +Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { static const std::array LOOKUP_TABLE = [] { std::array table{}; table.fill(Keycode::NONE); - // Map all SDL scancodes to Keycodes table[SDL_SCANCODE_SPACE] = Keycode::SPACE; table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; table[SDL_SCANCODE_COMMA] = Keycode::COMMA; @@ -184,26 +184,12 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { return table; }(); + if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { return Keycode::NONE; } - return LOOKUP_TABLE[sdl_key]; -} -std::array SDLContext::get_keyboard_state() { - // Array to hold the key states (true if pressed, false if not) - std::array keyState{}; - SDL_PumpEvents(); - const Uint8 * current_state = SDL_GetKeyboardState(nullptr); - for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { - Keycode key = sdl_to_keycode(static_cast(i)); - - if (key != Keycode::NONE) { - keyState[key] = current_state[i] != 0; - } - } - - return keyState; + return LOOKUP_TABLE[sdl_key]; } MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { @@ -236,25 +222,19 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { - return SDL_Rect{ - .x = sprite.mask.x, - .y = sprite.mask.y, - .w = sprite.mask.w, - .h = sprite.mask.h, - }; -} - SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; + float aspect_ratio + = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio; + vec2 size = data.size; if (data.size.x == 0 && data.size.y != 0) { - size.x = data.size.y * ctx.sprite.aspect_ratio; + size.x = data.size.y * aspect_ratio; } if (data.size.y == 0 && data.size.x != 0) { - size.y = data.size.x / ctx.sprite.aspect_ratio; + size.y = data.size.x / aspect_ratio; } const CameraValues & cam = ctx.cam; @@ -275,15 +255,24 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { - const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) | (SDL_FLIP_VERTICAL * data.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect srcrect; + SDL_Rect * srcrect_ptr = NULL; + if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) { + srcrect.w = ctx.sprite.mask.w; + srcrect.h = ctx.sprite.mask.h; + srcrect.x = ctx.sprite.mask.x; + srcrect.y = ctx.sprite.mask.y; + srcrect_ptr = &srcrect; + } + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, + .texture = ctx.texture, .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, @@ -291,9 +280,9 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; - this->set_color_texture(ctx.sprite.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + this->set_color_texture(ctx.texture, ctx.sprite.data.color); + SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect, + angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { @@ -355,8 +344,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { return ret_cam; } -uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } - std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { @@ -383,112 +370,71 @@ SDLContext::texture_from_path(const std::string & path) { ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y); return size; } -void SDLContext::delay(int ms) const { SDL_Delay(ms); } - std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; - - // Handle general SDL events while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::SHUTDOWN, + }); break; case SDL_KEYDOWN: - event_list.push_back( - {SDLContext::EventType::KEYDOWN, - {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0}, - {}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::KEYDOWN, + .key = sdl_to_keycode(event.key.keysym.scancode), + .key_repeat = (event.key.repeat != 0), + }); break; case SDL_KEYUP: - event_list.push_back({SDLContext::EventType::KEYUP, - {sdl_to_keycode(event.key.keysym.scancode), false}, - {}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::KEYUP, + .key = sdl_to_keycode(event.key.keysym.scancode), + }); break; case SDL_MOUSEBUTTONDOWN: - event_list.push_back({SDLContext::EventType::MOUSEDOWN, - {}, - {sdl_to_mousebutton(event.button.button), - {event.button.x, event.button.y}}, - {}}); - break; - case SDL_MOUSEBUTTONUP: - event_list.push_back({SDLContext::EventType::MOUSEUP, - {}, - {sdl_to_mousebutton(event.button.button), - {event.button.x, event.button.y}}, - {}}); - break; - case SDL_MOUSEMOTION: - event_list.push_back({SDLContext::EventType::MOUSEMOVE, - {}, - {{}, - {event.motion.x, event.motion.y}, - -1, - INFINITY, - {event.motion.xrel, event.motion.yrel}}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEDOWN, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); break; - case SDL_MOUSEWHEEL: + case SDL_MOUSEBUTTONUP: { + int x, y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEUP, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); + } break; + + case SDL_MOUSEMOTION: { event_list.push_back( - {SDLContext::EventType::MOUSEWHEEL, - {}, - {{}, {}, event.wheel.y < 0 ? -1 : 1, event.wheel.preciseY, {}}, - {}}); - break; - - // Forward window events for further processing - case SDL_WINDOWEVENT: - this->handle_window_event(event.window, event_list); - break; + EventData{.event_type = SDLContext::EventType::MOUSEMOVE, + .mouse_position = {event.motion.x, event.motion.y}, + .rel_mouse_move = {event.motion.xrel, event.motion.yrel}}); + } break; + + case SDL_MOUSEWHEEL: { + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEWHEEL, + .mouse_position = {event.motion.x, event.motion.y}, + // TODO: why is this needed? + .scroll_direction = event.wheel.y < 0 ? -1 : 1, + .scroll_delta = event.wheel.preciseY, + }); + } break; } } - return event_list; } - -// Separate function for SDL_WINDOWEVENT subtypes -void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, - std::vector & event_list) { - switch (window_event.event) { - case SDL_WINDOWEVENT_EXPOSED: - event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_RESIZED: - event_list.push_back({SDLContext::EventType::WINDOW_RESIZE, - {}, - {}, - {{}, {window_event.data1, window_event.data2}}}); - break; - case SDL_WINDOWEVENT_MOVED: - event_list.push_back({SDLContext::EventType::WINDOW_MOVE, - {}, - {}, - {{window_event.data1, window_event.data2}, {}}}); - break; - case SDL_WINDOWEVENT_MINIMIZED: - event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_MAXIMIZED: - event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_FOCUS_GAINED: - event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_FOCUS_LOST: - event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}}); - break; - } -} - void SDLContext::set_color_texture(const Texture & texture, const Color & color) { - SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); - SDL_SetTextureAlphaMod(texture.texture.get(), color.a); + SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); + SDL_SetTextureAlphaMod(texture.get_img(), color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 46b779f..6b725e3 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index fe5466f..52454a1 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -5,17 +5,19 @@ #include #include #include +#include using namespace crepe; - int main() { + SDLFontContext font_facade; Mediator mediator; + SDLContext sdl_context{mediator}; try{ // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test","OpenSymbol"); std::unique_ptr asset = font_facade.get_font_asset(label->font_family); std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr font = make_unique(asset,mediator) + std::unique_ptr font = std::make_unique(*asset,mediator); }catch (const std::exception& e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 55f4aaf179cf723c5b703b0c4d848bf059ae190a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:08:22 +0100 Subject: font family implemented in the text component --- src/crepe/api/Text.cpp | 8 ++++-- src/crepe/api/Text.h | 17 +++++++++--- src/crepe/facade/CMakeLists.txt | 4 +-- src/crepe/facade/FontFacade.cpp | 50 +++++++++++++++++++++++++++++++++ src/crepe/facade/FontFacade.h | 28 +++++++++++++++++++ src/crepe/facade/SDLFontContext.cpp | 55 ------------------------------------- src/crepe/facade/SDLFontContext.h | 29 ------------------- src/example/loadfont.cpp | 21 ++++---------- 8 files changed, 105 insertions(+), 107 deletions(-) create mode 100644 src/crepe/facade/FontFacade.cpp create mode 100644 src/crepe/facade/FontFacade.h delete mode 100644 src/crepe/facade/SDLFontContext.cpp delete mode 100644 src/crepe/facade/SDLFontContext.h (limited to 'src/crepe/facade/CMakeLists.txt') diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 568af7f..e6db140 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,14 @@ +#include "../facade/FontFacade.h" + #include "Text.h" + using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, const std::string & font_family, const Data & data) : UIObject(id, dimensions, offset), text(text), - data(data), - font_family(font_family) {} + data(data), + font(FontFacade::get_font_asset(font_family)) { +} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 64b2008..13b4375 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -5,6 +5,7 @@ #include "../Component.h" #include "Color.h" +#include "Asset.h" #include "UIObject.h" namespace crepe { @@ -39,13 +40,21 @@ public: }; public: + /** + * + * \param dimensions Width and height of the UIObject. + * \param offset Offset of the UIObject relative to its transform + * \param text The text to be displayed. + * \param font_family The font style name to be displayed. + * \param data Data struct containing extra text parameters. + */ Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string &, const std::string & font_family, const Data & data); - //! font family name such as (Arial,Helvetica,Inter) - std::string font_family = ""; + const std::string & text, const std::string & font_family, const Data & data); //! Label text. std::string text = ""; - // Data instance for data not gotten from constructor + //! Font asset variable + Asset font; + //! Data instance Data data; }; diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index e61b680..243ae46 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -4,7 +4,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp - SDLFontContext.cpp + FontFacade.cpp Font.cpp ) @@ -14,7 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h - SDLFontContext.h + FontFacade.h Font.h ) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp new file mode 100644 index 0000000..12e10ed --- /dev/null +++ b/src/crepe/facade/FontFacade.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "FontFacade.h" + +using namespace crepe; +using namespace std; + +Asset FontFacade::get_font_asset(const string font_family) { + if (!FcInit()) { + throw 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 == NULL) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig * config = FcConfigGetCurrent(); + if (config == NULL) { + 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 == NULL) { + 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 == NULL) { + 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(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 new file mode 100644 index 0000000..1b835d4 --- /dev/null +++ b/src/crepe/facade/FontFacade.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "../api/Asset.h" + +namespace crepe { + +/** + * + * \brief Font facade class for converting font family names to absolute file paths + * + */ +class FontFacade { +public: + /** + * + * \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(). + * \param font_family Name of the font family name. + * \return Asset with filepath to the font. + */ + static Asset get_font_asset(const std::string font_family); +}; + +} // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp deleted file mode 100644 index 45d70cb..0000000 --- a/src/crepe/facade/SDLFontContext.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -#include "SDLFontContext.h" - -using namespace crepe; -using namespace std; - -SDLFontContext::SDLFontContext() { - if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } -} - -SDLFontContext::~SDLFontContext() { FcFini(); } - -Asset SDLFontContext::get_font_asset(const string font_family) { - - // Create a pattern to search for the font family - FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (pattern == NULL) { - throw runtime_error("Failed to create font pattern."); - } - - // Default configuration - FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - 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 == NULL) { - 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 == NULL) { - 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(file_path)); - FcPatternDestroy(matched_pattern); - return Asset(font_file_path); -} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h deleted file mode 100644 index d15e1a3..0000000 --- a/src/crepe/facade/SDLFontContext.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - - -#include - -#include "../api/Asset.h" - -namespace crepe { -class SDLFontContext { -public: - SDLFontContext(); - ~SDLFontContext(); - SDLFontContext(const SDLFontContext &) = delete; - SDLFontContext(SDLFontContext &&) = delete; - SDLFontContext & operator=(const SDLFontContext &) = delete; - SDLFontContext & operator=(SDLFontContext &&) = 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. - * - * \param font_family Name of the font family name. - */ - Asset get_font_asset(const std::string font_family); - -}; - -} // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index dae64bc..0ea6d86 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -9,26 +8,18 @@ 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 label = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); - Asset asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset.get_path() << std::endl; - std::unique_ptr font = std::make_unique(asset, mediator); - // Get the TTF_Font from the Font object - TTF_Font * ttf_font = font->get_font(); - //example if the asset is not correct for font - //std::unique_ptr fontThrow = std::make_unique(Asset("../help.txt"), mediator); - // Check if the font is loaded properly - if (ttf_font != nullptr) { - std::cout << "Font successfully loaded!" << std::endl; - } else { - std::cout << "Failed to load font." << std::endl; - } + std::cout << "Path: " << label->font.get_path() << std::endl; + + std::unique_ptr label2 = std::make_unique( + 1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::cout << "Path: " << label2->font.get_path() << std::endl; } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3