Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Character
- {
- unsigned int TextureID;
- glm::ivec2 Size;
- glm::ivec2 Bearing;
- unsigned int Advance;
- };
- struct Text
- {
- std::string text;
- glm::vec2 position;
- glm::vec2 scale;
- float rotation;
- glm::vec4 color;
- std::string font;
- };
- inline std::map<std::string, std::map<char, Character>> fonts;
- void LoadFont(const std::string& fontPath)
- {
- if (fonts.find(fontPath) != fonts.end())
- {
- // Font already loaded
- return;
- }
- FT_Face face;
- if (FT_New_Face(ft, fontPath.c_str(), 0, &face))
- {
- std::cerr << "ERROR::FREETYPE: Failed to load font" << std::endl;
- return;
- }
- FT_Set_Pixel_Sizes(face, 0, 48);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- std::map<char, Character> characters;
- for (GLubyte c = 0; c < 128; c++)
- {
- if (FT_Load_Char(face, c, FT_LOAD_RENDER))
- {
- std::cerr << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
- continue;
- }
- GLuint texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows,
- 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- Character character = {texture,
- glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
- glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
- static_cast<GLuint>(face->glyph->advance.x)};
- characters.insert(std::pair<char, Character>(c, character));
- }
- fonts.insert(std::pair<std::string, std::map<char, Character>>(fontPath, characters));
- FT_Done_Face(face);
- }
Advertisement
Add Comment
Please, Sign In to add comment