diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:58:52 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:58:52 +0100 |
commit | 8d3fd95ad8564740bb169f4e08bc085a232a4d83 (patch) | |
tree | 7d0334ee18d0f9278817143b73be0385516b4093 /src | |
parent | 31fe4b1d44a7a7114c7ef8c0c5310a6070214cb2 (diff) |
workig rending text
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 36 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 8 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 16 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 6 |
4 files changed, 45 insertions, 21 deletions
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<Text> texts = mgr.get_components_by_type<Text>(); - for (const Text & text : texts) { + for (const Text & text : texts) { if (!text.active) continue; - const Font & res = resource_manager.get<Font>(text.font.value()); - ctx.draw_text(text, res); + const Font & font = resource_manager.get<Font>(text.font.value()); + const auto & transform + = mgr.get_components_by_id<Transform>(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<Camera>(ivec2{400, 400}, vec2{400, 400}, + auto & cam = game_object.add_component<Camera>(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ .bg_color = Color::WHITE, }); - game_object.add_component<Text>(vec2{200, 200}, vec2{0, 0}, "test test", "Ariel.ttf", - Text::Data{}); + game_object.add_component<Text>(vec2{400, 400}, vec2{0, 0}, "ComicSansMS", + Text::Data{.text_color = Color::RED}, "TEST test"); } string get_name() const { return "TestScene"; }; |