aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent7f7f1237532b4318361e69349cc38f34fe981803 (diff)
font family implemented in the text component
Diffstat (limited to 'src')
-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
-rw-r--r--src/example/loadfont.cpp21
7 files changed, 58 insertions, 60 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
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 <crepe/api/Text.h>
#include <crepe/facade/Font.h>
#include <crepe/facade/SDLContext.h>
-#include <crepe/facade/SDLFontContext.h>
#include <crepe/manager/Mediator.h>
#include <exception>
#include <iostream>
@@ -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<Text> label = std::make_unique<Text>(
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> font = std::make_unique<Font>(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<Font> fontThrow = std::make_unique<Font>(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<Text> label2 = std::make_unique<Text>(
+ 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;
}