-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
328 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* text_library.cpp :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */ | ||
/* Updated: 2023/12/12 22:47:33 by kbz_8 ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <renderer/text_library.h> | ||
#include <core/errors.h> | ||
#include <renderer/renderer.h> | ||
#include <algorithm> | ||
|
||
namespace mlx | ||
{ | ||
void TextData::init(std::string text, Font const* font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data) | ||
{ | ||
_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 TextData::bind(Renderer& renderer) noexcept | ||
{ | ||
_vbo[renderer.getActiveImageIndex()].bind(renderer); | ||
_ibo.bind(renderer); | ||
} | ||
|
||
void TextData::destroy() noexcept | ||
{ | ||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) | ||
_vbo[i].destroy(); | ||
_ibo.destroy(); | ||
} | ||
|
||
std::shared_ptr<TextData> TextLibrary::getTextData(TextID id) | ||
{ | ||
if(!_cache.count(id)) | ||
core::error::report(e_kind::fatal_error, "Text Library : wrong text ID '%d'", id); | ||
return _cache[id]; | ||
} | ||
|
||
TextID TextLibrary::addTextToLibrary(std::shared_ptr<TextData> text) | ||
{ | ||
auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair<TextID, std::shared_ptr<TextData>>& v) | ||
{ | ||
return v.second->getText() == text->getText(); | ||
}); | ||
if(it != _cache.end()) | ||
return it->first; | ||
_cache[_current_id] = text; | ||
_current_id++; | ||
return _current_id - 1; | ||
} | ||
|
||
void TextLibrary::removeTextFromLibrary(TextID id) | ||
{ | ||
if(_cache.count(id)) | ||
{ | ||
_cache[id]->destroy(); | ||
_cache.erase(id); | ||
} | ||
} | ||
|
||
void TextLibrary::clearLibrary() | ||
{ | ||
for(auto [id, text] : _cache) | ||
text->destroy(); | ||
_cache.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* font.cpp :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */ | ||
/* Updated: 2023/12/12 23:57:53 by kbz_8 ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <renderer/font.h> | ||
#include <renderer/renderer.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()) | ||
{ | ||
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) | ||
{ | ||
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, 6.0, 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() | ||
{ | ||
_atlas.destroy(); | ||
} | ||
} |
Oops, something went wrong.