From 8abc6008880dd9ed0c16a68a126b49f0eb03caa2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 16 Dec 2024 20:52:43 +0100 Subject: fonting --- src/crepe/facade/Font.cpp | 10 ++++++++-- src/crepe/facade/Font.h | 1 + src/crepe/facade/SDLContext.cpp | 24 ++++++++++++++++++++++++ src/crepe/facade/SDLContext.h | 4 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) (limited to 'src/crepe/facade') 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(); -- cgit v1.2.3