Guest User

Untitled

a guest
Dec 26th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. struct Character
  2. {
  3. unsigned int TextureID;
  4. glm::ivec2 Size;
  5. glm::ivec2 Bearing;
  6. unsigned int Advance;
  7. };
  8.  
  9. struct Text
  10. {
  11. std::string text;
  12. glm::vec2 position;
  13. glm::vec2 scale;
  14. float rotation;
  15. glm::vec4 color;
  16. std::string font;
  17. };
  18.  
  19. inline std::map<std::string, std::map<char, Character>> fonts;
  20.  
  21. void LoadFont(const std::string& fontPath)
  22. {
  23. if (fonts.find(fontPath) != fonts.end())
  24. {
  25. // Font already loaded
  26. return;
  27. }
  28.  
  29. FT_Face face;
  30. if (FT_New_Face(ft, fontPath.c_str(), 0, &face))
  31. {
  32. std::cerr << "ERROR::FREETYPE: Failed to load font" << std::endl;
  33. return;
  34. }
  35. FT_Set_Pixel_Sizes(face, 0, 48);
  36.  
  37. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  38.  
  39. std::map<char, Character> characters;
  40. for (GLubyte c = 0; c < 128; c++)
  41. {
  42. if (FT_Load_Char(face, c, FT_LOAD_RENDER))
  43. {
  44. std::cerr << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
  45. continue;
  46. }
  47. GLuint texture;
  48. glGenTextures(1, &texture);
  49. glBindTexture(GL_TEXTURE_2D, texture);
  50. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows,
  51. 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
  52.  
  53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  54. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  55. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  56. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  57.  
  58. Character character = {texture,
  59. glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
  60. glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
  61. static_cast<GLuint>(face->glyph->advance.x)};
  62.  
  63. characters.insert(std::pair<char, Character>(c, character));
  64. }
  65. fonts.insert(std::pair<std::string, std::map<char, Character>>(fontPath, characters));
  66.  
  67. FT_Done_Face(face);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment