From 8d3fd95ad8564740bb169f4e08bc085a232a4d83 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 13:58:52 +0100 Subject: workig rending text --- src/crepe/facade/SDLContext.cpp | 36 +++++++++++++++++++++++++----------- src/crepe/facade/SDLContext.h | 8 +++++++- 2 files changed, 32 insertions(+), 12 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a5ccdae..27f2cb3 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -22,6 +22,7 @@ #include "../api/Sprite.h" #include "../util/Log.h" #include "api/Text.h" +#include "api/Transform.h" #include "facade/Font.h" #include "manager/Mediator.h" @@ -286,27 +287,40 @@ void SDLContext::draw(const RenderContext & ctx) { angle, NULL, render_flip); } +void SDLContext::draw_text(const RenderText & data) { -void SDLContext::draw_text(const Text & text, const Font & font){ - SDL_Color color { + const Text & text = data.text; + const Font & font = data.font; + const Transform & transform = data.transform; + + 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_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_Rect dstrect { - .x = (int)text.offset.x, - .y = (int)text.offset.y, - .w = (int)text.dimensions.x, - .h = (int)text.dimensions.y, + vec2 size = text.dimensions * cam_aux_data.render_scale; + vec2 screen_pos = (transform.position + text.offset - cam_aux_data.cam_pos + + (cam_aux_data.zoomed_viewport) / 2) + * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; + + + SDL_FRect dstrect{ + .x = screen_pos.x, + .y = screen_pos.y, + .w = size.x, + .h = size.y, }; - SDL_RenderCopy(this->game_renderer.get(), font_texture, NULL, NULL); - SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, NULL, 0 , NULL, SDL_FLIP_NONE); + SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, &dstrect, 0, NULL, + SDL_FLIP_NONE); SDL_DestroyTexture(font_texture); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 33a4ff9..066a881 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -71,6 +71,12 @@ public: const double & scale; }; + struct RenderText { + const Text & text; + const Font & font; + const Transform & transform; + }; + public: //! EventType enum for passing eventType enum EventType { @@ -187,7 +193,7 @@ public: */ void draw(const RenderContext & ctx); - void draw_text(const Text & text, const Font & font); + void draw_text(const RenderText & data); //! Clears the screen, preparing for a new frame. void clear_screen(); -- cgit v1.2.3