aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-17 13:36:35 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-17 13:36:35 +0100
commit31fe4b1d44a7a7114c7ef8c0c5310a6070214cb2 (patch)
tree14564711f3ce996ccb1000e3f4f07d265e6fab88
parent0c02511f6a64766174cb0db3beb4cc159d3efdf7 (diff)
parent69ca7fdd738fd4ed98aefc07bab5a43486a55619 (diff)
Merge branch 'wouter/text-component' into niels/UI
-rw-r--r--src/crepe/api/LoopManager.h3
-rw-r--r--src/crepe/api/Script.h1
-rw-r--r--src/crepe/api/Text.cpp8
-rw-r--r--src/crepe/api/Text.h11
-rw-r--r--src/crepe/facade/Font.cpp3
-rw-r--r--src/crepe/facade/FontFacade.cpp10
-rw-r--r--src/crepe/facade/FontFacade.h10
-rw-r--r--src/crepe/facade/SDLContext.cpp2
-rw-r--r--src/crepe/facade/SDLContext.h7
-rw-r--r--src/crepe/system/RenderSystem.cpp34
-rw-r--r--src/example/FontExample.cpp5
-rw-r--r--src/example/loadfont.cpp10
12 files changed, 47 insertions, 57 deletions
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index 1725810..1d23cbf 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -71,7 +71,7 @@ private:
private:
//! Global context
Mediator mediator;
-
+
//! SDLContext instance
SDLContext sdl_context{mediator};
//! Component manager instance
@@ -86,7 +86,6 @@ private:
ResourceManager resource_manager{mediator};
//! Save manager instance
SaveManager save_manager{mediator};
-
private:
/**
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 4503525..8bca38a 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -156,7 +156,6 @@ private:
void subscribe_internal(const EventHandler<EventType> & callback, event_channel_t channel);
protected:
-
// NOTE: This must be the only constructor on Script, see "Late references" below
Script() = default;
//! Only \c BehaviorScript instantiates Script
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index 1620247..2e248de 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -5,11 +5,11 @@
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)
+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)
: UIObject(id, dimensions, offset),
text(text),
data(data),
font_family(font_family),
- font(font) {
-}
+ font(font) {}
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
index ab72bc0..92cca18 100644
--- a/src/crepe/api/Text.h
+++ b/src/crepe/api/Text.h
@@ -1,7 +1,7 @@
#pragma once
+#include <optional>
#include <string>
-#include <optional>
#include "../Component.h"
@@ -50,10 +50,10 @@ public:
* \param data Data struct containing extra text parameters.
* \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);
-
+ 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);
+
//! Label text.
std::string text = "";
//! font family name
@@ -62,7 +62,6 @@ public:
std::optional<Asset> font;
//! Data instance
Data data;
-
};
} // namespace crepe
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
index 9c1a800..81a9e7a 100644
--- a/src/crepe/facade/Font.cpp
+++ b/src/crepe/facade/Font.cpp
@@ -9,8 +9,7 @@
using namespace std;
using namespace crepe;
-Font::Font(const Asset & src, Mediator & mediator)
- : Resource(src, mediator){
+Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) {
Config & config = Config::get_instance();
const std::string FONT_PATH = src.get_path();
diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp
index 9919032..cec3507 100644
--- a/src/crepe/facade/FontFacade.cpp
+++ b/src/crepe/facade/FontFacade.cpp
@@ -7,16 +7,14 @@
using namespace crepe;
using namespace std;
-FontFacade::FontFacade(){
+FontFacade::FontFacade() {
if (!FcInit()) {
throw runtime_error("Failed to initialize Fontconfig.");
}
}
-FontFacade::~FontFacade(){
- FcFini();
-}
-Asset FontFacade::get_font_asset(const string& font_family) {
-
+FontFacade::~FontFacade() { FcFini(); }
+Asset FontFacade::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) {
diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h
index 2e08f3f..9761070 100644
--- a/src/crepe/facade/FontFacade.h
+++ b/src/crepe/facade/FontFacade.h
@@ -15,10 +15,10 @@ class FontFacade {
public:
FontFacade();
~FontFacade();
- FontFacade(const FontFacade & other) = delete;
- FontFacade & operator=(const FontFacade & other) = delete;
- FontFacade(FontFacade && other) noexcept = delete;
- FontFacade & operator=(FontFacade && other) noexcept = delete;
+ FontFacade(const FontFacade & other) = delete;
+ FontFacade & operator=(const FontFacade & other) = delete;
+ FontFacade(FontFacade && other) noexcept = delete;
+ FontFacade & operator=(FontFacade && other) noexcept = delete;
/**
*
* \brief Facade function to convert a font_family into an asset.
@@ -28,7 +28,7 @@ public:
* \param font_family Name of the font family name.
* \return Asset with filepath to the corresponding font.
*/
- Asset get_font_asset(const std::string& font_family);
+ Asset get_font_asset(const std::string & font_family);
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index c19f0aa..a5ccdae 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -467,6 +467,6 @@ void SDLContext::set_color_texture(const Texture & texture, const Color & color)
SDL_SetTextureAlphaMod(texture.get_img(), color.a);
}
-Asset SDLContext::get_font_from_name(const std::string& font_family){
+Asset SDLContext::get_font_from_name(const std::string & font_family) {
return this->font_facade.get_font_asset(font_family);
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index fcf3559..33a4ff9 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -18,6 +18,7 @@
#include "types.h"
#include "FontFacade.h"
+
namespace crepe {
class Texture;
@@ -244,9 +245,11 @@ private:
* - this is defined in this class because get_events() needs this information aswell
*/
CameraAuxiliaryData cam_aux_data;
-private:
+
+private:
//! instance of the font_facade
FontFacade font_facade{};
+
public:
/**
* \brief Function to Get asset from font_family
@@ -257,7 +260,7 @@ public:
*
* \return asset with the font style absolute path
*/
- Asset get_font_from_name(const std::string& font_family);
+ Asset get_font_from_name(const std::string & font_family);
};
} // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 578a4c0..872ba29 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -10,8 +10,8 @@
#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Text.h"
-#include "../facade/Font.h"
#include "../api/Transform.h"
+#include "../facade/Font.h"
#include "../facade/SDLContext.h"
#include "../facade/Texture.h"
#include "../manager/ComponentManager.h"
@@ -25,7 +25,6 @@
using namespace crepe;
using namespace std;
-
void RenderSystem::clear_screen() {
SDLContext & ctx = this->mediator.sdl_context;
ctx.clear_screen();
@@ -145,12 +144,11 @@ void RenderSystem::render() {
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
ResourceManager & resource_manager = this->mediator.resource_manager;
RefVector<Sprite> sorted_sprites = this->sort(sprites);
- RefVector<Text> text_components = mgr.get_components_by_type<Text>();
- for(Text& text : text_components){
+ RefVector<Text> text_components = mgr.get_components_by_type<Text>();
+ for (Text & text : text_components) {
const Transform & transform
= mgr.get_components_by_id<Transform>(text.game_object_id).front().get();
- this->render_text(text,transform);
-
+ this->render_text(text, transform);
}
for (const Sprite & sprite : sorted_sprites) {
if (!sprite.active) continue;
@@ -162,26 +160,20 @@ void RenderSystem::render() {
if (rendered_particles) continue;
this->render_normal(sprite, transform);
-
-
-
}
-
}
void RenderSystem::render_text(Text & text, const Transform & tm) {
- SDLContext & ctx = this->mediator.sdl_context;
+ SDLContext & ctx = this->mediator.sdl_context;
- // Check if font is available in text
- if (!text.font.has_value()) {
- }
+ if (!text.font.has_value()) {
+ text.font = ctx.get_font_from_name(text.font_family);
+ }
- ResourceManager & resource_manager = this->mediator.resource_manager;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
- if (text.font.has_value()) {
- const Asset& font_asset = text.font.value();
- const Font & res = resource_manager.get<Font>(font_asset);
+ if (!text.font.has_value()) {
+ return;
}
+ const Asset & font_asset = text.font.value();
+ const Font & res = resource_manager.get<Font>(font_asset);
}
-
-
-
diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp
index 7b2dadb..6a334b1 100644
--- a/src/example/FontExample.cpp
+++ b/src/example/FontExample.cpp
@@ -1,6 +1,7 @@
#include <SDL2/SDL_ttf.h>
#include <chrono>
#include <crepe/api/Camera.h>
+#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/LoopManager.h>
#include <crepe/api/Scene.h>
@@ -11,7 +12,6 @@
#include <crepe/manager/EventManager.h>
#include <crepe/manager/Mediator.h>
#include <crepe/manager/ResourceManager.h>
-#include <crepe/api/Config.h>
#include <exception>
#include <iostream>
#include <memory>
@@ -37,7 +37,8 @@ class TestScene : public Scene {
public:
void load_scene() override {
GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1);
- text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{});
+ text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "OpenSymbol",
+ Text::Data{});
text_object.add_component<BehaviorScript>().set_script<TestScript>();
text_object.add_component<Camera>(ivec2{300, 300}, vec2{100, 100}, Camera::Data{});
}
diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp
index 36d00dd..ed67ffa 100644
--- a/src/example/loadfont.cpp
+++ b/src/example/loadfont.cpp
@@ -1,14 +1,14 @@
#include <SDL2/SDL_ttf.h>
+#include <crepe/api/Asset.h>
#include <crepe/api/Text.h>
#include <crepe/facade/Font.h>
#include <crepe/facade/SDLContext.h>
-#include <crepe/api/Asset.h>
#include <crepe/manager/Mediator.h>
#include <crepe/manager/ResourceManager.h>
#include <exception>
#include <iostream>
-#include <optional>
#include <memory>
+#include <optional>
using namespace crepe;
int main() {
@@ -20,11 +20,11 @@ int main() {
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", Asset(""));
// 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),"fsaafdafsdafsdafsdasfdds", Text::Data{});
+ 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;
std::cout << label->font.value().get_path() << std::endl;