6#include <brenta/asset.hpp>
7#include <brenta/logger.hpp>
8#include <brenta/font.hpp>
9#include <brenta/renderer/opengl/shader.hpp>
10#include <brenta/renderer/opengl/texture.hpp>
15#include "renderer/shaders/c/text_fs.c"
16#include "renderer/shaders/c/text_vs.c"
18using namespace brenta;
20Font::Font(
const std::filesystem::path &path,
int size)
24 this->vbo.init(Buffer::Target::Array);
27 if (FT_Init_FreeType(&ft))
29 ERROR(
"Font: could not init FreeType library");
33 auto text_shader = AssetManager::get<Shader>(
"TextShader");
37 AssetManager::new_asset<Shader>(
"TextShader",
39 .
object({ Shader::Type::Vertex, text_vs })
40 .
object({ Shader::Type::Fragment, text_fs }));
42 if (!text_shader)
return;
44 this->shader = text_shader;
50 ERROR(
"Font: could not find font at path {}", path.string());
55 if (FT_New_Face(ft, path.string().c_str(), 0, &face))
57 ERROR(
"Font: could not load font at path {}", path.string());
63 FT_Set_Pixel_Sizes(face, 0, size);
66 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69 for (
unsigned char c = 0; c < 128; c++)
72 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
74 ERROR(
"Font: could not load glyph for font at path {}",
80 glGenTextures(1, &texture);
81 Texture::bind_id(Texture::Target::Texture2D, texture);
82 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
83 face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
84 face->glyph->bitmap.buffer);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93 glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
94 glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
95 static_cast<unsigned int>(face->glyph->advance.x)};
96 characters.insert(std::pair<char, Font::Character>(c, character));
98 Texture::bind_id(Texture::Target::Texture2D, 0);
102 FT_Done_FreeType(ft);
107 glBufferData(GL_ARRAY_BUFFER,
sizeof(
float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
108 glEnableVertexAttribArray(0);
109 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 *
sizeof(
float), 0);
113 EVENT(Logger::Event::Lifetime,
"Font: initialized");
118 this->shader =
nullptr;
122 EVENT(Logger::Event::Lifetime,
"Font: destoyed");
125Font::Builder& Font::Builder::path(
const std::filesystem::path& path)
128 this->watch_paths.push_back(path);
138Font::Builder& Font::Builder::watch(
const std::filesystem::path& path)
140 this->watch_paths.push_back(path);
144Font Font::Builder::build()
146 return Font(this->_path, this->_size);
149tenno::vector<std::filesystem::path> Font::Builder::get_watch_paths()
const
151 return this->watch_paths;