mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 22:53:34 +00:00
new text management, texts and textures are on the same level, new texture rendering management, fixing issues
This commit is contained in:
88
src/renderer/texts/font.cpp
git.filemode.normal_file
88
src/renderer/texts/font.cpp
git.filemode.normal_file
@@ -0,0 +1,88 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* font.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */
|
||||
/* Updated: 2024/01/11 01:23:20 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <renderer/texts/font.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <core/profiler.h>
|
||||
#include <fstream>
|
||||
|
||||
constexpr const int RANGE = 1024;
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : non_copyable(), _name(path.string()), _scale(scale)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
|
||||
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
|
||||
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if(!file.is_open())
|
||||
{
|
||||
core::error::report(e_kind::error, "Font load : cannot open font file, %s", _name.c_str());
|
||||
return;
|
||||
}
|
||||
std::ifstream::pos_type fileSize = std::filesystem::file_size(path);
|
||||
file.seekg(0, std::ios::beg);
|
||||
std::vector<uint8_t> bytes(fileSize);
|
||||
file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
|
||||
file.close();
|
||||
|
||||
stbtt_pack_context pc;
|
||||
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
|
||||
stbtt_PackFontRange(&pc, bytes.data(), 0, scale, 32, 96, _cdata.data());
|
||||
stbtt_PackEnd(&pc);
|
||||
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
|
||||
{
|
||||
vulkan_bitmap[j + 0] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 1] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 2] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 3] = tmp_bitmap[i];
|
||||
}
|
||||
#ifdef DEBUG
|
||||
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(_name + "_font_altas").c_str(), true);
|
||||
#else
|
||||
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
|
||||
#endif
|
||||
_atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
|
||||
}
|
||||
|
||||
Font::Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale) : non_copyable(), _name(name), _scale(scale)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
|
||||
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
|
||||
stbtt_pack_context pc;
|
||||
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
|
||||
stbtt_PackFontRange(&pc, ttf_data.data(), 0, scale, 32, 96, _cdata.data());
|
||||
stbtt_PackEnd(&pc);
|
||||
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
|
||||
{
|
||||
vulkan_bitmap[j + 0] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 1] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 2] = tmp_bitmap[i];
|
||||
vulkan_bitmap[j + 3] = tmp_bitmap[i];
|
||||
}
|
||||
#ifdef DEBUG
|
||||
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(_name + "_font_altas").c_str(), true);
|
||||
#else
|
||||
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
|
||||
#endif
|
||||
_atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_atlas.destroy();
|
||||
}
|
||||
}
|
||||
60
src/renderer/texts/font.h
git.filemode.normal_file
60
src/renderer/texts/font.h
git.filemode.normal_file
@@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* font.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */
|
||||
/* Updated: 2023/12/14 17:51:40 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_FONT__
|
||||
#define __MLX_FONT__
|
||||
|
||||
#include <array>
|
||||
#include <stb_truetype.h>
|
||||
#include <utils/non_copyable.h>
|
||||
#include <renderer/images/texture_atlas.h>
|
||||
#include <utils/combine_hash.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class Font : public non_copyable
|
||||
{
|
||||
public:
|
||||
Font() = delete;
|
||||
Font(class Renderer& renderer, const std::filesystem::path& path, float scale);
|
||||
Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale);
|
||||
inline const std::string& getName() const { return _name; }
|
||||
inline float getScale() const noexcept { return _scale; }
|
||||
inline const std::array<stbtt_packedchar, 96>& getCharData() const { return _cdata; }
|
||||
inline const TextureAtlas& getAtlas() const noexcept { return _atlas; }
|
||||
inline bool operator==(const Font& rhs) const { return rhs._name == _name; }
|
||||
inline bool operator!=(const Font& rhs) const { return rhs._name != _name; }
|
||||
~Font();
|
||||
|
||||
private:
|
||||
std::array<stbtt_packedchar, 96> _cdata;
|
||||
TextureAtlas _atlas;
|
||||
std::string _name;
|
||||
float _scale = 0;
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<mlx::Font>
|
||||
{
|
||||
std::size_t operator()(const mlx::Font& f) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
mlx::hashCombine(hash, f.getName(), f.getScale());
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
55
src/renderer/texts/text.cpp
git.filemode.normal_file
55
src/renderer/texts/text.cpp
git.filemode.normal_file
@@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 00:11:56 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 03:31:57 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <core/profiler.h>
|
||||
#include <renderer/texts/text.h>
|
||||
#include <renderer/renderer.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Text::init(std::string text, Font const* font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_text = std::move(text);
|
||||
_font = font;
|
||||
#ifdef DEBUG
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), _text.c_str());
|
||||
_ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), _text.c_str());
|
||||
#else
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), nullptr);
|
||||
_ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Text::bind(Renderer& renderer) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_vbo[renderer.getActiveImageIndex()].bind(renderer);
|
||||
_ibo.bind(renderer);
|
||||
}
|
||||
|
||||
void Text::updateVertexData(int frame, std::vector<Vertex> vbo_data)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_vbo[frame].setData(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()));
|
||||
}
|
||||
|
||||
void Text::destroy() noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
_vbo[i].destroy();
|
||||
_ibo.destroy();
|
||||
}
|
||||
}
|
||||
47
src/renderer/texts/text.h
git.filemode.normal_file
47
src/renderer/texts/text.h
git.filemode.normal_file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 00:09:04 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 00:13:25 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_TEXT__
|
||||
#define __MLX_TEXT__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <renderer/texts/font.h>
|
||||
#include <renderer/buffers/vk_ibo.h>
|
||||
#include <renderer/buffers/vk_vbo.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class Text
|
||||
{
|
||||
public:
|
||||
Text() = default;
|
||||
|
||||
void init(std::string text, Font const* font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data);
|
||||
void bind(class Renderer& renderer) noexcept;
|
||||
inline const Font& getFontInUse() const noexcept { return *_font; }
|
||||
void updateVertexData(int frame, std::vector<Vertex> vbo_data);
|
||||
inline uint32_t getIBOsize() noexcept { return _ibo.getSize(); }
|
||||
inline const std::string& getText() const { return _text; }
|
||||
void destroy() noexcept;
|
||||
|
||||
~Text() = default;
|
||||
|
||||
private:
|
||||
std::array<D_VBO, MAX_FRAMES_IN_FLIGHT> _vbo;
|
||||
C_IBO _ibo;
|
||||
std::string _text;
|
||||
Font const* _font = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
104
src/renderer/texts/text_descriptor.cpp
git.filemode.normal_file
104
src/renderer/texts/text_descriptor.cpp
git.filemode.normal_file
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_descriptor.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 03:40:54 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <core/profiler.h>
|
||||
#include <renderer/texts/text_descriptor.h>
|
||||
#include <renderer/images/texture_atlas.h>
|
||||
#include <renderer/texts/font.h>
|
||||
#include <renderer/texts/text.h>
|
||||
|
||||
#define STB_RECT_PACK_IMPLEMENTATION
|
||||
#include <stb_rect_pack.h>
|
||||
|
||||
#include <core/memory.h>
|
||||
|
||||
#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 <stb_truetype.h>
|
||||
|
||||
constexpr const int RANGE = 1024;
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
TextDrawDescriptor::TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), _text(std::move(text))
|
||||
{}
|
||||
|
||||
void TextDrawDescriptor::init(Font* const font) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
std::vector<Vertex> vertexData;
|
||||
std::vector<uint16_t> indexData;
|
||||
|
||||
float stb_x = 0.0f;
|
||||
float stb_y = 0.0f;
|
||||
|
||||
for(char c : _text)
|
||||
{
|
||||
if(c < 32)
|
||||
continue;
|
||||
|
||||
stbtt_aligned_quad q;
|
||||
stbtt_GetPackedQuad(font->getCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
|
||||
|
||||
std::size_t index = vertexData.size();
|
||||
|
||||
glm::vec4 vertex_color = {
|
||||
static_cast<float>((color & 0x000000FF)) / 255.f,
|
||||
static_cast<float>((color & 0x0000FF00) >> 8) / 255.f,
|
||||
static_cast<float>((color & 0x00FF0000) >> 16) / 255.f,
|
||||
static_cast<float>((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> text_data = std::make_shared<Text>();
|
||||
text_data->init(_text, font, 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<VkDescriptorSet, 2>& sets, Renderer& renderer)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id);
|
||||
TextureAtlas& atlas = const_cast<TextureAtlas&>(draw_data->getFontInUse().getAtlas());
|
||||
draw_data->bind(renderer);
|
||||
if(atlas.getSet() == VK_NULL_HANDLE)
|
||||
atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
|
||||
if(!atlas.hasBeenUpdated())
|
||||
atlas.updateSet(0);
|
||||
sets[1] = const_cast<TextureAtlas&>(atlas).getSet();
|
||||
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<Text> draw_data = TextLibrary::get().getTextData(id);
|
||||
TextureAtlas& atlas = const_cast<TextureAtlas&>(draw_data->getFontInUse().getAtlas());
|
||||
atlas.resetUpdate();
|
||||
}
|
||||
}
|
||||
65
src/renderer/texts/text_descriptor.h
git.filemode.normal_file
65
src/renderer/texts/text_descriptor.h
git.filemode.normal_file
@@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_descriptor.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 00:13:34 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 04:28:58 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_TEXT_DESCRIPTOR__
|
||||
#define __MLX_TEXT_DESCRIPTOR__
|
||||
|
||||
#include <string>
|
||||
#include <mlx_profile.h>
|
||||
#include <volk.h>
|
||||
#include <utils/combine_hash.h>
|
||||
#include <renderer/core/drawable_resource.h>
|
||||
#include <renderer/texts/text_library.h>
|
||||
#include <array>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class TextDrawDescriptor : public DrawableResource
|
||||
{
|
||||
friend class std::hash<TextDrawDescriptor>;
|
||||
|
||||
public:
|
||||
TextID id;
|
||||
uint32_t color;
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public:
|
||||
TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y);
|
||||
|
||||
void init(Font* const font) noexcept;
|
||||
bool operator==(const TextDrawDescriptor& rhs) const { return _text == rhs._text && x == rhs.x && y == rhs.y && color == rhs.color; }
|
||||
void render(std::array<VkDescriptorSet, 2>& sets, Renderer& renderer) override;
|
||||
void resetUpdate() override;
|
||||
|
||||
TextDrawDescriptor() = default;
|
||||
|
||||
private:
|
||||
std::string _text;
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<mlx::TextDrawDescriptor>
|
||||
{
|
||||
std::size_t operator()(const mlx::TextDrawDescriptor& d) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
mlx::hashCombine(hash, d.x, d.y, d.color, d._text);
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
67
src/renderer/texts/text_library.cpp
git.filemode.normal_file
67
src/renderer/texts/text_library.cpp
git.filemode.normal_file
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_library.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 05:19:24 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
#include <renderer/texts/text_library.h>
|
||||
#include <renderer/texts/text.h>
|
||||
#include <core/errors.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <algorithm>
|
||||
#include <core/profiler.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
std::shared_ptr<Text> TextLibrary::getTextData(TextID id)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
|
||||
core::error::report(e_kind::fatal_error, "Text Library : wrong text ID '%d'", id);
|
||||
return _cache[id];
|
||||
}
|
||||
|
||||
TextID TextLibrary::addTextToLibrary(std::shared_ptr<Text> text)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(_cache.begin(), _cache.end(), [&](const std::pair<TextID, std::shared_ptr<Text>>& v)
|
||||
{
|
||||
return v.second->getText() == text->getText() && std::find(_invalid_ids.begin(), _invalid_ids.end(), v.first) == _invalid_ids.end();
|
||||
});
|
||||
if(it != _cache.end())
|
||||
return it->first;
|
||||
_cache[_current_id] = text;
|
||||
_current_id++;
|
||||
return _current_id - 1;
|
||||
}
|
||||
|
||||
void TextLibrary::removeTextFromLibrary(TextID id)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
|
||||
{
|
||||
core::error::report(e_kind::warning, "Text Library : trying to remove a text with an unkown or invalid ID '%d'", id);
|
||||
return;
|
||||
}
|
||||
_cache[id]->destroy();
|
||||
_invalid_ids.push_back(id);
|
||||
}
|
||||
|
||||
void TextLibrary::clearLibrary()
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
for(auto& [id, text] : _cache)
|
||||
{
|
||||
text->destroy();
|
||||
_invalid_ids.push_back(id);
|
||||
}
|
||||
// do not `_cache.clear();` as it releases the texts and may not destroy Vertex and Index buffers that are in use by command buffers
|
||||
}
|
||||
}
|
||||
55
src/renderer/texts/text_library.h
git.filemode.normal_file
55
src/renderer/texts/text_library.h
git.filemode.normal_file
@@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_library.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/10 11:52:30 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 05:08:04 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_TEXT_LIBRARY__
|
||||
#define __MLX_TEXT_LIBRARY__
|
||||
|
||||
#include <renderer/buffers/vk_vbo.h>
|
||||
#include <renderer/buffers/vk_ibo.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <mlx_profile.h>
|
||||
#include <renderer/texts/font.h>
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <utils/singleton.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
using TextID = uint32_t;
|
||||
constexpr TextID nulltext = 0;
|
||||
|
||||
class TextLibrary : public Singleton<TextLibrary>
|
||||
{
|
||||
friend class Singleton<TextLibrary>;
|
||||
|
||||
public:
|
||||
std::shared_ptr<class Text> getTextData(TextID id);
|
||||
TextID addTextToLibrary(std::shared_ptr<Text> text);
|
||||
void removeTextFromLibrary(TextID id);
|
||||
|
||||
void clearLibrary();
|
||||
|
||||
private:
|
||||
TextLibrary() = default;
|
||||
~TextLibrary() = default;
|
||||
|
||||
private:
|
||||
std::unordered_map<TextID, std::shared_ptr<class Text>> _cache;
|
||||
std::vector<TextID> _invalid_ids;
|
||||
TextID _current_id = 1;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
66
src/renderer/texts/text_manager.cpp
git.filemode.normal_file
66
src/renderer/texts/text_manager.cpp
git.filemode.normal_file
@@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_manager.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 04:54:16 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <renderer/texts/text_descriptor.h>
|
||||
#include <renderer/texts/text_library.h>
|
||||
#include <renderer/texts/text.h>
|
||||
#include <renderer/texts/text_manager.h>
|
||||
#include <array>
|
||||
#include <core/profiler.h>
|
||||
#include <fstream>
|
||||
|
||||
#include <utils/dogica_ttf.h>
|
||||
#include <cstdio>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void TextManager::init(Renderer& renderer) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, "default", dogica_ttf, 6.0f).first);
|
||||
}
|
||||
|
||||
void TextManager::loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(filepath.string() == "default") // we're sure it is already loaded
|
||||
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, "default", dogica_ttf, scale).first);
|
||||
else
|
||||
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, filepath, scale).first);
|
||||
}
|
||||
|
||||
std::pair<DrawableResource*, bool> TextManager::registerText(int x, int y, uint32_t color, std::string str)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto res = _text_descriptors.emplace(std::move(str), color, x, y);
|
||||
if(res.second)
|
||||
{
|
||||
const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use);
|
||||
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextDrawDescriptor&>(*res.first)), true);
|
||||
}
|
||||
|
||||
auto text_ptr = TextLibrary::get().getTextData(res.first->id);
|
||||
if(*_font_in_use != text_ptr->getFontInUse())
|
||||
{
|
||||
TextLibrary::get().removeTextFromLibrary(res.first->id);
|
||||
const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use);
|
||||
}
|
||||
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextDrawDescriptor&>(*res.first)), false);
|
||||
}
|
||||
|
||||
void TextManager::destroy() noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_text_descriptors.clear();
|
||||
_font_set.clear();
|
||||
}
|
||||
}
|
||||
48
src/renderer/texts/text_manager.h
git.filemode.normal_file
48
src/renderer/texts/text_manager.h
git.filemode.normal_file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* text_manager.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/11 05:18:42 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_TEXT_MANAGER__
|
||||
#define __MLX_TEXT_MANAGER__
|
||||
|
||||
#include <renderer/renderer.h>
|
||||
#include <renderer/images/texture_atlas.h>
|
||||
#include <string>
|
||||
#include <stb_truetype.h>
|
||||
#include <cstdint>
|
||||
#include <unordered_set>
|
||||
#include <mlx_profile.h>
|
||||
#include <renderer/texts/text_descriptor.h>
|
||||
#include <renderer/texts/text_library.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class TextManager
|
||||
{
|
||||
public:
|
||||
TextManager() = default;
|
||||
|
||||
void init(Renderer& renderer) noexcept;
|
||||
std::pair<DrawableResource*, bool> registerText(int x, int y, uint32_t color, std::string str);
|
||||
inline void clear() { _text_descriptors.clear(); TextLibrary::get().clearLibrary(); }
|
||||
void loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale);
|
||||
void destroy() noexcept;
|
||||
|
||||
~TextManager() = default;
|
||||
|
||||
private:
|
||||
std::unordered_set<TextDrawDescriptor> _text_descriptors;
|
||||
std::unordered_set<Font> _font_set;
|
||||
Font* _font_in_use = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user