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 +++++++- src/crepe/system/RenderSystem.cpp | 16 ++++++++++------ src/example/rendering_particle.cpp | 6 +++--- 4 files changed, 45 insertions(+), 21 deletions(-) (limited to 'src') 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(); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 872ba29..2641435 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -76,20 +76,24 @@ void RenderSystem::update() { this->present_screen(); } - -void RenderSystem::render_text(){ +void RenderSystem::render_text() { SDLContext & ctx = this->mediator.sdl_context; ComponentManager & mgr = this->mediator.component_manager; ResourceManager & resource_manager = this->mediator.resource_manager; RefVector texts = mgr.get_components_by_type(); - for (const Text & text : texts) { + for (const Text & text : texts) { if (!text.active) continue; - const Font & res = resource_manager.get(text.font.value()); - ctx.draw_text(text, res); + const Font & font = resource_manager.get(text.font.value()); + const auto & transform + = mgr.get_components_by_id(text.game_object_id).front().get(); + ctx.draw_text(SDLContext::RenderText{ + .text = text, + .font = font, + .transform = transform, + }); } - } bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index b261f52..aa71dd0 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -62,12 +62,12 @@ public: .position_offset = {0, 0}, }); - auto & cam = game_object.add_component(ivec2{400, 400}, vec2{400, 400}, + auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ .bg_color = Color::WHITE, }); - game_object.add_component(vec2{200, 200}, vec2{0, 0}, "test test", "Ariel.ttf", - Text::Data{}); + game_object.add_component(vec2{400, 400}, vec2{0, 0}, "ComicSansMS", + Text::Data{.text_color = Color::RED}, "TEST test"); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3