aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-10 12:27:59 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-10 12:27:59 +0100
commitb2c72ee8ce282bb13b0fbeb2ddb01fdfd6ad1280 (patch)
treef3c678779f326f4b175f91a2b3c7db44d026cd2f /src/crepe
parent29f2d4537cf4d4d4c74c16447a45a5d7034161f4 (diff)
font progress
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Config.h11
-rw-r--r--src/crepe/facade/SDLFontContext.cpp19
-rw-r--r--src/crepe/facade/SDLFontContext.h5
-rw-r--r--src/crepe/facade/font.cpp21
-rw-r--r--src/crepe/facade/font.h47
5 files changed, 94 insertions, 9 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index a9745c3..e4808fe 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -86,6 +86,17 @@ public:
*/
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.
+ *
+ */
+ int font_size = 16;
+ } font;
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp
index 6ce5efd..a4be143 100644
--- a/src/crepe/facade/SDLFontContext.cpp
+++ b/src/crepe/facade/SDLFontContext.cpp
@@ -1,19 +1,22 @@
-#include "SDLFontContext.h"
#include <stdexcept>
+#include "SDLFontContext.h"
+
+
using namespace crepe;
using namespace std;
SDLFontContext::SDLFontContext(){
- if (!FcInit()) {
- throw std::runtime_error("Failed to initialize Fontconfig.");
- }
+
}
SDLFontContext::~SDLFontContext(){
- FcFini();
+
}
-Asset SDLFontContext::get_font_asset(const std::string & font_family) {
+unique_ptr<Asset> SDLFontContext::get_font_asset(const std::string & font_family) {
+ if (!FcInit()) {
+ throw std::runtime_error("Failed to initialize Fontconfig.");
+ }
// Create a pattern to search for the font family
FcPattern* pattern = FcNameParse(reinterpret_cast<const FcChar8*>(font_family.c_str()));
if (!pattern) {
@@ -46,6 +49,6 @@ Asset SDLFontContext::get_font_asset(const std::string & font_family) {
// Convert the file path to a std::string
std::string font_file_path(reinterpret_cast<const char*>(file_path));
FcPatternDestroy(matched_pattern);
-
- return Asset(font_file_path);
+ FcFini();
+ return std::move(make_unique<Asset>(font_file_path));
}
diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h
index 3b2e162..cd91383 100644
--- a/src/crepe/facade/SDLFontContext.h
+++ b/src/crepe/facade/SDLFontContext.h
@@ -1,14 +1,17 @@
#pragma once
+#include <memory>
#include <SDL2/SDL_ttf.h>
#include <fontconfig/fontconfig.h>
+
#include "../api/Asset.h"
+
namespace crepe {
class SDLFontContext{
public:
SDLFontContext();
~SDLFontContext();
- Asset get_font_asset(const std::string & font_family);
+ std::unique_ptr<Asset> get_font_asset(const std::string & font_family);
private:
};
diff --git a/src/crepe/facade/font.cpp b/src/crepe/facade/font.cpp
new file mode 100644
index 0000000..259a6cd
--- /dev/null
+++ b/src/crepe/facade/font.cpp
@@ -0,0 +1,21 @@
+#include "font.h"
+#include "../api/Config.h"
+using namespace std;
+using namespace crepe;
+
+void Font::load(unique_ptr<Asset> res){
+ const char* font_path = res->get_path();
+ int font_size = Config::get_instance().font.font_size;
+ this->font = std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)>(
+ TTF_OpenFont(font_path, font_size), &TTF_CloseFont);
+
+ if (!font) {
+ throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError()));
+ }
+}
+Font::Font(const char* src){
+ this->load(make_unique<Asset>(src));
+}
+Font::Font(std::unique_ptr<Asset> res){
+ this->load(std::move(res));
+}
diff --git a/src/crepe/facade/font.h b/src/crepe/facade/font.h
new file mode 100644
index 0000000..f5c7785
--- /dev/null
+++ b/src/crepe/facade/font.h
@@ -0,0 +1,47 @@
+#pragma once
+
+#include <memory>
+#include <SDL2/SDL_ttf.h>
+
+#include "../api/Asset.h"
+
+namespace crepe {
+
+/**
+ * \brief Font resource facade
+ *
+ * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage.
+ */
+class Font {
+public:
+ /**
+ * \param src The file path to the font file.
+ */
+ Font(const char* src);
+
+ /**
+ * \param res A unique pointer to an Asset holding the font resource.
+ */
+ Font(std::unique_ptr<Asset> res);
+
+ /**
+ * \brief Destructor to clean up font resources.
+ */
+ ~Font() = default;
+
+ void draw(const vec2& pos, const vec2&)
+private:
+ /**
+ * \brief Load the font from the given resource.
+ *
+ * This method is used by both constructors to load the font resource.
+ *
+ * \param res A unique pointer to an Asset holding the font resource.
+ */
+ void load(std::unique_ptr<Asset> res);
+
+private:
+ std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font; ///< The SDL_ttf font object with custom deleter.
+};
+
+} // namespace crepe