aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/Config.h11
-rw-r--r--src/crepe/api/Text.cpp10
-rw-r--r--src/crepe/api/Text.h52
-rw-r--r--src/crepe/facade/CMakeLists.txt4
-rw-r--r--src/crepe/facade/Font.cpp19
-rw-r--r--src/crepe/facade/Font.h41
-rw-r--r--src/crepe/facade/SDLContext.cpp4
-rw-r--r--src/crepe/facade/SDLContext.h2
-rw-r--r--src/crepe/facade/SDLFontContext.cpp53
-rw-r--r--src/crepe/facade/SDLFontContext.h31
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/example/loadfont.cpp36
14 files changed, 269 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 97b21f0..696856c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -9,10 +9,12 @@ project(crepe C CXX)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
+find_package(SDL2_ttf REQUIRED)
find_package(SoLoud REQUIRED)
find_package(GTest REQUIRED)
find_package(whereami REQUIRED)
find_library(BERKELEY_DB db)
+find_library(FONTCONFIG_LIB fontconfig)
add_library(crepe SHARED)
add_executable(test_main EXCLUDE_FROM_ALL)
@@ -24,9 +26,11 @@ target_include_directories(crepe
target_link_libraries(crepe
PRIVATE soloud
PUBLIC SDL2
+ PUBLIC SDL2_ttf
PUBLIC SDL2_image
PUBLIC ${BERKELEY_DB}
PUBLIC whereami
+ PUBLIC ${FONTCONFIG_LIB}
)
add_subdirectory(crepe)
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 8f84f06..e8d6f92 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
Scene.cpp
)
@@ -51,4 +52,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 ca2d3f1..47a81b7 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -76,6 +76,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;
//! Audio system settings
struct {
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
new file mode 100644
index 0000000..9de9038
--- /dev/null
+++ b/src/crepe/api/Text.cpp
@@ -0,0 +1,10 @@
+#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) {}
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
new file mode 100644
index 0000000..96e1265
--- /dev/null
+++ b/src/crepe/api/Text.h
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <string>
+
+#include "../Component.h"
+
+#include "Color.h"
+#include "UIObject.h"
+
+namespace crepe {
+/**
+ * \brief Text UIObject component for displaying text
+ *
+ * This class can be used to display text on screen. By setting the font_family to a font already stored on the current device it will automatically be loaded in.
+ */
+class Text : public UIObject {
+public:
+ //! Text data that does not have to be set in the constructor
+ struct Data {
+ /**
+ * \brief fontsize for text rendering
+ *
+ * \note this is not the actual font size that is loaded in.
+ *
+ * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size.
+ * 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.
+ */
+ unsigned int font_size = 16;
+
+ //! Layer sorting level of the text
+ const int sorting_in_layer = 0;
+
+ //! Order within the sorting text
+ const int order_in_layer = 0;
+
+ //! Label text color.
+ Color text_color = Color::BLACK;
+ };
+
+public:
+ 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 = "";
+ //! Label text.
+ std::string text = "";
+ // Data instance for data not gotten from constructor
+ Data data;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index 0598e16..e61b680 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -4,6 +4,8 @@ target_sources(crepe PUBLIC
SoundContext.cpp
SDLContext.cpp
DB.cpp
+ SDLFontContext.cpp
+ Font.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -12,5 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SoundContext.h
SDLContext.h
DB.h
+ SDLFontContext.h
+ Font.h
)
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
new file mode 100644
index 0000000..f111af9
--- /dev/null
+++ b/src/crepe/facade/Font.cpp
@@ -0,0 +1,19 @@
+#include "../api/Config.h"
+
+#include "Font.h"
+
+using namespace std;
+using namespace crepe;
+
+Font::Font(const Asset & src, Mediator & mediator)
+ : Resource(src, mediator),
+ font(nullptr, TTF_CloseFont) {
+ Config & config = Config::get_instance();
+ const std::string FONT_PATH = src.get_path();
+ TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size);
+ if (font == NULL)
+ throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH));
+ this->font = { font, [] (TTF_Font * font) { TTF_CloseFont(font); } };
+}
+
+TTF_Font * Font::get_font() const { return this->font.get(); }
diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h
new file mode 100644
index 0000000..f7d5b50
--- /dev/null
+++ b/src/crepe/facade/Font.h
@@ -0,0 +1,41 @@
+#pragma once
+#include <SDL2/SDL_ttf.h>
+#include <memory>
+
+#include "../Resource.h"
+#include "../api/Asset.h"
+#include "../api/Config.h"
+
+namespace crepe {
+
+/**
+ * \brief Resource for managing font creation and destruction
+ *
+ * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage.
+ * It loads a font from an Asset and manages its lifecycle. The font is automatically unloaded
+ * when this object is destroyed.
+ */
+class Font : public Resource {
+public:
+ /**
+ * \param src The Asset containing the font file path and metadata to load the font.
+ * \param mediator The Mediator object used for managing the SDL context or related systems.
+ */
+ Font(const Asset & src, Mediator & mediator);
+
+ /**
+ * \brief Gets the underlying TTF_Font resource.
+ *
+ * This function returns the raw pointer to the SDL_ttf TTF_Font object that represents
+ * the loaded font. This can be used with SDL_ttf functions to render text.
+ *
+ * \return The raw TTF_Font object wrapped in a unique pointer.
+ */
+ TTF_Font * get_font() const;
+
+private:
+ //! The SDL_ttf font object with custom deleter.
+ std::unique_ptr<TTF_Font, std::function<void(TTF_Font*)>> font;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 20bb030..cbb0f3b 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -31,6 +31,9 @@ using namespace std;
SDLContext::SDLContext(Mediator & mediator) {
dbg_trace();
+ if (TTF_Init() == -1) {
+ throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError()));
+ }
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError()));
}
@@ -73,6 +76,7 @@ SDLContext::~SDLContext() {
// before.
IMG_Quit();
SDL_Quit();
+ TTF_Quit();
}
Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index bcadf87..76cd99a 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -4,6 +4,7 @@
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <cmath>
#include <functional>
@@ -15,7 +16,6 @@
#include "api/KeyCodes.h"
#include "api/Sprite.h"
#include "api/Transform.h"
-
#include "types.h"
namespace crepe {
diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp
new file mode 100644
index 0000000..5123b3b
--- /dev/null
+++ b/src/crepe/facade/SDLFontContext.cpp
@@ -0,0 +1,53 @@
+#include <stdexcept>
+
+#include "SDLFontContext.h"
+
+using namespace crepe;
+using namespace std;
+
+SDLFontContext::SDLFontContext() {
+ 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) {
+ throw runtime_error("Failed to create font pattern.");
+ }
+
+ // Default configuration
+ FcConfig * config = FcConfigGetCurrent();
+ if (!config) {
+ FcPatternDestroy(pattern);
+ throw runtime_error("Failed to get current Fontconfig configuration.");
+ }
+
+ // Match the font pattern
+ FcResult result;
+ FcPattern * matched_pattern = FcFontMatch(config, pattern, &result);
+ FcPatternDestroy(pattern);
+
+ if (!matched_pattern) {
+ FcPatternDestroy(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) {
+ FcPatternDestroy(matched_pattern);
+ 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/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h
new file mode 100644
index 0000000..cb3ca9d
--- /dev/null
+++ b/src/crepe/facade/SDLFontContext.h
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <SDL2/SDL_ttf.h>
+#include <fontconfig/fontconfig.h>
+#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);
+
+private:
+};
+
+} // namespace crepe
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 187ed46..0e2d5e2 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -19,4 +19,5 @@ endfunction()
add_example(rendering_particle)
add_example(game)
add_example(button)
+add_example(loadfont)
add_example(AITest)
diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp
new file mode 100644
index 0000000..a52e7f0
--- /dev/null
+++ b/src/example/loadfont.cpp
@@ -0,0 +1,36 @@
+#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>
+#include <memory>
+using namespace crepe;
+int main() {
+
+ 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();
+
+ // 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;
+ }
+ } catch (const std::exception & e) {
+ std::cout << "Standard exception thrown: " << e.what() << std::endl;
+ }
+
+ return 0;
+}