From 875ad80337d81a54f1e52c63b42cb802f40706a4 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Fri, 2 Aug 2024 23:05:30 +0300 Subject: [PATCH] give more meaningful names --- CMakeLists.txt | 4 +- ...sources.cmake => CopyResourcesToExe.cmake} | 2 +- .../Scene/{Text.cpp => TextDrawable.cpp} | 38 ++++++++++--------- .../Scene/{Text.hpp => TextDrawable.hpp} | 6 +-- 4 files changed, 27 insertions(+), 23 deletions(-) rename cmake/{CopyResources.cmake => CopyResourcesToExe.cmake} (93%) rename openVulkanoCpp/Scene/{Text.cpp => TextDrawable.cpp} (73%) rename openVulkanoCpp/Scene/{Text.hpp => TextDrawable.hpp} (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a66998..f94e957 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ include(cmake/SetupVulkan.cmake) include(cmake/Filter.cmake) include(cmake/AppleHelper.cmake) include(cmake/SetShaderDependency.cmake) -include(cmake/CopyResources.cmake) +include(cmake/CopyResourcesToExe.cmake) set(DEPENDENCY_MIRROR_FILE "DependencyMirrors.txt" CACHE STRING "Dependency mirror") VarsFromFile("${DEPENDENCY_MIRROR_FILE}") # Load mirror list (for CICD) @@ -108,7 +108,7 @@ if (WIN32) endif () SetupVulkan(openVulkanoCpp) -CopyResources(openVulkanoCpp "${CMAKE_CURRENT_SOURCE_DIR}/fonts" ".ttf") +CopyResourcesToExe(openVulkanoCpp "${CMAKE_CURRENT_SOURCE_DIR}/fonts" ".ttf") SetShaderDependency(openVulkanoCpp ${CMAKE_CURRENT_SOURCE_DIR}/openVulkanoCpp/Shader ${SHADER_OUTPUT_DEST}) diff --git a/cmake/CopyResources.cmake b/cmake/CopyResourcesToExe.cmake similarity index 93% rename from cmake/CopyResources.cmake rename to cmake/CopyResourcesToExe.cmake index a431490..9e237e6 100644 --- a/cmake/CopyResources.cmake +++ b/cmake/CopyResourcesToExe.cmake @@ -1,4 +1,4 @@ -function(CopyResources TARGET FROM EXTENSIONS) +function(CopyResourcesToExe TARGET FROM EXTENSIONS) file(GLOB RESOURCES "${FROM}/*") set(RESOURCES_TO_COPY "") diff --git a/openVulkanoCpp/Scene/Text.cpp b/openVulkanoCpp/Scene/TextDrawable.cpp similarity index 73% rename from openVulkanoCpp/Scene/Text.cpp rename to openVulkanoCpp/Scene/TextDrawable.cpp index b996ffd..e52f66a 100644 --- a/openVulkanoCpp/Scene/Text.cpp +++ b/openVulkanoCpp/Scene/TextDrawable.cpp @@ -4,13 +4,14 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "Text.hpp" +#include "TextDrawable.hpp" #include "Scene/Geometry.hpp" #include "Scene/Material.hpp" #include "Scene/Vertex.hpp" #include "Scene/UniformBuffer.hpp" #include "Scene/FontAtlasGenerator.hpp" #include "Base/Logger.hpp" +#include "utf8.h" #include "fmt/core.h" namespace OpenVulkano::Scene @@ -18,13 +19,13 @@ namespace OpenVulkano::Scene using namespace msdfgen; using namespace msdf_atlas; - Text::~Text() + TextDrawable::~TextDrawable() { delete m_mesh; delete m_material; } - void Text::GenerateText(const std::string& text, const Math::Vector3f& pos) + void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos) { if (!m_fontAtlasGenerator) { @@ -62,9 +63,11 @@ namespace OpenVulkano::Scene }; double cursorX = pos.x; - for (size_t i = 0; i < text.size(); i++) + auto begin = text.begin(); + auto end = text.end(); + for (size_t i = 0; begin != end; i++) { - unicode_t c = text[i]; + unicode_t c = utf8::next(begin, end); if (symbols.find(c) != symbols.end()) { Bbox glyphBaselineBbox, glyphAtlasBbox; @@ -86,41 +89,42 @@ namespace OpenVulkano::Scene double ax = cursorX + bearingX; double ay = pos.y - (h - bearingY); - m_material->texture = &info.texture; + const Texture& atlasTex = m_fontAtlasGenerator->GetAtlas(); + m_material->texture = const_cast(&atlasTex); m_mesh->vertices[vIdx].position.x = ax; m_mesh->vertices[vIdx].position.y = ay; m_mesh->vertices[vIdx].position.z = 1; - m_mesh->vertices[vIdx].textureCoordinates.x = l / info.texture.resolution.x; - m_mesh->vertices[vIdx].textureCoordinates.y = b / info.texture.resolution.y; + m_mesh->vertices[vIdx].textureCoordinates.x = l / atlasTex.resolution.x; + m_mesh->vertices[vIdx].textureCoordinates.y = b / atlasTex.resolution.y; m_mesh->vertices[vIdx + 1].position.x = ax + w; m_mesh->vertices[vIdx + 1].position.y = ay; m_mesh->vertices[vIdx + 1].position.z = 1; - m_mesh->vertices[vIdx + 1].textureCoordinates.x = r / info.texture.resolution.x; - m_mesh->vertices[vIdx + 1].textureCoordinates.y = b / info.texture.resolution.y; + m_mesh->vertices[vIdx + 1].textureCoordinates.x = r / atlasTex.resolution.x; + m_mesh->vertices[vIdx + 1].textureCoordinates.y = b / atlasTex.resolution.y; m_mesh->vertices[vIdx + 2].position.x = ax + w; m_mesh->vertices[vIdx + 2].position.y = ay + h; m_mesh->vertices[vIdx + 2].position.z = 1; - m_mesh->vertices[vIdx + 2].textureCoordinates.x = r / info.texture.resolution.x; - m_mesh->vertices[vIdx + 2].textureCoordinates.y = t / info.texture.resolution.y; + m_mesh->vertices[vIdx + 2].textureCoordinates.x = r / atlasTex.resolution.x; + m_mesh->vertices[vIdx + 2].textureCoordinates.y = t / atlasTex.resolution.y; m_mesh->vertices[vIdx + 3].position.x = ax; m_mesh->vertices[vIdx + 3].position.y = ay + h; m_mesh->vertices[vIdx + 3].position.z = 1; - m_mesh->vertices[vIdx + 3].textureCoordinates.x = l / info.texture.resolution.x; - m_mesh->vertices[vIdx + 3].textureCoordinates.y = t / info.texture.resolution.y; - + m_mesh->vertices[vIdx + 3].textureCoordinates.x = l / atlasTex.resolution.x; + m_mesh->vertices[vIdx + 3].textureCoordinates.y = t / atlasTex.resolution.y; + m_mesh->SetIndices(indices, 6, 6 * i); // TODO: change to lower value(or ideally remove completely) to avoid overlapping and make less space between symbols // when setting for depth comparison operator will be available( <= ) - cursorX += info.glyphBox.advance +0.08; + cursorX += info.glyphBox.advance + 0.08; } else { // throw ? replace with ? character (if available) ? - Logger::RENDER->error(fmt::format("Could not find glyph for character", c)); + Logger::RENDER->error(fmt::format("Could not find glyph for character {}", c)); } } } diff --git a/openVulkanoCpp/Scene/Text.hpp b/openVulkanoCpp/Scene/TextDrawable.hpp similarity index 94% rename from openVulkanoCpp/Scene/Text.hpp rename to openVulkanoCpp/Scene/TextDrawable.hpp index 7df40b6..4b09a72 100644 --- a/openVulkanoCpp/Scene/Text.hpp +++ b/openVulkanoCpp/Scene/TextDrawable.hpp @@ -34,11 +34,11 @@ namespace OpenVulkano::Scene //bool sdfMultiChannel = false; }; - class Text : public SimpleDrawable + class TextDrawable : public SimpleDrawable { public: - Text() = default; - ~Text(); + TextDrawable() = default; + ~TextDrawable(); void SetUniformBuffer(UniformBuffer* buffer) { m_uniBuffer = buffer; } void GenerateText(const std::string& text, const Math::Vector3f& pos = Math::Vector3f(0.f)); void SetConfig(const TextConfig& cfg) { m_cfg = cfg; }