/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* text_descriptor.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */ /* Updated: 2024/03/25 19:04:52 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include #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), _text(std::move(text)) {} void TextDrawDescriptor::init(FontID font) noexcept { MLX_PROFILE_FUNCTION(); std::vector vertexData; std::vector indexData; float stb_x = 0.0f; float stb_y = 0.0f; { std::shared_ptr font_data = FontLibrary::get().getFontData(font); for(char c : _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 = vertexData.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 }; vertexData.emplace_back(glm::vec2{q.x0, q.y0}, vertex_color, glm::vec2{q.s0, q.t0}); vertexData.emplace_back(glm::vec2{q.x1, q.y0}, vertex_color, glm::vec2{q.s1, q.t0}); vertexData.emplace_back(glm::vec2{q.x1, q.y1}, vertex_color, glm::vec2{q.s1, q.t1}); vertexData.emplace_back(glm::vec2{q.x0, q.y1}, vertex_color, glm::vec2{q.s0, q.t1}); indexData.emplace_back(index + 0); indexData.emplace_back(index + 1); indexData.emplace_back(index + 2); indexData.emplace_back(index + 2); indexData.emplace_back(index + 3); indexData.emplace_back(index + 0); } } std::shared_ptr text_data = std::make_shared(); text_data->init(_text, font, color, std::move(vertexData), std::move(indexData)); id = TextLibrary::get().addTextToLibrary(text_data); #ifdef DEBUG core::error::report(e_kind::message, "Text put : registered new text to render"); #endif } void TextDrawDescriptor::render(std::array& sets, 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); sets[1] = const_cast(atlas).getVkSet(); vkCmdBindDescriptorSets(renderer.getActiveCmdBuffer().get(), VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); 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(); } }