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