aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-16 19:08:22 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-16 19:08:22 +0100
commit55f4aaf179cf723c5b703b0c4d848bf059ae190a (patch)
tree56b7034d665783384415abc1a0e9282c194ae1c3 /src/crepe
parent7f7f1237532b4318361e69349cc38f34fe981803 (diff)
font family implemented in the text component
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Text.cpp8
-rw-r--r--src/crepe/api/Text.h17
-rw-r--r--src/crepe/facade/CMakeLists.txt4
-rw-r--r--src/crepe/facade/FontFacade.cpp (renamed from src/crepe/facade/SDLFontContext.cpp)11
-rw-r--r--src/crepe/facade/FontFacade.h28
-rw-r--r--src/crepe/facade/SDLFontContext.h29
6 files changed, 52 insertions, 45 deletions
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/SDLFontContext.cpp b/src/crepe/facade/FontFacade.cpp
index 45d70cb..12e10ed 100644
--- a/src/crepe/facade/SDLFontContext.cpp
+++ b/src/crepe/facade/FontFacade.cpp
@@ -2,21 +2,15 @@
#include <SDL2/SDL_ttf.h>
#include <fontconfig/fontconfig.h>
-#include "SDLFontContext.h"
+#include "FontFacade.h"
using namespace crepe;
using namespace std;
-SDLFontContext::SDLFontContext() {
+Asset FontFacade::get_font_asset(const string font_family) {
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<const FcChar8 *>(font_family.c_str()));
if (pattern == NULL) {
@@ -51,5 +45,6 @@ Asset SDLFontContext::get_font_asset(const string font_family) {
// Convert the file path to a string
string font_file_path(reinterpret_cast<const char *>(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 <memory>
+
+#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.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 <memory>
-
-#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