Added Font to ResourceManager
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#ifndef Font_h__
|
||||
#define Font_h__
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include "../OpenGL.h"
|
||||
#include "../GLM.h"
|
||||
#include "../Core/ResourceManager.h"
|
||||
|
||||
class Font : public Resource
|
||||
{
|
||||
friend class ResourceManager;
|
||||
private:
|
||||
Font(std::string path);
|
||||
|
||||
public:
|
||||
struct Character {
|
||||
GLuint TextureID; // ID handle of the glyph texture
|
||||
glm::ivec2 Size; // Size of glyph
|
||||
glm::ivec2 Bearing; // Offset from baseline to left/top of glyph
|
||||
GLuint Advance; // Offset to advance to next glyph
|
||||
};
|
||||
~Font();
|
||||
|
||||
std::map<GLchar, Character> m_Characters;
|
||||
};
|
||||
#endif
|
||||
@@ -4,9 +4,11 @@
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include "OpenGL.h"
|
||||
#include "GLM.h"
|
||||
#include "../OpenGL.h"
|
||||
#include "../GLM.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "Font.h"
|
||||
#include "../Core/ResourceManager.h"
|
||||
|
||||
class TextRenderer
|
||||
{
|
||||
@@ -17,17 +19,7 @@ public:
|
||||
void Draw(glm::mat4 projection, glm::mat4 view);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
struct Character {
|
||||
GLuint TextureID; // ID handle of the glyph texture
|
||||
glm::ivec2 Size; // Size of glyph
|
||||
glm::ivec2 Bearing; // Offset from baseline to left/top of glyph
|
||||
GLuint Advance; // Offset to advance to next glyph
|
||||
};
|
||||
|
||||
std::map<GLchar, Character> Characters;
|
||||
|
||||
Font* font;
|
||||
|
||||
GLuint VAO, VBO;
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ public:
|
||||
void Bind(GLenum textureUnit = GL_TEXTURE0);
|
||||
|
||||
GLuint m_Texture = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "Rendering/Font.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "Rendering/Font.h"
|
||||
|
||||
|
||||
Font::Font(std::string path)
|
||||
{
|
||||
FT_Library library;
|
||||
FT_Face face;
|
||||
|
||||
if (FT_Init_FreeType(&library)) {
|
||||
LOG_ERROR("FreeType error: init failed");
|
||||
}
|
||||
|
||||
if (FT_New_Face(library, path.c_str(), 0, &face)) {
|
||||
LOG_ERROR("FreeType error: loading font");
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(face, 0, 48);
|
||||
|
||||
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER)) {
|
||||
LOG_ERROR("FreeType error: loading char");
|
||||
}
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
for (GLubyte c = 0; c < 128; c++) {
|
||||
//Load character glyph
|
||||
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
||||
continue;
|
||||
}
|
||||
printf("Char: %c\n", c);
|
||||
//Generate texture
|
||||
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
|
||||
);
|
||||
// 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
|
||||
Character character = {
|
||||
texture,
|
||||
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
|
||||
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
|
||||
face->glyph->advance.x
|
||||
};
|
||||
|
||||
m_Characters.insert(std::pair<GLchar, Character>(c, character));
|
||||
}
|
||||
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(library);
|
||||
GLERROR("Font Load");
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
for (auto c : m_Characters) {
|
||||
glDeleteTextures(1, &c.second.TextureID);
|
||||
}
|
||||
}
|
||||
@@ -7,67 +7,7 @@ TextRenderer::TextRenderer()
|
||||
|
||||
void TextRenderer::Initialize()
|
||||
{
|
||||
FT_Library library;
|
||||
FT_Face face;
|
||||
|
||||
if (FT_Init_FreeType(&library)) {
|
||||
LOG_ERROR("FreeType error: init failed");
|
||||
}
|
||||
|
||||
if (FT_New_Face(library, "fonts/arial.ttf", 0, &face)) {
|
||||
LOG_ERROR("FreeType error: loading font");
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(face, 0, 48);
|
||||
|
||||
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER)) {
|
||||
LOG_ERROR("FreeType error: loading char");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
for (GLubyte c = 0; c < 128; c++) {
|
||||
//Load character glyph
|
||||
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
||||
continue;
|
||||
}
|
||||
printf("Char: %c\n", c);
|
||||
//Generate texture
|
||||
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
|
||||
);
|
||||
// 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
|
||||
Character character = {
|
||||
texture,
|
||||
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
|
||||
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
|
||||
face->glyph->advance.x
|
||||
};
|
||||
Characters.insert(std::pair<GLchar, Character>(c, character));
|
||||
}
|
||||
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(library);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -115,7 +55,8 @@ void TextRenderer::Draw(glm::mat4 projection, glm::mat4 view)
|
||||
|
||||
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color, glm::mat4 projection, glm::mat4 view)
|
||||
{
|
||||
|
||||
font = ResourceManager::Load<Font>("fonts/arial.ttf");
|
||||
|
||||
glm::mat4 modelMatrix = glm::translate(glm::mat4(), glm::vec3(0.5f, 0.3f, 0.f)) * glm::toMat4(glm::quat()) * glm::scale(glm::vec3(0.01f));
|
||||
|
||||
// Activate corresponding render state
|
||||
@@ -134,7 +75,7 @@ void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat sc
|
||||
// Iterate through all characters
|
||||
std::string::const_iterator c;
|
||||
for (c = text.begin(); c != text.end(); c++) {
|
||||
Character ch = Characters[*c];
|
||||
Font::Character ch = font->m_Characters[*c];
|
||||
|
||||
GLfloat xpos = x + ch.Bearing.x * scale;
|
||||
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
|
||||
|
||||
@@ -6,6 +6,7 @@ Game::Game(int argc, char* argv[])
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
ResourceManager::RegisterType<Font>("FontFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
Reference in New Issue
Block a user