aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-17 15:54:17 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-17 15:54:17 +0100
commit8d53634e2f47a616ff90f114fe1afbc2ba4df094 (patch)
tree5a5e11e9c54c845a5dd570bb76aa73724acc5dd9 /src
parent0524c68cc4eb9fbd36c17ed4f5d8152cac2f977b (diff)
parenta8ccf7fe8662086bb223aa4eafd0f85e717d16cf (diff)
Merge branch 'master' into niels/UI
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Asset.h2
-rw-r--r--src/crepe/api/Config.h11
-rw-r--r--src/crepe/api/Script.h1
-rw-r--r--src/crepe/api/Text.cpp6
-rw-r--r--src/crepe/api/Text.h3
-rw-r--r--src/crepe/facade/Font.cpp2
-rw-r--r--src/crepe/facade/FontFacade.cpp48
-rw-r--r--src/crepe/facade/SDLContext.cpp1
-rw-r--r--src/crepe/facade/SDLContext.h32
-rw-r--r--src/example/loadfont.cpp13
-rw-r--r--src/test/InputTest.cpp7
11 files changed, 67 insertions, 59 deletions
diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h
index b367a92..bfd0ac7 100644
--- a/src/crepe/api/Asset.h
+++ b/src/crepe/api/Asset.h
@@ -37,7 +37,7 @@ public:
private:
//! path to asset
- std::string src;
+ const std::string src;
private:
/**
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index e91ecd1..074c113 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -80,6 +80,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;
//! Configuration for click tolerance.
struct {
//! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click.
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 5d49223..65306cd 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -201,7 +201,6 @@ private:
*
* \{
*/
-
//! Game object ID of game object parent BehaviorScript is attached to
game_object_id_t game_object_id;
//! Reference to parent component
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index 2e248de..9d57abd 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -6,10 +6,8 @@
using namespace crepe;
Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text,
- std::optional<Asset> font)
+ const std::string & font_family, const Data & data, const std::string & text)
: UIObject(id, dimensions, offset),
text(text),
data(data),
- font_family(font_family),
- font(font) {}
+ font_family(font_family) {}
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
index 92cca18..c30dc80 100644
--- a/src/crepe/api/Text.h
+++ b/src/crepe/api/Text.h
@@ -51,8 +51,7 @@ public:
* \param font Optional font asset that can be passed or left empty.
*/
Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text = "",
- std::optional<Asset> font = std::nullopt);
+ const std::string & font_family, const Data & data, const std::string & text = "");
//! Label text.
std::string text = "";
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
index 4694f7c..0c670c1 100644
--- a/src/crepe/facade/Font.cpp
+++ b/src/crepe/facade/Font.cpp
@@ -10,7 +10,7 @@ using namespace std;
using namespace crepe;
Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) {
- Config & config = Config::get_instance();
+ const Config & config = Config::get_instance();
const std::string FONT_PATH = src.get_path();
TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size);
diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp
index cec3507..87f95ab 100644
--- a/src/crepe/facade/FontFacade.cpp
+++ b/src/crepe/facade/FontFacade.cpp
@@ -1,51 +1,43 @@
#include <fontconfig/fontconfig.h>
-#include <iostream>
+#include <functional>
+#include <memory>
#include <stdexcept>
+#include <string>
#include "FontFacade.h"
-using namespace crepe;
using namespace std;
+using namespace crepe;
FontFacade::FontFacade() {
- if (!FcInit()) {
- throw runtime_error("Failed to initialize Fontconfig.");
- }
+ if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig.");
}
+
FontFacade::~FontFacade() { FcFini(); }
+
Asset FontFacade::get_font_asset(const string & font_family) {
+ FcPattern * raw_pattern
+ = FcNameParse(reinterpret_cast<const FcChar8 *>(font_family.c_str()));
+ if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern.");
- // 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.");
- }
+ unique_ptr<FcPattern, function<void(FcPattern *)>> pattern{
+ raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }};
- // Default configuration
FcConfig * config = FcConfigGetCurrent();
- if (config == NULL) {
- // FcPatternDestroy(pattern);
- throw runtime_error("Failed to get current Fontconfig configuration.");
- }
+ if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration.");
- // Match the font pattern
FcResult result;
- FcPattern * matched_pattern = FcFontMatch(config, pattern, &result);
- FcPatternDestroy(pattern);
+ FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result);
+ if (raw_matched_pattern == NULL) throw runtime_error("No matching font found.");
+
+ unique_ptr<FcPattern, function<void(FcPattern *)>> matched_pattern
+ = {raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }};
- if (!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 == NULL) {
- // FcPatternDestroy(matched_pattern);
+ FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path);
+ if (res != FcResultMatch || file_path == NULL)
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/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index ada560b..ea3b71c 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -7,6 +7,7 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_ttf.h>
+#include <SDL2/SDL_video.h>
#include <array>
#include <cmath>
#include <cstddef>
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 01d82a0..1dada74 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -18,7 +18,9 @@
#include "api/KeyCodes.h"
#include "api/Sprite.h"
#include "api/Transform.h"
+#include "types.h"
+#include "EventData.h"
#include "FontFacade.h"
namespace crepe {
@@ -251,6 +253,20 @@ private:
CameraAuxiliaryData cam_aux_data;
private:
+ //! instance of the font_facade
+ FontFacade font_facade{};
+
+public:
+ /**
+ * \brief Function to Get asset from font_family
+ *
+ * This function uses the FontFacade function to convert a font_family to an asset.
+ *
+ * \param font_family name of the font style that needs to be used (will return an asset with default font path of the font_family doesnt exist)
+ *
+ * \return asset with the font style absolute path
+ */
+ Asset get_font_from_name(const std::string & font_family);
//! variable to store the state of each key (true = pressed, false = not pressed)
keyboard_state_t keyboard_state;
//! lookup table for converting SDL_SCANCODES to Keycodes
@@ -353,22 +369,6 @@ private:
{SDL_SCANCODE_RALT, Keycode::RIGHT_ALT},
{SDL_SCANCODE_RGUI, Keycode::RIGHT_SUPER},
{SDL_SCANCODE_MENU, Keycode::MENU}};
-
-private:
- //! instance of the font_facade
- FontFacade font_facade{};
-
-public:
- /**
- * \brief Function to Get asset from font_family
- *
- * This function uses the FontFacade function to convert a font_family to an asset.
- *
- * \param font_family name of the font style that needs to be used (will return an asset with default font path of the font_family doesnt exist)
- *
- * \return asset with the font style absolute path
- */
- Asset get_font_from_name(const std::string & font_family);
};
} // namespace crepe
diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp
index ed67ffa..e459332 100644
--- a/src/example/loadfont.cpp
+++ b/src/example/loadfont.cpp
@@ -2,6 +2,7 @@
#include <crepe/api/Asset.h>
#include <crepe/api/Text.h>
#include <crepe/facade/Font.h>
+#include <crepe/facade/FontFacade.h>
#include <crepe/facade/SDLContext.h>
#include <crepe/manager/Mediator.h>
#include <crepe/manager/ResourceManager.h>
@@ -14,24 +15,26 @@ int main() {
// SDLFontContext font_facade;
Mediator mediator;
+ FontFacade font_facade{};
SDLContext sdl_context{mediator};
// ComponentManager component_manager{mediator};
ResourceManager resource_manager{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), "OpenSymbol", Text::Data{}, "test text", Asset(""));
+ 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text");
// std::cout << "Path: " << label->font.get_path() << std::endl;
-
+ Asset asset1 = font_facade.get_font_asset("OpenSymbol");
+ std::cout << asset1.get_path() << std::endl;
std::unique_ptr<Text> label2 = std::make_unique<Text>(
1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{});
Asset asset = Asset("test test");
- label->font = asset;
+ label->font.emplace(asset);
std::cout << label->font.value().get_path() << std::endl;
// label2->font = std::make_optional(asset);
// std::cout << "Path: " << label2->font.get_path() << std::endl;
- // ResourceManager & resource_mgr = mediator.resource_manager;
- // const Font & res = resource_manager.get<Font>(label->font);
+ ResourceManager & resource_mgr = mediator.resource_manager;
+ const Font & res = resource_manager.get<Font>(label->font.value());
// TTF_Font * test_font = res.get_font();
// if (test_font == NULL) {
// std::cout << "error with font" << std::endl;
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
index d893276..2d844d4 100644
--- a/src/test/InputTest.cpp
+++ b/src/test/InputTest.cpp
@@ -1,7 +1,11 @@
-#include "system/RenderSystem.h"
#include <gtest/gtest.h>
+
+#include <crepe/manager/ResourceManager.h>
+#include <crepe/system/RenderSystem.h>
+
#define protected public
#define private public
+
#include "api/KeyCodes.h"
#include "manager/ComponentManager.h"
#include "manager/EventManager.h"
@@ -29,6 +33,7 @@ public:
SDLContext sdl_context{mediator};
InputSystem input_system{mediator};
+ ResourceManager resman{mediator};
RenderSystem render{mediator};
EventManager event_manager{mediator};
//GameObject camera;