diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/Font.cpp | 2 | ||||
-rw-r--r-- | src/crepe/facade/FontFacade.cpp | 48 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 1 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 32 |
4 files changed, 38 insertions, 45 deletions
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 4694f7c..0c670c1 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -10,7 +10,7 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { - Config & config = Config::get_instance(); + const 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/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index cec3507..87f95ab 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,51 +1,43 @@ #include <fontconfig/fontconfig.h> -#include <iostream> +#include <functional> +#include <memory> #include <stdexcept> +#include <string> #include "FontFacade.h" -using namespace crepe; using namespace std; +using namespace crepe; FontFacade::FontFacade() { - if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } + if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig."); } + FontFacade::~FontFacade() { FcFini(); } + Asset FontFacade::get_font_asset(const string & font_family) { + FcPattern * raw_pattern + = FcNameParse(reinterpret_cast<const FcChar8 *>(font_family.c_str())); + if (raw_pattern == NULL) throw runtime_error("Failed to create font 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."); - } + unique_ptr<FcPattern, function<void(FcPattern *)>> pattern{ + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; - // Default configuration FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - // FcPatternDestroy(pattern); - throw runtime_error("Failed to get current Fontconfig configuration."); - } + if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration."); - // Match the font pattern FcResult result; - FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); + FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); + if (raw_matched_pattern == NULL) throw runtime_error("No matching font found."); + + unique_ptr<FcPattern, function<void(FcPattern *)>> matched_pattern + = {raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; - 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 == NULL) { - // FcPatternDestroy(matched_pattern); + FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path); + if (res != FcResultMatch || file_path == NULL) 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 Asset(font_file_path); } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ada560b..ea3b71c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -7,6 +7,7 @@ #include <SDL2/SDL_render.h> #include <SDL2/SDL_surface.h> #include <SDL2/SDL_ttf.h> +#include <SDL2/SDL_video.h> #include <array> #include <cmath> #include <cstddef> diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 01d82a0..1dada74 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -18,7 +18,9 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" +#include "types.h" +#include "EventData.h" #include "FontFacade.h" namespace crepe { @@ -251,6 +253,20 @@ private: 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); //! variable to store the state of each key (true = pressed, false = not pressed) keyboard_state_t keyboard_state; //! lookup table for converting SDL_SCANCODES to Keycodes @@ -353,22 +369,6 @@ private: {SDL_SCANCODE_RALT, Keycode::RIGHT_ALT}, {SDL_SCANCODE_RGUI, Keycode::RIGHT_SUPER}, {SDL_SCANCODE_MENU, Keycode::MENU}}; - -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 |