aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-11 22:17:43 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-11 22:17:43 +0100
commite75e355c3a59d53a1d64fd8fae3331b2234083e2 (patch)
tree24176a074d529271e7279794f7fa2dcf5aed2525 /src
parentbb2a6f1a0ab19f6cbc78b2747a26920f24bf13ca (diff)
text component pretty much finished
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/Config.h4
-rw-r--r--src/crepe/api/Text.cpp6
-rw-r--r--src/crepe/api/Text.h8
-rw-r--r--src/crepe/facade/Font.cpp10
-rw-r--r--src/crepe/facade/Font.h12
-rw-r--r--src/crepe/facade/SDLFontContext.cpp11
-rw-r--r--src/crepe/facade/SDLFontContext.h8
-rw-r--r--src/example/loadfont.cpp23
9 files changed, 57 insertions, 27 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index fb11c8d..90e4d1f 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -20,6 +20,7 @@ target_sources(crepe PUBLIC
Button.cpp
UIObject.cpp
AI.cpp
+ Text.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -49,4 +50,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Button.h
UIObject.h
AI.h
+ Text.h
)
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index def4c49..159be99 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -81,11 +81,11 @@ struct Config final {
/**
* \brief Default font size
*
- * using the SDL_ttf library the font size needs to be set when loading the font.
+ * 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;
+ unsigned int size = 16;
} font;
//! Audio system settings
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index e69de29..dd44dd9 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -0,0 +1,6 @@
+#include "Text.h"
+
+using namespace crepe;
+
+Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font) : UIObject(id,dimensions,offset),source(font){}
+
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
index 9dd275b..d5f47fa 100644
--- a/src/crepe/api/Text.h
+++ b/src/crepe/api/Text.h
@@ -11,11 +11,11 @@
namespace crepe{
class Text : public UIObject{
public:
- Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font, int font_size);
+ Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font);
//! Label text.
std::string text;
- //! Label text color
+ //! Label text color.
Color text_color = Color::BLACK;
/**
* \brief fontsize for text rendering
@@ -26,8 +26,8 @@ public:
* The default font size that is loaded is set in the Config.
* Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much.
*/
- int font_size = 16;
-
+ unsigned int font_size = 16;
+ //! Font asset
const Asset source;
private:
};
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
index 2ad176a..b600b01 100644
--- a/src/crepe/facade/Font.cpp
+++ b/src/crepe/facade/Font.cpp
@@ -1,24 +1,28 @@
-#include "font.h"
#include "../api/Config.h"
+
+#include "font.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);
+ TTF_OpenFont(font_path, this->default_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));
}
+
const TTF_Font& Font::get_font() const{
return this->font;
}
diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h
index e34ac63..8bf9fc9 100644
--- a/src/crepe/facade/Font.h
+++ b/src/crepe/facade/Font.h
@@ -9,17 +9,17 @@
namespace crepe {
/**
- * \brief Font resource facade
+ * \brief Resource for managing font creation and destruction
*
* This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage.
*/
class Font : public Resource{
public:
- /**.
- * \param src Asset with texture data to load.
- * \param mediator use the SDLContext reference to load the image
+ /**
+ * \param src Asset with font data to load.
+ * \param mediator use the SDLContext reference to load text
*/
- Font(const Asset & src, Mediator & mediator);
+ Font( src, Mediator & mediator);
~Font() = default
@@ -27,7 +27,7 @@ public:
private:
//! The SDL_ttf font object with custom deleter.
std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font;
- int default_font_size = Config::get_instance().font.font_size;
+ unsigned int default_font_size = Config::get_instance().font.size;
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp
index a4be143..d7a0bff 100644
--- a/src/crepe/facade/SDLFontContext.cpp
+++ b/src/crepe/facade/SDLFontContext.cpp
@@ -7,16 +7,16 @@ using namespace crepe;
using namespace std;
SDLFontContext::SDLFontContext(){
-
+ if (!FcInit()) {
+ throw std::runtime_error("Failed to initialize Fontconfig.");
+ }
}
SDLFontContext::~SDLFontContext(){
-
+ FcFini();
}
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) {
@@ -49,6 +49,5 @@ unique_ptr<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);
- 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 cd91383..c890b2d 100644
--- a/src/crepe/facade/SDLFontContext.h
+++ b/src/crepe/facade/SDLFontContext.h
@@ -11,6 +11,14 @@ namespace crepe {
public:
SDLFontContext();
~SDLFontContext();
+ /**
+ *
+ * \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.
+ */
std::unique_ptr<Asset> get_font_asset(const std::string & font_family);
private:
};
diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp
index 8931ce3..0002026 100644
--- a/src/example/loadfont.cpp
+++ b/src/example/loadfont.cpp
@@ -1,13 +1,24 @@
#include <iostream>
+#include <exception>
#include <memory>
-
#include <crepe/facade/SDLFontContext.h>
+#include <crepe/api/Text.h>
using namespace crepe;
-int main(){
- SDLFontContext font_facade;
- std::unique_ptr<Asset> asset = font_facade.get_font_asset("OpenSymbol");
- std::cout << "path: " << asset->get_path() << std::endl;
- return 0;
+int main() {
+ SDLFontContext font_facade;
+
+ // Create a unique pointer to the font asset
+ std::unique_ptr<Asset> asset = font_facade.get_font_asset("OpenSymbol");
+ std::cout << "path: " << asset->get_path() << std::endl;
+ 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), *asset);
+ }catch (const std::exception& e) {
+ std::cout << "Standard exception thrown: " << e.what() << std::endl;
+ }
+
+
+ return 0;
}