/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* TextDescriptor.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */ /* Updated: 2024/04/24 01:38:40 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #define STB_RECT_PACK_IMPLEMENTATION #include #include #define STB_TRUETYPE_IMPLEMENTATION #define STB_malloc(x, u) ((void)(u), MemManager::Malloc(x)) #define STB_free(x, u) ((void)(u), MemManager::Free(x)) #include constexpr const int RANGE = 1024; namespace mlx { TextDrawDescriptor::TextDrawDescriptor(std::string text, std::uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), m_text(std::move(text)) {} void TextDrawDescriptor::Init(FontID font) noexcept { MLX_PROFILE_FUNCTION(); std::vector vertex_data; std::vector index_data; float stb_x = 0.0f; float stb_y = 0.0f; { std::shared_ptr font_data = FontLibrary::Get().GetFontData(font); for(char c : m_text) { if(c < 32) continue; stbtt_aligned_quad q; stbtt_GetPackedQuad(font_data->GetCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1); std::size_t index = vertex_data.size(); glm::vec4 vertex_color = { static_cast((color & 0x000000FF)) / 255.f, static_cast((color & 0x0000FF00) >> 8) / 255.f, static_cast((color & 0x00FF0000) >> 16) / 255.f, static_cast((color & 0xFF000000) >> 24) / 255.f }; vertex_data.emplace_back(glm::vec2{q.x0, q.y0}, vertex_color, glm::vec2{q.s0, q.t0}); vertex_data.emplace_back(glm::vec2{q.x1, q.y0}, vertex_color, glm::vec2{q.s1, q.t0}); vertex_data.emplace_back(glm::vec2{q.x1, q.y1}, vertex_color, glm::vec2{q.s1, q.t1}); vertex_data.emplace_back(glm::vec2{q.x0, q.y1}, vertex_color, glm::vec2{q.s0, q.t1}); index_data.emplace_back(index + 0); index_data.emplace_back(index + 1); index_data.emplace_back(index + 2); index_data.emplace_back(index + 2); index_data.emplace_back(index + 3); index_data.emplace_back(index + 0); } } std::shared_ptr text_data = std::make_shared(); text_data->Init(m_text, font, color, std::move(vertex_data), std::move(index_data)); id = TextLibrary::Get().AddTextToLibrary(text_data); DebugLog("Text put : registered new text to render"); } void TextDrawDescriptor::Render(Renderer& renderer) { MLX_PROFILE_FUNCTION(); std::shared_ptr draw_data = TextLibrary::Get().GetTextData(id); std::shared_ptr font_data = FontLibrary::Get().GetFontData(draw_data->GetFontInUse()); TextureAtlas& atlas = const_cast(font_data->GetAtlas()); draw_data->Bind(renderer); if(!atlas.GetSet().IsInit()) atlas.SetDescriptor(renderer.GetFragDescriptorSet().Duplicate()); if(!atlas.HasBeenUpdated()) atlas.UpdateSet(0); atlas.GetSet().Bind(); atlas.Render(renderer, x, y, draw_data->GetIBOsize()); } void TextDrawDescriptor::ResetUpdate() { std::shared_ptr draw_data = TextLibrary::Get().GetTextData(id); std::shared_ptr font_data = FontLibrary::Get().GetFontData(draw_data->GetFontInUse()); TextureAtlas& atlas = const_cast(font_data->GetAtlas()); atlas.ResetUpdate(); } }