mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
adding text management in engine
This commit is contained in:
65
ScopEngine/Runtime/Sources/Graphics/Font.cpp
git.filemode.normal_file
65
ScopEngine/Runtime/Sources/Graphics/Font.cpp
git.filemode.normal_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);
|
||||
}
|
||||
}
|
||||
@@ -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
48
ScopEngine/Runtime/Sources/Graphics/Text.cpp
git.filemode.normal_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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user