Advertisement
Guest User

CFont.h/CFont.cpp

a guest
Oct 16th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1.  
  2. //================================================CFONT.H=============================================
  3. #ifndef __C_FONT_H__
  4. #define __C_FONT_H__
  5.  
  6. #include <ft2build.h>
  7. #include FT_FREETYPE_H
  8.  
  9. class ShaderProgram;
  10.  
  11. class CFont
  12. {
  13. private:
  14.     static FT_Library library;
  15.     static ShaderProgram* textShader;
  16.  
  17. private:
  18.     unsigned int texID; //The texture id that stores the texture this font uses
  19.     unsigned int vboID; //The vbo id that holds the combined vertex and texture coordinates
  20.     FT_Face face; //The face used by this font
  21.     FT_GlyphSlot slot; //The shortcut to the active glyph in the face
  22.  
  23. public:
  24.     CFont(const char* file, unsigned int size);
  25.     ~CFont();
  26.  
  27.     void SetSize(unsigned int);
  28.     void RenderText(const char* text, float x, float y, float color[4]);
  29. };
  30.  
  31. #endif
  32.  
  33. //=============================================CFONT.CPP==========================================
  34. #include "cfont.h"
  35. #include "shader_program.h"
  36. #include "globals.h"
  37.  
  38. #include <GL\glew.h>
  39.  
  40. #include <stdexcept>
  41.  
  42. FT_Library CFont::library = nullptr;
  43. ShaderProgram* CFont::textShader = nullptr;
  44.  
  45. CFont::CFont(const char* file, unsigned int size)
  46. {
  47.     //Static init
  48.     if (!library)
  49.     {
  50.         int err = FT_Init_FreeType(&library);
  51.         if (err)
  52.             throw std::runtime_error("Failed to load FreeType font library.");
  53.  
  54.         textShader = new ShaderProgram("data\\shaders\\TextRenderer.vert", "data\\shaders\\TextRenderer.frag");
  55.     }
  56.  
  57.     //Load face and set size
  58.     int err;
  59.     err = FT_New_Face(library, file, 0, &face);
  60.     if (err)
  61.         throw std::runtime_error("Could not load font " + *file);
  62.     err = FT_Set_Pixel_Sizes(face, 0, size);
  63.     if (err)
  64.         throw std::runtime_error("Could not change fint size.");
  65.  
  66.     slot = face->glyph;
  67.  
  68.     //Load graphics stuff
  69.     glGenTextures(1, &texID);
  70.     glBindTexture(GL_TEXTURE_2D, texID);
  71.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  72.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  73.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  74.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  75.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  76.     glBindTexture(GL_TEXTURE_2D, 0);
  77.     glGenBuffers(1, &vboID);
  78. }
  79.  
  80. CFont::~CFont()
  81. {
  82.  
  83. }
  84.  
  85. void CFont::SetSize(unsigned int size)
  86. {
  87.     int err = FT_Set_Pixel_Sizes(face, 0, size);
  88.     if (err)
  89.         throw std::runtime_error("Could not set font size.");
  90. }
  91.  
  92. void CFont::RenderText(const char* text, float x, float y, float color[4])
  93. {
  94.     float penX = x, penY = y;
  95.  
  96.     ShaderProgram::SetActive(textShader);
  97.     glActiveTexture(GL_TEXTURE0);
  98.     glBindTexture(GL_TEXTURE_2D, texID);
  99.     textShader->Uniform1i("glyphTex", 0);
  100.     textShader->Uniform4f("color", color[0], color[1], color[2], color[3]);
  101.     glBindBuffer(GL_ARRAY_BUFFER, vboID);
  102.     glEnableVertexAttribArray(glGetAttribLocation(textShader->GetProgramID(), "coord"));
  103.     glVertexAttribPointer(glGetAttribLocation(textShader->GetProgramID(), "coord"), 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
  104.  
  105.     const char* p;
  106.     for (p = text; *p; p++)
  107.     {
  108.         int err = FT_Load_Char(face, *p, FT_LOAD_RENDER);
  109.         if (err)
  110.             continue;
  111.  
  112.         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, slot->bitmap.width, slot->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, slot->bitmap.buffer);
  113.  
  114.         float x2 = penX + slot->bitmap_left * (2.0f / g_screenWidth);
  115.         float y2 = -penY - slot->bitmap_top * (2.0f / g_screenHeight);
  116.         float w = slot->bitmap.width * (2.0f / g_screenWidth);
  117.         float h = slot->bitmap.rows * (2.0f / g_screenHeight);
  118.  
  119.         GLfloat data[4][4] =
  120.         {
  121.             {x2, -y2, 0, 0},
  122.             {x2 + w, -y2, 1, 0},
  123.             {x2, -y2 - h, 0, 1},
  124.             {x2 + w, -y2 - h, 1, 1},
  125.         };
  126.  
  127.         glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_DYNAMIC_DRAW);
  128.         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  129.  
  130.         penX += (slot->advance.x >> 6) * (2.0f / g_screenWidth);
  131.         penY += (slot->advance.y >> 6) * (2.0f / g_screenHeight);
  132.     }
  133.  
  134.     glDisableVertexAttribArray(glGetAttribLocation(textShader->GetProgramID(), "coord"));
  135.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  136.     glBindTexture(GL_TEXTURE_2D, 0);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement