adding text management in engine

This commit is contained in:
2025-05-31 23:54:35 +02:00
parent 92ea24c313
commit 202b269302
17 changed files with 9058 additions and 2 deletions

2833
ScopEngine/Runtime/Includes/Graphics/DogicaTTF.h git.filemode.normal_file

File diff suppressed because it is too large Load Diff

61
ScopEngine/Runtime/Includes/Graphics/Font.h git.filemode.normal_file
View File

@@ -0,0 +1,61 @@
#ifndef __SCOP_FONT__
#define __SCOP_FONT__
#include <variant>
#include <filesystem>
#include <unordered_set>
#include <stb_truetype.h>
#include <Renderer/Image.h>
namespace Scop
{
constexpr const int RANGE = 1024;
class Font
{
public:
Font(const std::filesystem::path& path, float scale) : m_build_data(path), m_name(path.string()), m_scale(scale) {}
Font(const std::string& name, const std::vector<std::uint8_t>& ttf_data, float scale) : m_build_data(ttf_data), m_name(name), m_scale(scale) {}
void BuildFont();
void Destroy();
inline const std::string& GetName() const { return m_name; }
inline float GetScale() const noexcept { return m_scale; }
inline const std::array<stbtt_packedchar, 96>& GetCharData() const { return m_cdata; }
inline const Texture& GetTexture() const noexcept { return m_atlas; }
inline bool operator==(const Font& rhs) const { return rhs.m_name == m_name && rhs.m_scale == m_scale; }
inline bool operator!=(const Font& rhs) const { return rhs.m_name != m_name || rhs.m_scale != m_scale; }
inline ~Font() { Destroy(); }
private:
std::array<stbtt_packedchar, 96> m_cdata;
Texture m_atlas;
std::variant<std::filesystem::path, std::vector<std::uint8_t>> m_build_data;
std::string m_name;
float m_scale;
};
class FontRegistry
{
public:
FontRegistry() = default;
inline void RegisterFont(std::shared_ptr<Font> font);
inline void UnregisterFont(std::shared_ptr<Font> font);
inline std::shared_ptr<Font> GetFont(const std::filesystem::path& name, float scale);
inline void Reset();
~FontRegistry() = default;
private:
std::unordered_set<std::shared_ptr<Font>> m_fonts_registry;
};
}
#include <Graphics/Font.inl>
#endif

31
ScopEngine/Runtime/Includes/Graphics/Font.inl git.filemode.normal_file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <Graphics/Font.h>
namespace Scop
{
void FontRegistry::RegisterFont(std::shared_ptr<Font> font)
{
m_fonts_registry.insert(font);
}
void FontRegistry::UnregisterFont(std::shared_ptr<Font> font)
{
m_fonts_registry.erase(font);
}
std::shared_ptr<Font> FontRegistry::GetFont(const std::filesystem::path& name, float scale)
{
auto it = std::find_if(m_fonts_registry.begin(), m_fonts_registry.end(), [&name, scale](std::shared_ptr<Font> rhs)
{
return (name == rhs->GetName() && scale == rhs->GetScale());
});
return (it != m_fonts_registry.end() ? *it : nullptr);
}
void FontRegistry::Reset()
{
for(auto& font: m_fonts_registry)
font->Destroy();
m_fonts_registry.clear();
}
}

View File

@@ -17,6 +17,8 @@
#include <Graphics/Cameras/Base.h>
#include <Renderer/Pipelines/Shader.h>
#include <Renderer/Pipelines/Graphics.h>
#include <Graphics/Font.h>
#include <Graphics/Text.h>
namespace Scop
{
@@ -66,6 +68,11 @@ namespace Scop
Sprite& CreateSprite(std::shared_ptr<Texture> texture) noexcept;
Sprite& CreateSprite(std::string_view name, std::shared_ptr<Texture> texture);
Text& CreateText(std::string text) noexcept;
Text& CreateText(std::string_view name, std::string text);
void LoadFont(std::filesystem::path path, float scale);
void RemoveActor(Actor& actor) noexcept;
void RemoveNarrator(Narrator& narrator) noexcept;
void RemoveSprite(Sprite& sprite) noexcept;
@@ -79,6 +86,7 @@ namespace Scop
[[nodiscard]] inline PostProcessData& GetPostProcessData() noexcept { return m_post_process; }
[[nodiscard]] inline const std::unordered_map<std::uint64_t, Actor>& GetActors() const noexcept { return m_actors; }
[[nodiscard]] inline const std::unordered_map<std::uint64_t, Sprite>& GetSprites() const noexcept { return m_sprites; }
[[nodiscard]] inline const std::unordered_map<std::uint64_t, Text>& GetTexts() const noexcept { return m_texts; }
[[nodiscard]] inline const std::string& GetName() const noexcept { return m_name; }
[[nodiscard]] inline GraphicPipeline& GetPipeline() noexcept { return m_pipeline; }
[[nodiscard]] inline std::shared_ptr<BaseCamera> GetCamera() const { return m_descriptor.camera; }
@@ -101,13 +109,16 @@ namespace Scop
PostProcessData m_post_process;
DepthImage m_depth;
SceneDescriptor m_descriptor;
FontRegistry m_fonts_registry;
std::shared_ptr<CubeTexture> p_skybox;
std::unordered_map<std::uint64_t, Actor> m_actors;
std::unordered_map<std::uint64_t, Text> m_texts;
std::unordered_map<std::uint64_t, Sprite> m_sprites;
std::unordered_map<std::uint64_t, Narrator> m_narrators;
std::vector<Scene> m_scene_children;
std::string m_name;
NonOwningPtr<Scene> p_parent;
std::shared_ptr<Font> p_bound_font;
};
}

56
ScopEngine/Runtime/Includes/Graphics/Text.h git.filemode.normal_file
View File

@@ -0,0 +1,56 @@
#ifndef __SCOP_TEXT__
#define __SCOP_TEXT__
#include <Graphics/Font.h>
#include <Graphics/Mesh.h>
#include <Renderer/Descriptor.h>
namespace Scop
{
class Text
{
friend class Render2DPass;
public:
Text(std::uint64_t uuid, const std::string& text, std::shared_ptr<Font> font);
inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec2ui position) noexcept { m_position = position; }
inline void SetScale(Vec2f scale) noexcept { m_scale = scale; }
[[nodiscard]] inline const std::string& GetText() const { return m_text; }
[[nodiscard]] inline std::shared_ptr<Font> GetFont() const { return p_font; }
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec2ui& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline const Vec2f& GetScale() const noexcept { return m_scale; }
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] inline std::uint64_t GetUUID() const noexcept { return m_uuid; }
virtual ~Text() = default;
private:
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
inline void UpdateDescriptorSet(const DescriptorSet& set)
{
m_set = set.Duplicate();
}
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
{
m_set.SetImage(frame_index, 0, const_cast<Texture&>(p_font->GetTexture()));
m_set.Update(frame_index, cmd);
}
private:
DescriptorSet m_set;
std::shared_ptr<Mesh> p_mesh;
std::shared_ptr<Font> p_font;
std::string m_text;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec2ui m_position = Vec2ui{ 0, 0 };
Vec2f m_scale = Vec2f{ 1.0f, 1.0f };
std::uint64_t m_uuid;
};
}
#endif

65
ScopEngine/Runtime/Sources/Graphics/Font.cpp git.filemode.normal_file
View File

@@ -0,0 +1,65 @@
#include <Graphics/Font.h>
#include <Core/Logs.h>
#include <fstream>
#define STBRP_ASSERT(x) Scop::Assert(x, "internal stb assertion " #x)
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
namespace Scop
{
void Font::BuildFont()
{
std::vector<std::uint8_t> file_bytes;
if(std::holds_alternative<std::filesystem::path>(m_build_data))
{
std::ifstream file(std::get<std::filesystem::path>(m_build_data), std::ios::binary);
if(!file.is_open())
{
Error("Font: cannot open font file, %", m_name);
return;
}
std::ifstream::pos_type file_size = std::filesystem::file_size(std::get<std::filesystem::path>(m_build_data));
file.seekg(0, std::ios::beg);
file_bytes.resize(file_size);
file.read(reinterpret_cast<char*>(file_bytes.data()), file_size);
file.close();
}
CPUBuffer bitmap(RANGE * RANGE);
stbtt_pack_context pc;
stbtt_PackBegin(&pc, bitmap.GetData(), RANGE, RANGE, RANGE, 1, nullptr);
if(std::holds_alternative<std::filesystem::path>(m_build_data))
stbtt_PackFontRange(&pc, file_bytes.data(), 0, m_scale, 32, 96, m_cdata.data());
else
stbtt_PackFontRange(&pc, std::get<std::vector<std::uint8_t>>(m_build_data).data(), 0, m_scale, 32, 96, m_cdata.data());
stbtt_PackEnd(&pc);
// TODO : find better solution; No, using VK_FORMAT_R8_SRGB does not work
CPUBuffer vulkan_bitmap(RANGE * RANGE * 4);
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
{
vulkan_bitmap.GetData()[j + 0] = bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 1] = bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 2] = bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 3] = bitmap.GetData()[i];
}
m_atlas.Init(std::move(vulkan_bitmap), RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, m_name + "_font_atlas_" + std::to_string(m_scale));
Message("Font: loaded % with a scale of %", m_name, m_scale);
}
void Font::Destroy()
{
if(!m_atlas.IsInit())
return;
m_atlas.Destroy();
Message("Font: unloaded % with a scale of %", m_name, m_scale);
}
}

View File

@@ -6,6 +6,7 @@
#include <Renderer/ViewerData.h>
#include <Core/EventBus.h>
#include <Core/Engine.h>
#include <Graphics/DogicaTTF.h>
#include <cstring>
@@ -14,11 +15,13 @@ namespace Scop
Scene::Scene(std::string_view name, SceneDescriptor desc)
: m_name(name), m_descriptor(std::move(desc)), p_parent(nullptr)
{
LoadFont("default", 6.0f);
}
Scene::Scene(std::string_view name, SceneDescriptor desc, NonOwningPtr<Scene> parent)
: m_name(name), m_descriptor(std::move(desc)), p_parent(parent)
{
LoadFont("default", 6.0f);
}
Actor& Scene::CreateActor(Model model) noexcept
@@ -57,6 +60,33 @@ namespace Scop
return m_sprites.try_emplace(uuid, uuid, texture).first->second;
}
Text& Scene::CreateText(std::string text) noexcept
{
UUID uuid = UUID();
return m_texts.try_emplace(uuid, uuid, std::move(text), p_bound_font).first->second;
}
Text& Scene::CreateText(std::string_view name, std::string text)
{
UUID uuid = UUID();
return m_texts.try_emplace(uuid, uuid, std::move(text), p_bound_font).first->second;
}
void Scene::LoadFont(std::filesystem::path path, float scale)
{
std::shared_ptr<Font> font = m_fonts_registry.GetFont(path, scale);
if(!font)
{
if(path.string() == "default")
font = std::make_shared<Font>("default", dogica_ttf, scale);
else
font = std::make_shared<Font>(std::move(path), scale);
font->BuildFont();
m_fonts_registry.RegisterFont(font);
}
p_bound_font = font;
}
void Scene::RemoveActor(Actor& actor) noexcept
{
auto it = m_actors.find(actor.GetUUID());
@@ -175,6 +205,7 @@ namespace Scop
m_forward.matrices_buffer->Destroy();
if(m_post_process.data_buffer)
m_post_process.data_buffer->Destroy();
m_fonts_registry.Reset();
for(auto& child : m_scene_children)
child.Destroy();
}

48
ScopEngine/Runtime/Sources/Graphics/Text.cpp git.filemode.normal_file
View File

@@ -0,0 +1,48 @@
#include <Graphics/Text.h>
#include <Renderer/Vertex.h>
#include <vector>
namespace Scop
{
Text::Text(std::uint64_t uuid, const std::string& text, std::shared_ptr<Font> font) : m_uuid(uuid)
{
Assert(font != nullptr, "invalid font");
std::vector<Vertex> vertex_data;
std::vector<std::uint32_t> index_data;
float stb_x = 0.0f;
float stb_y = 0.0f;
const auto& char_data = font->GetCharData();
for(char c : text)
{
if(c < 32)
continue;
stbtt_aligned_quad q;
stbtt_GetPackedQuad(char_data.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
std::size_t index = vertex_data.size();
vertex_data.emplace_back(Vec4f{ q.x0, q.y0, 0.0f, 1.0f }, Vec4f{ 1.0f }, -Vec2f{ q.s0, -q.t0 });
vertex_data.emplace_back(Vec4f{ q.x1, q.y0, 0.0f, 1.0f }, Vec4f{ 1.0f }, -Vec2f{ q.s1, -q.t0 });
vertex_data.emplace_back(Vec4f{ q.x1, q.y1, 0.0f, 1.0f }, Vec4f{ 1.0f }, -Vec2f{ q.s1, -q.t1 });
vertex_data.emplace_back(Vec4f{ q.x0, q.y1, 0.0f, 1.0f }, Vec4f{ 1.0f }, -Vec2f{ q.s0, -q.t1 });
index_data.push_back(index + 0);
index_data.push_back(index + 1);
index_data.push_back(index + 2);
index_data.push_back(index + 2);
index_data.push_back(index + 3);
index_data.push_back(index + 0);
}
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) });
p_mesh = mesh;
p_font = font;
m_text = text;
}
}

View File

@@ -98,6 +98,19 @@ namespace Scop
RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data);
sprite.GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef());
}
for(const auto& [_, text] : scene.GetTexts())
{
SpriteData sprite_data;
sprite_data.position = Vec2f{ static_cast<float>(text.GetPosition().x), static_cast<float>(text.GetPosition().y) };
sprite_data.color = text.GetColor();
if(!text.IsSetInit())
const_cast<Text&>(text).UpdateDescriptorSet(*p_texture_set);
const_cast<Text&>(text).Bind(frame_index, cmd);
std::array<VkDescriptorSet, 2> sets = { p_viewer_data_set->GetSet(frame_index), text.GetSet(frame_index) };
RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data);
text.GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef());
}
m_pipeline.EndPipeline(cmd);
}