diff options
Diffstat (limited to 'engine-ocean/Graphics/font.cpp')
-rw-r--r-- | engine-ocean/Graphics/font.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/engine-ocean/Graphics/font.cpp b/engine-ocean/Graphics/font.cpp new file mode 100644 index 0000000..b5bc7a1 --- /dev/null +++ b/engine-ocean/Graphics/font.cpp @@ -0,0 +1,74 @@ +#include "font.h" +#include <iostream> + +Font::Font(const std::string filepath){ + FT_Library ft; + if (FT_Init_FreeType(&ft)) + { + std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl; + } + + FT_Face face; + if (FT_New_Face(ft, filepath.data(), 0, &face)) + { + std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl; + } + + FT_Set_Pixel_Sizes(face, 0, 48); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction + + for (unsigned char c = 0; c < 128; c++) + { + createGlyphTexture(c, face); + } + + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // Back to default value + FT_Done_Face(face); + FT_Done_FreeType(ft); +} + +Font::~Font(){ + +} + +void Font::createGlyphTexture(unsigned char c, FT_Face &face){ + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) + { + std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl; + return; + } + // generate texture + unsigned int 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 + ); + // set texture options + 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); + // now store character for later use + unsigned int advance = static_cast<unsigned int>(face->glyph->advance.x); + Character character = { + texture, + glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), + glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), + advance + }; + m_characters.insert(std::pair<char, Character>(c, character)); +} + +Character& Font::getCharacter(char c){ + return m_characters[c]; +} |