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.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/crepe/facade/SDLContext.cpp') 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; -- cgit v1.2.3 From 2f8d36a33854715828d412792663f355b9e393ae Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 09:08:06 +0100 Subject: idk --- src/crepe/api/Config.h | 2 +- src/crepe/facade/Font.cpp | 4 +++- src/crepe/facade/SDLContext.cpp | 23 +++++++++++++---------- src/crepe/system/RenderSystem.cpp | 2 +- src/example/rendering_particle.cpp | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index c20287d..b86faff 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -85,7 +85,7 @@ struct Config final { * This config option is the font size at which all fonts will be loaded initially. * */ - unsigned int size = 16; + unsigned int size = 32; } font; //! Audio system settings diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 83e8519..a9f1633 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,5 +1,6 @@ #include "../api/Config.h" #include "util/Log.h" +#include #include "Font.h" @@ -10,7 +11,8 @@ Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { dbg_trace(); Config & config = Config::get_instance(); - const std::string FONT_PATH = src.get_path(); + const std::string & FONT_PATH = src.get_path(); + cout << FONT_PATH.c_str() << endl; TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (font == NULL) throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4e969b9..a06940d 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -34,9 +34,6 @@ using namespace std; SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); - if (TTF_Init() == -1) { - throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); - } if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -65,6 +62,10 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + if (TTF_Init() == -1) { + throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); + } + mediator.sdl_context = *this; } @@ -77,8 +78,8 @@ SDLContext::~SDLContext() { // TODO: how are we going to ensure that these are called from the same // thread that SDL_Init() was called on? This has caused problems for me // before. - IMG_Quit(); TTF_Quit(); + IMG_Quit(); SDL_Quit(); } @@ -297,13 +298,15 @@ void SDLContext::draw_text(const Text & text, const Font & font){ 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_Rect dstrect { + .x = (int)text.offset.x, + .y = (int)text.offset.y, + .w = (int)text.dimensions.x, + .h = (int)text.dimensions.y, }; - SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, &dstrect , 0 , NULL, SDL_FLIP_NONE); + + 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_DestroyTexture(font_texture); } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index f2ed97d..9611a9e 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -84,7 +84,7 @@ void RenderSystem::render_text(){ for (const Text & text : texts) { if (!text.active) continue; const Font & res = resource_manager.get(text.font); - //ctx.draw_text(text, font); + ctx.draw_text(text, res); } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3857acc..21ed7fc 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -66,7 +66,7 @@ public: .bg_color = Color::WHITE, }); - game_object.add_component(vec2{200,200}, vec2{0,0}, "test test", "OpenSymbol", Text::Data{}); + game_object.add_component(vec2{200,200}, vec2{0,0}, "test test", "", Text::Data{}); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From bcaee968761c1d2e85c20925b237480c87da9747 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 13:29:58 +0100 Subject: tmp fix --- src/crepe/facade/Font.cpp | 11 +++++++++++ src/crepe/facade/Font.h | 8 ++++++-- src/crepe/facade/FontFacade.cpp | 12 ++++++------ src/crepe/facade/SDLContext.cpp | 2 ++ src/crepe/system/RenderSystem.cpp | 5 +++-- src/example/rendering_particle.cpp | 7 ++++--- 6 files changed, 32 insertions(+), 13 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 8c16563..47d0e39 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -2,6 +2,7 @@ #include "../api/Config.h" #include "util/Log.h" #include +#include #include "Font.h" @@ -13,11 +14,21 @@ Font::Font(const Asset & src, Mediator & mediator) dbg_trace(); Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); + + this->path = FONT_PATH; + + cout << this->path << endl; + /* TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (loaded_font == NULL) { throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); } this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; + */ } TTF_Font * Font::get_font() const { return this->font.get(); } + +const string & Font::get_path() const noexcept{ + return this->path; +} diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 91c59c0..0b1768b 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,10 +1,11 @@ #pragma once #include +#include #include +#include #include "../Resource.h" -#include "../api/Config.h" namespace crepe { @@ -24,7 +25,6 @@ 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. @@ -36,9 +36,13 @@ public: */ TTF_Font * get_font() const; + const std::string & get_path() const noexcept; + private: //! The SDL_ttf font object with custom deleter. std::unique_ptr> font; + + std::string path; }; } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index aa9d00c..a0464e0 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "FontFacade.h" @@ -12,13 +13,13 @@ Asset FontFacade::get_font_asset(const string font_family) { } // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (pattern == NULL) { + if (!pattern) { throw runtime_error("Failed to create font pattern."); } // Default configuration FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { + if (!config) { FcPatternDestroy(pattern); throw runtime_error("Failed to get current Fontconfig configuration."); } @@ -28,15 +29,14 @@ Asset FontFacade::get_font_asset(const string font_family) { FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); FcPatternDestroy(pattern); - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); + if (!matched_pattern) { throw runtime_error("No matching font found."); } // Extract the file path FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || file_path == NULL) { + || !file_path) { FcPatternDestroy(matched_pattern); throw runtime_error("Failed to get font file path."); } @@ -44,6 +44,6 @@ Asset FontFacade::get_font_asset(const string font_family) { // Convert the file path to a string string font_file_path = reinterpret_cast(file_path); FcPatternDestroy(matched_pattern); - FcFini(); + cout << font_file_path << endl; return Asset(font_file_path); } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a06940d..1befbe5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -294,7 +294,9 @@ void SDLContext::draw_text(const Text & text, const Font & font){ .b = text.data.text_color.b, .a = text.data.text_color.a, }; + TTF_Font * ttf_font = TTF_OpenFont(font.get_path().c_str(), Config::get_instance().font.size); SDL_Surface * font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color); + TTF_CloseFont(ttf_font); SDL_Texture * font_texture = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface); SDL_FreeSurface(font_surface); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 9611a9e..4a70843 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,15 +13,16 @@ #include "../facade/Texture.h" #include "../manager/ComponentManager.h" #include "../manager/ResourceManager.h" - -#include "RenderSystem.h" #include "api/Text.h" #include "facade/Font.h" + +#include "RenderSystem.h" #include "types.h" using namespace crepe; using namespace std; + void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 21ed7fc..b261f52 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,3 +1,5 @@ + + #include "api/Asset.h" #include "api/Text.h" #include @@ -14,7 +16,6 @@ #include #include #include -#include using namespace crepe; using namespace std; @@ -65,8 +66,8 @@ public: Camera::Data{ .bg_color = Color::WHITE, }); - - game_object.add_component(vec2{200,200}, vec2{0,0}, "test test", "", Text::Data{}); + game_object.add_component(vec2{200, 200}, vec2{0, 0}, "test test", "Ariel.ttf", + Text::Data{}); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From 0c02511f6a64766174cb0db3beb4cc159d3efdf7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 13:36:17 +0100 Subject: merging wouter --- src/crepe/facade/Font.cpp | 9 --------- src/crepe/facade/Font.h | 3 --- src/crepe/facade/SDLContext.cpp | 2 -- src/crepe/system/RenderSystem.cpp | 2 +- 4 files changed, 1 insertion(+), 15 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 51e9690..9c1a800 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -2,8 +2,6 @@ #include "../api/Asset.h" #include "../api/Config.h" -#include "util/Log.h" -#include #include #include "Font.h" @@ -16,10 +14,6 @@ Font::Font(const Asset & src, Mediator & mediator) Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); - this->path = FONT_PATH; - - cout << this->path << endl; - /* TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (loaded_font == NULL) { throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); @@ -29,6 +23,3 @@ Font::Font(const Asset & src, Mediator & mediator) TTF_Font * Font::get_font() const { return this->font.get(); } -const string & Font::get_path() const noexcept{ - return this->path; -} diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 3eff3c6..b08366d 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "../Resource.h" @@ -35,8 +34,6 @@ public: */ TTF_Font * get_font() const; - const std::string & get_path() const noexcept; - private: //! The SDL_ttf font object with custom deleter. std::unique_ptr> font = nullptr; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 7bd9dbc..c19f0aa 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -294,9 +294,7 @@ void SDLContext::draw_text(const Text & text, const Font & font){ .b = text.data.text_color.b, .a = text.data.text_color.a, }; - TTF_Font * ttf_font = TTF_OpenFont(font.get_path().c_str(), Config::get_instance().font.size); SDL_Surface * font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color); - TTF_CloseFont(ttf_font); SDL_Texture * font_texture = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface); SDL_FreeSurface(font_surface); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 7d6a31b..578a4c0 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -87,7 +87,7 @@ void RenderSystem::render_text(){ for (const Text & text : texts) { if (!text.active) continue; - const Font & res = resource_manager.get(text.font); + const Font & res = resource_manager.get(text.font.value()); ctx.draw_text(text, res); } -- 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.cpp') 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 dff8c63a5af46fd3b0115e10efe9f0b67e9111b8 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 13:59:19 +0100 Subject: make format --- src/crepe/facade/Font.cpp | 1 - src/crepe/facade/SDLContext.cpp | 1 - src/crepe/system/RenderSystem.h | 1 - 3 files changed, 3 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 81a9e7a..4694f7c 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -21,4 +21,3 @@ Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { } TTF_Font * Font::get_font() const { return this->font.get(); } - diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 27f2cb3..41c6d21 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -311,7 +311,6 @@ void SDLContext::draw_text(const RenderText & data) { * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; - SDL_FRect dstrect{ .x = screen_pos.x, .y = screen_pos.y, diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 3476765..3aa846e 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(); -- 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.cpp') 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 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.cpp') 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 36f941aa1cdea5faeec20a0dc793e0538ccd6d15 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 20:47:14 +0100 Subject: implemented feedback --- src/crepe/api/Text.cpp | 3 --- src/crepe/api/Text.h | 2 -- src/crepe/facade/Font.cpp | 2 -- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLContext.cpp | 4 ++-- src/crepe/system/RenderSystem.cpp | 1 - 6 files changed, 3 insertions(+), 11 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 9d57abd..4a94180 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,6 +1,3 @@ -#include "../facade/FontFacade.h" -#include "util/Log.h" - #include "Text.h" using namespace crepe; diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index c30dc80..da40141 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -3,8 +3,6 @@ #include #include -#include "../Component.h" - #include "Asset.h" #include "Color.h" #include "UIObject.h" diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 0c670c1..771002f 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -2,7 +2,6 @@ #include "../api/Asset.h" #include "../api/Config.h" -#include #include "Font.h" @@ -12,7 +11,6 @@ using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { const Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); - TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (loaded_font == NULL) { throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index b08366d..b208d96 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,10 +1,10 @@ #pragma once #include -#include #include #include "../Resource.h" +#include "../api/Config.h" namespace crepe { diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ea3b71c..9a122f5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -208,14 +208,14 @@ void SDLContext::draw_text(const RenderText & data) { }; SDL_Surface * tmp_font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color); - if (!tmp_font_surface) { + if (tmp_font_surface == NULL) { 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) { + if (tmp_font_texture == NULL) { throw runtime_error(format("draw_text: font texture error: {}", SDL_GetError())); } font_texture diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4082591..bf2bfd3 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -87,7 +87,6 @@ void RenderSystem::render_text() { if (!text.active) continue; if (!text.font.has_value()) text.font.emplace(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 -- cgit v1.2.3 From ea5e62b4ca8fbe214605abdab486bf6dcc1cc540 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 18 Dec 2024 11:31:54 +0100 Subject: added particle begin lifespan rendering, added world_space to sprite so that coordinates are in camera_space or world_sapce --- src/crepe/api/Sprite.h | 10 ++++++++++ src/crepe/facade/SDLContext.cpp | 17 +++++++++++++---- src/crepe/system/RenderSystem.cpp | 9 ++++++--- src/crepe/system/RenderSystem.h | 3 ++- src/example/rendering_particle.cpp | 25 ++++--------------------- 5 files changed, 35 insertions(+), 29 deletions(-) (limited to 'src/crepe/facade/SDLContext.cpp') diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a2409c2..a3fc319 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -66,6 +66,16 @@ public: //! independent sprite offset position vec2 position_offset; + + /** + * \brief gives the user the option to render this in world space or in camera space + * + * - if true will this be rendered in world space this means that the sprite can be + * rendered off the screen + * - if false --> will the sprite be rendered in camera space. this means that the + * coordinates given on the \c Sprite and \c Transform will be inside the camera + */ + bool world_space = true; }; public: diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fffbe34..35f82d8 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -137,6 +138,9 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio; vec2 size = data.size; + vec2 screen_pos = ctx.pos + data.position_offset; + + if (data.size.x == 0 && data.size.y != 0) { size.x = data.size.y * aspect_ratio; } @@ -145,10 +149,15 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset; - vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos - + (cam_aux_data.zoomed_viewport) / 2) - * cam_aux_data.render_scale - - size / 2 + cam_aux_data.bar_size; + if (ctx.sprite.data.world_space) { + cout << "world_space" << endl; + vec2 multiplier = cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; + screen_pos += multiplier; + } else { + cout << "camera space" << endl; + vec2 multiplier = (cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; + screen_pos += multiplier; + } return SDL_FRect{ .x = screen_pos.x, diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 62d42ec..57d180f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -73,7 +73,8 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const float & transform_angle, + const float & scale) { ComponentManager & mgr = this->mediator.component_manager; SDLContext & ctx = this->mediator.sdl_context; @@ -92,12 +93,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.particles) { if (!p.active) continue; + if (p.time_in_life < em.data.begin_lifespan) continue; ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .texture = res, .pos = p.position, - .angle = p.angle, + .angle = p.angle + transform_angle, .scale = scale, }); } @@ -136,7 +138,8 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform.scale); + bool rendered_particles + = this->render_particle(sprite, transform.rotation, transform.scale); if (rendered_particles) continue; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 56a0553..76df0e0 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -49,7 +49,8 @@ private: * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale); + bool render_particle(const Sprite & sprite, const float & transform_angle, + const float & scale); /** * \brief Renders all Text components * diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index add43f4..1c8072e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -13,7 +13,6 @@ #include #include #include -#include using namespace crepe; using namespace std; @@ -21,11 +20,7 @@ using namespace std; class TestScene : public Scene { public: void load_scene() { - - cout << "TestScene" << endl; - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object = new_object("", "", vec2{0, 0}, 0, 1); Color color(255, 255, 255, 255); @@ -41,25 +36,13 @@ public: .angle_offset = 0, .position_offset = {0, 0}, }); + auto & emitter = game_object.add_component(test_sprite, ParticleEmitter::Data{}); - //auto & anim = game_object.add_component(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); - //anim.set_anim(0); - - auto & cam = game_object.add_component(ivec2{720, 1280}, vec2{400, 400}, + auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ .bg_color = Color::WHITE, }); - - function on_click = [&]() { cout << "button clicked" << std::endl; }; - function on_enter = [&]() { cout << "enter" << std::endl; }; - function on_exit = [&]() { cout << "exit" << std::endl; }; - - auto & button - = game_object.add_component