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/SDLContext.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/crepe/facade/SDLContext.h') 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 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/crepe/facade/SDLContext.h') 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 From 68be6b67f79413cb4af0ca15262ca1547a7d0d41 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 14:16:47 +0100 Subject: finalized text rendering --- src/crepe/facade/SDLContext.cpp | 22 ++++++++++++++++------ src/crepe/facade/SDLContext.h | 5 +++++ src/crepe/system/RenderSystem.cpp | 26 +++++--------------------- src/crepe/system/RenderSystem.h | 8 ++------ 4 files changed, 28 insertions(+), 33 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 41c6d21..c3c1b59 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -292,6 +292,8 @@ void SDLContext::draw_text(const RenderText & data) { const Text & text = data.text; const Font & font = data.font; const Transform & transform = data.transform; + std::unique_ptr> font_surface; + std::unique_ptr> font_texture; SDL_Color color{ .r = text.data.text_color.r, @@ -299,11 +301,20 @@ void SDLContext::draw_text(const RenderText & data) { .b = text.data.text_color.b, .a = text.data.text_color.a, }; - SDL_Surface * font_surface + SDL_Surface * tmp_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); + if (!tmp_font_surface) { + throw runtime_error(format("draw_text: font surface error: {}", SDL_GetError())); + } + font_surface = {tmp_font_surface, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }}; + + SDL_Texture * tmp_font_texture + = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface.get()); + if (!tmp_font_texture) { + throw runtime_error(format("draw_text: font texture error: {}", SDL_GetError())); + } + font_texture + = {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; vec2 size = text.dimensions * cam_aux_data.render_scale; vec2 screen_pos = (transform.position + text.offset - cam_aux_data.cam_pos @@ -318,9 +329,8 @@ void SDLContext::draw_text(const RenderText & data) { .h = size.y, }; - SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, &dstrect, 0, NULL, + SDL_RenderCopyExF(this->game_renderer.get(), font_texture.get(), NULL, &dstrect, 0, NULL, SDL_FLIP_NONE); - SDL_DestroyTexture(font_texture); } void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 066a881..aeace07 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -193,6 +193,11 @@ public: */ void draw(const RenderContext & ctx); + /** + * \brief draws a text to the screen + * + * \param data Reference to the rendering data needed to draw + */ void draw_text(const RenderText & data); //! Clears the screen, preparing for a new frame. diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 2641435..42e4236 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -83,8 +83,11 @@ void RenderSystem::render_text() { RefVector texts = mgr.get_components_by_type(); - for (const Text & text : texts) { + for (Text & text : texts) { if (!text.active) continue; + if (!text.font.has_value()) text.font = ctx.get_font_from_name(text.font_family); + if (!text.font.has_value()) continue; + const Font & font = resource_manager.get(text.font.value()); const auto & transform = mgr.get_components_by_id(text.game_object_id).front().get(); @@ -149,11 +152,7 @@ void RenderSystem::render() { ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); RefVector text_components = mgr.get_components_by_type(); - for (Text & text : text_components) { - const Transform & transform - = mgr.get_components_by_id(text.game_object_id).front().get(); - this->render_text(text, transform); - } + for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; const Transform & transform @@ -166,18 +165,3 @@ void RenderSystem::render() { this->render_normal(sprite, transform); } } -void RenderSystem::render_text(Text & text, const Transform & tm) { - SDLContext & ctx = this->mediator.sdl_context; - - if (!text.font.has_value()) { - text.font = ctx.get_font_from_name(text.font_family); - } - - ResourceManager & resource_manager = this->mediator.resource_manager; - - if (!text.font.has_value()) { - return; - } - const Asset & font_asset = text.font.value(); - const Font & res = resource_manager.get(font_asset); -} diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 3aa846e..5fe24d7 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -27,7 +27,6 @@ public: void update() override; private: - void render_text(); //! Clears the screen in preparation for rendering. void clear_screen(); @@ -39,7 +38,7 @@ private: //! Renders the whole screen void render(); - + /** * \brief Renders all the particles on the screen from a given sprite. * @@ -53,11 +52,8 @@ private: bool render_particle(const Sprite & sprite, const double & scale); /** * \brief Renders all Text components - * - * \param text The text component to be rendered. - * \param tm the Transform component that holds the position,rotation and scale */ - void render_text(Text & text, const Transform & tm); + void render_text(); /** * \brief renders a sprite with a Transform component on the screen * -- cgit v1.2.3 From 6ba8f83d482b5b7ade4cf63293cb9625a7e91f55 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 14:44:40 +0100 Subject: config will be checout with master once PR#77 is merged, and sdlcontext adjustemetns for merging --- src/crepe/api/Config.h | 4 ++++ src/crepe/facade/SDLContext.h | 7 ------- 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index ed1cf38..e91ecd1 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -46,6 +46,10 @@ struct Config final { std::string location = "save.crepe.db"; } savemgr; + struct { + unsigned int size = 16; + } font; + //! physics-related settings struct { /** diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 7c791f2..e9c334e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -78,13 +78,6 @@ public: const Transform & transform; }; -public: - /** - * \brief Gets the singleton instance of SDLContext. - * \return Reference to the SDLContext instance. - */ - static SDLContext & get_instance(); - public: SDLContext(const SDLContext &) = delete; SDLContext(SDLContext &&) = delete; -- cgit v1.2.3 From 27bf7f7a6e2493b742f502b5da394a5a2d74cdd2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 14:45:06 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 1 - src/crepe/facade/SDLContext.h | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 271b746..f368828 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -459,7 +459,6 @@ void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, } } - void SDLContext::set_color_texture(const Texture & texture, const Color & color) { SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); SDL_SetTextureAlphaMod(texture.get_img(), color.a); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e9c334e..fc44f82 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,12 +12,12 @@ #include #include "../types.h" +#include "EventData.h" #include "api/Camera.h" #include "api/Color.h" #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "EventData.h" #include "FontFacade.h" @@ -72,7 +72,7 @@ public: const double & scale; }; - struct RenderText { + struct RenderText { const Text & text; const Font & font; const Transform & transform; -- cgit v1.2.3 From 9633c6a8608175e9832f6995efc46023deebcf94 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 19 Dec 2024 15:05:56 +0100 Subject: removed absoluut position because file name, renamed functions in sdlcontext --- src/crepe/facade/SDLContext.cpp | 4 ++-- src/crepe/facade/SDLContext.h | 2 +- src/crepe/util/AbsoluutPosition.cpp | 20 -------------------- src/crepe/util/AbsoluutPosition.h | 14 -------------- 4 files changed, 3 insertions(+), 37 deletions(-) delete mode 100644 src/crepe/util/AbsoluutPosition.cpp delete mode 100644 src/crepe/util/AbsoluutPosition.h (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 642aef8..31b0a0b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -24,11 +24,11 @@ #include "api/Transform.h" #include "facade/Font.h" #include "manager/Mediator.h" +#include "util/AbsolutePosition.h" #include "SDLContext.h" #include "Texture.h" #include "types.h" -#include "util/AbsoluutPosition.h" using namespace crepe; using namespace std; @@ -204,7 +204,7 @@ void SDLContext::draw_text(const RenderText & data) { const Text & text = data.text; const Font & font = data.font; - vec2 absoluut_pos = AbsoluutPosition::get_position(data.transform, data.text.offset); + vec2 absoluut_pos = AbsolutePosition::get_position(data.transform, data.text.offset); std::unique_ptr> font_surface; std::unique_ptr> font_texture; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1dada74..7f07d34 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -18,8 +18,8 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "types.h" +#include "types.h" #include "EventData.h" #include "FontFacade.h" diff --git a/src/crepe/util/AbsoluutPosition.cpp b/src/crepe/util/AbsoluutPosition.cpp deleted file mode 100644 index 296cc09..0000000 --- a/src/crepe/util/AbsoluutPosition.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "AbsoluutPosition.h" - -using namespace crepe; - -vec2 AbsoluutPosition::get_position(const Transform & transform, const vec2 & offset) { - // Get the rotation in radians - float radians1 = transform.rotation * (M_PI / 180.0); - - // Calculate total offset with scale - vec2 total_offset = offset * transform.scale; - - // Rotate - float rotated_total_offset_x1 - = total_offset.x * cos(radians1) - total_offset.y * sin(radians1); - float rotated_total_offset_y1 - = total_offset.x * sin(radians1) + total_offset.y * cos(radians1); - - // Final positions considering scaling and rotation - return (transform.position + vec2(rotated_total_offset_x1, rotated_total_offset_y1)); -} diff --git a/src/crepe/util/AbsoluutPosition.h b/src/crepe/util/AbsoluutPosition.h deleted file mode 100644 index 30a7f93..0000000 --- a/src/crepe/util/AbsoluutPosition.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "api/Transform.h" - -#include "types.h" - -namespace crepe { - -class AbsoluutPosition { -public: - static vec2 get_position(const Transform & transform, const vec2 & offset); -}; - -} // namespace crepe -- cgit v1.2.3 From 7c8c94663fdedb212e993eec232e6d40290cb67e Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 19 Dec 2024 15:51:59 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 8 ++++---- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/RenderSystem.h | 1 - src/example/rendering_particle.cpp | 7 +++---- 4 files changed, 8 insertions(+), 10 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index dc3f362..6fb312b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -153,13 +153,13 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset; if (ctx.sprite.data.world_space) { - screen_pos = (screen_pos - cam_aux_data.cam_pos - + cam_aux_data.zoomed_viewport / 2) + screen_pos = (screen_pos - cam_aux_data.cam_pos + cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; } else { - screen_pos = (screen_pos + cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale - - size / 2 + cam_aux_data.bar_size; + screen_pos + = (screen_pos + cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; } cout << "HALLO" << endl; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 7f07d34..e570073 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -19,9 +19,9 @@ #include "api/Sprite.h" #include "api/Transform.h" -#include "types.h" #include "EventData.h" #include "FontFacade.h" +#include "types.h" namespace crepe { class Texture; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index ed55dd5..14e5c2d 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -66,7 +66,6 @@ private: * \return returns a sorted reference vector */ RefVector sort(RefVector & objs) const; - }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index b623596..5440fdd 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -38,7 +38,7 @@ public: .size = {1, 1}, .angle_offset = 0, .position_offset = {0, 1}, - .world_space = false, + .world_space = false, }); //auto & emitter = game_object.add_component(test_sprite, ParticleEmitter::Data{}); @@ -47,16 +47,15 @@ public: .color = color, .size = {1, 1}, .position_offset = {0, -1}, - .world_space = false, + .world_space = false, }); auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{5, 5}, Camera::Data{ .bg_color = Color::WHITE, - .postion_offset = {1000,1000}, + .postion_offset = {1000, 1000}, }); - /* game_object.add_component(vec2{1, 1}, vec2{0, -0.5}, "ComicSansMS", Text::Data{.text_color = Color::RED}, "test TEST"); -- cgit v1.2.3