aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-16 20:52:43 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-16 20:52:43 +0100
commit8abc6008880dd9ed0c16a68a126b49f0eb03caa2 (patch)
tree98bf7773f81d9896df6c17a7d662dd6f4380023f /src/crepe
parent56342ffab9daade7802b015ac83b9c47a8b9b18b (diff)
fonting
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Config.h2
-rw-r--r--src/crepe/api/Text.cpp5
-rw-r--r--src/crepe/facade/Font.cpp10
-rw-r--r--src/crepe/facade/Font.h1
-rw-r--r--src/crepe/facade/SDLContext.cpp24
-rw-r--r--src/crepe/facade/SDLContext.h4
-rw-r--r--src/crepe/system/RenderSystem.cpp19
-rw-r--r--src/crepe/system/RenderSystem.h2
8 files changed, 63 insertions, 4 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 47a81b7..c20287d 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -26,7 +26,7 @@ struct Config final {
*
* Only messages with equal or higher priority than this value will be logged.
*/
- Log::Level level = Log::Level::INFO;
+ Log::Level level = Log::Level::DEBUG;
/**
* \brief Colored log output
*
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index 5b2befe..07d1705 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -1,4 +1,5 @@
#include "../facade/FontFacade.h"
+#include "util/Log.h"
#include "Text.h"
@@ -9,4 +10,6 @@ Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
: UIObject(id, dimensions, offset),
text(text),
data(data),
- font(FontFacade::get_font_asset(font_family)) {}
+ font(FontFacade::get_font_asset(font_family)) {
+ dbg_trace();
+}
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
index 1ee3de1..83e8519 100644
--- a/src/crepe/facade/Font.cpp
+++ b/src/crepe/facade/Font.cpp
@@ -1,4 +1,5 @@
#include "../api/Config.h"
+#include "util/Log.h"
#include "Font.h"
@@ -6,8 +7,8 @@ using namespace std;
using namespace crepe;
Font::Font(const Asset & src, Mediator & mediator)
- : Resource(src, mediator),
- font(nullptr, TTF_CloseFont) {
+ : Resource(src, mediator) {
+ dbg_trace();
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);
@@ -16,4 +17,9 @@ Font::Font(const Asset & src, Mediator & mediator)
this->font = {font, [](TTF_Font * font) { TTF_CloseFont(font); }};
}
+Font::~Font(){
+ dbg_trace();
+ this->font.reset();
+}
+
TTF_Font * Font::get_font() const { return this->font.get(); }
diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h
index e93bfe9..e9f5fa1 100644
--- a/src/crepe/facade/Font.h
+++ b/src/crepe/facade/Font.h
@@ -22,6 +22,7 @@ public:
* \param mediator The Mediator object used for managing the SDL context or related systems.
*/
Font(const Asset & src, Mediator & mediator);
+ ~Font();
/**
* \brief Gets the underlying TTF_Font resource.
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index d1d109c..4e969b9 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -21,6 +21,8 @@
#include "../api/Config.h"
#include "../api/Sprite.h"
#include "../util/Log.h"
+#include "api/Text.h"
+#include "facade/Font.h"
#include "manager/Mediator.h"
#include "SDLContext.h"
@@ -283,6 +285,28 @@ void SDLContext::draw(const RenderContext & ctx) {
angle, NULL, render_flip);
}
+
+void SDLContext::draw_text(const Text & text, const Font & font){
+ SDL_Color color {
+ .r = text.data.text_color.r,
+ .g = text.data.text_color.g,
+ .b = text.data.text_color.b,
+ .a = text.data.text_color.a,
+ };
+ SDL_Surface * font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color);
+ SDL_Texture * font_texture = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface);
+ SDL_FreeSurface(font_surface);
+
+ SDL_FRect dstrect {
+ .x = text.offset.x,
+ .y = text.offset.y,
+ .w = text.dimensions.x,
+ .h = text.dimensions.y,
+ };
+ SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, &dstrect , 0 , NULL, SDL_FLIP_NONE);
+ SDL_DestroyTexture(font_texture);
+}
+
void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) {
const Camera::Data & cam_data = cam.data;
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index efdd6fe..3f9f9ee 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -20,6 +20,8 @@
namespace crepe {
class Texture;
+class Text;
+class Font;
class Mediator;
/**
@@ -183,6 +185,8 @@ public:
*/
void draw(const RenderContext & ctx);
+ void draw_text(const Text & text, const Font & font);
+
//! Clears the screen, preparing for a new frame.
void clear_screen();
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index afd9548..7f00891 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -15,6 +15,8 @@
#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
+#include "api/Text.h"
+#include "facade/Font.h"
#include "types.h"
using namespace crepe;
@@ -67,9 +69,26 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const {
void RenderSystem::update() {
this->clear_screen();
this->render();
+ this->render_text();
this->present_screen();
}
+
+void RenderSystem::render_text(){
+ SDLContext & ctx = this->mediator.sdl_context;
+ ComponentManager & mgr = this->mediator.component_manager;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+
+ RefVector<Text> texts = mgr.get_components_by_type<Text>();
+
+ for (Text & text : texts) {
+ if (!text.active) continue;
+ resource_manager.get<Font>(text.font);
+ //ctx.draw_text(text, font);
+ }
+
+}
+
bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
ComponentManager & mgr = this->mediator.component_manager;
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index fc7b46e..ae55404 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -27,6 +27,8 @@ public:
void update() override;
private:
+
+ void render_text();
//! Clears the screen in preparation for rendering.
void clear_screen();