diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 15:54:17 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 15:54:17 +0100 |
commit | 8d53634e2f47a616ff90f114fe1afbc2ba4df094 (patch) | |
tree | 5a5e11e9c54c845a5dd570bb76aa73724acc5dd9 /src/crepe | |
parent | 0524c68cc4eb9fbd36c17ed4f5d8152cac2f977b (diff) | |
parent | a8ccf7fe8662086bb223aa4eafd0f85e717d16cf (diff) |
Merge branch 'master' into niels/UI
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Asset.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 11 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 1 | ||||
-rw-r--r-- | src/crepe/api/Text.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 3 | ||||
-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 |
9 files changed, 53 insertions, 53 deletions
diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index b367a92..bfd0ac7 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -37,7 +37,7 @@ public: private: //! path to asset - std::string src; + const std::string src; private: /** diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index e91ecd1..074c113 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -80,6 +80,17 @@ struct Config final { */ 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. + * + */ + unsigned int size = 16; + } font; //! Configuration for click tolerance. struct { //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click. diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 5d49223..65306cd 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -201,7 +201,6 @@ private: * * \{ */ - //! Game object ID of game object parent BehaviorScript is attached to game_object_id_t game_object_id; //! Reference to parent component diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 2e248de..9d57abd 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -6,10 +6,8 @@ using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & font_family, const Data & data, const std::string & text, - std::optional<Asset> font) + const std::string & font_family, const Data & data, const std::string & text) : UIObject(id, dimensions, offset), text(text), data(data), - font_family(font_family), - font(font) {} + font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 92cca18..c30dc80 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -51,8 +51,7 @@ public: * \param font Optional font asset that can be passed or left empty. */ Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & font_family, const Data & data, const std::string & text = "", - std::optional<Asset> font = std::nullopt); + const std::string & font_family, const Data & data, const std::string & text = ""); //! Label text. std::string text = ""; 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 |