@ -1,50 +1,17 @@
@@ -1,50 +1,17 @@
# include "TextComponent.h"
# include "../../text/TextRasterizer.h"
# include <memory>
TextComponent : : TextComponent ( const std : : string & text , const int x , const int y , const int fontSize , const bool bold , const int windowWidth , const int windowHeight ) {
this - > text = text ;
this - > x = x ;
this - > y = y ;
this - > fontSize = fontSize ;
this - > bold = bold ;
const std : : unique_ptr < TextRasterizer > textRasterizer = std : : make_unique < TextRasterizer > ( " DejaVuSerif.ttf " , fontSize , 72 , bold ) ;
unsigned int glyphCount ;
std : : unique_ptr < const Glyph [ ] > glyphs = textRasterizer - > rasterize ( text , x , y , height , glyphCount ) ;
if ( glyphs = = nullptr ) {
return ;
}
for ( int i = 0 ; i < glyphCount ; i + + ) {
const Glyph & glyph = glyphs [ i ] ;
float vx0 = glyph . x0 ;
float vy0 = glyph . y0 ;
float vx1 = glyph . x1 ;
float vy1 = glyph . y1 ;
pointToViewport ( vx0 , vy0 , windowWidth , windowHeight ) ;
pointToViewport ( vx1 , vy1 , windowWidth , windowHeight ) ;
std : : unique_ptr < float [ ] > vertices = std : : make_unique < float [ ] > ( 20 ) ;
vertices [ ( 0 * 5 ) + 0 ] = vx0 ;
vertices [ ( 0 * 5 ) + 1 ] = vy0 ;
vertices [ ( 0 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 0 * 5 ) + 3 ] = glyph . s0 ;
vertices [ ( 0 * 5 ) + 4 ] = glyph . t0 ;
vertices [ ( 1 * 5 ) + 0 ] = vx0 ;
vertices [ ( 1 * 5 ) + 1 ] = vy1 ;
vertices [ ( 1 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 1 * 5 ) + 3 ] = glyph . s0 ;
vertices [ ( 1 * 5 ) + 4 ] = glyph . t1 ;
vertices [ ( 2 * 5 ) + 0 ] = vx1 ;
vertices [ ( 2 * 5 ) + 1 ] = vy1 ;
vertices [ ( 2 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 2 * 5 ) + 3 ] = glyph . s1 ;
vertices [ ( 2 * 5 ) + 4 ] = glyph . t1 ;
vertices [ ( 3 * 5 ) + 0 ] = vx1 ;
vertices [ ( 3 * 5 ) + 1 ] = vy0 ;
vertices [ ( 3 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 3 * 5 ) + 3 ] = glyph . s1 ;
vertices [ ( 3 * 5 ) + 4 ] = glyph . t0 ;
glyphVertices . push_back ( std : : move ( vertices ) ) ;
rasterize ( text , x , y , fontSize , bold , windowWidth , windowHeight ) ;
for ( int i = 0 ; i < glyphVertices . size ( ) ; i + + ) {
const Glyph & glyph = glyphs [ i ] ;
const std : : unique_ptr < float [ ] > & glyphVertice = glyphVertices [ i ] ;
vertexArrayObjects . push_back ( 0 ) ;
vertexBufferObjects . push_back ( 0 ) ;
elementBufferObjects . push_back ( 0 ) ;
@ -55,7 +22,7 @@ TextComponent::TextComponent(const std::string &text, const int x, const int y,
@@ -55,7 +22,7 @@ TextComponent::TextComponent(const std::string &text, const int x, const int y,
glBindVertexArray ( vertexArrayObjects . back ( ) ) ;
glBindBuffer ( GL_ARRAY_BUFFER , vertexBufferObjects . back ( ) ) ;
glBufferData ( GL_ARRAY_BUFFER , ( ( 3 + 2 ) * 4 ) * sizeof ( float ) , glyphVertices . back ( ) . get ( ) , GL_STATIC_DRAW ) ;
glBufferData ( GL_ARRAY_BUFFER , ( ( 3 + 2 ) * 4 ) * sizeof ( float ) , glyphVertice . get ( ) , GL_STATIC_DRAW ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , elementBufferObjects . back ( ) ) ;
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , sizeof ( indices ) , indices , GL_STATIC_DRAW ) ;
@ -83,6 +50,50 @@ TextComponent::~TextComponent() {
@@ -83,6 +50,50 @@ TextComponent::~TextComponent() {
glDeleteTextures ( 1 , & textures [ i ] ) ;
}
}
# include <iostream>
void TextComponent : : rasterize ( const std : : string & text , const int x , const int y , const int fontSize , const bool bold , const int windowWidth , const int windowHeight ) {
const std : : unique_ptr < TextRasterizer > textRasterizer = std : : make_unique < TextRasterizer > ( " DejaVuSerif.ttf " , fontSize , 72 , bold ) ;
unsigned int glyphCount ;
glyphs = textRasterizer - > rasterize ( text , x , y , windowWidth , windowHeight , height , glyphCount ) ;
if ( glyphs = = nullptr ) {
return ;
}
glyphVertices . clear ( ) ;
for ( int i = 0 ; i < glyphCount ; i + + ) {
const Glyph & glyph = glyphs [ i ] ;
float vx0 = glyph . x0 ;
float vy0 = glyph . y0 ;
float vx1 = glyph . x1 ;
float vy1 = glyph . y1 ;
pointToViewport ( vx0 , vy0 , windowWidth , windowHeight ) ;
pointToViewport ( vx1 , vy1 , windowWidth , windowHeight ) ;
std : : unique_ptr < float [ ] > vertices = std : : make_unique < float [ ] > ( 20 ) ;
vertices [ ( 0 * 5 ) + 0 ] = vx0 ;
vertices [ ( 0 * 5 ) + 1 ] = vy0 ;
vertices [ ( 0 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 0 * 5 ) + 3 ] = glyph . s0 ;
vertices [ ( 0 * 5 ) + 4 ] = glyph . t0 ;
vertices [ ( 1 * 5 ) + 0 ] = vx0 ;
vertices [ ( 1 * 5 ) + 1 ] = vy1 ;
vertices [ ( 1 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 1 * 5 ) + 3 ] = glyph . s0 ;
vertices [ ( 1 * 5 ) + 4 ] = glyph . t1 ;
vertices [ ( 2 * 5 ) + 0 ] = vx1 ;
vertices [ ( 2 * 5 ) + 1 ] = vy1 ;
vertices [ ( 2 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 2 * 5 ) + 3 ] = glyph . s1 ;
vertices [ ( 2 * 5 ) + 4 ] = glyph . t1 ;
vertices [ ( 3 * 5 ) + 0 ] = vx1 ;
vertices [ ( 3 * 5 ) + 1 ] = vy0 ;
vertices [ ( 3 * 5 ) + 2 ] = 0.0f ;
vertices [ ( 3 * 5 ) + 3 ] = glyph . s1 ;
vertices [ ( 3 * 5 ) + 4 ] = glyph . t0 ;
glyphVertices . push_back ( std : : move ( vertices ) ) ;
}
}
void TextComponent : : render ( ) {
if ( verticesDirty ) {
@ -99,17 +110,8 @@ void TextComponent::render() {
@@ -99,17 +110,8 @@ void TextComponent::render() {
}
}
void TextComponent : : resize ( const float sx , const float sy ) {
for ( int i = 0 ; i < glyphVertices . size ( ) ; i + + ) {
glyphVertices [ i ] [ ( 0 * 5 ) + 0 ] = ( ( glyphVertices [ i ] [ ( 0 * 5 ) + 0 ] + 1 ) / sx ) - 1 ;
glyphVertices [ i ] [ ( 0 * 5 ) + 1 ] = ( ( glyphVertices [ i ] [ ( 0 * 5 ) + 1 ] + 1 ) / sy ) - 1 ;
glyphVertices [ i ] [ ( 1 * 5 ) + 0 ] = ( ( glyphVertices [ i ] [ ( 1 * 5 ) + 0 ] + 1 ) / sx ) - 1 ;
glyphVertices [ i ] [ ( 1 * 5 ) + 1 ] = ( ( glyphVertices [ i ] [ ( 1 * 5 ) + 1 ] + 1 ) / sy ) - 1 ;
glyphVertices [ i ] [ ( 2 * 5 ) + 0 ] = ( ( glyphVertices [ i ] [ ( 2 * 5 ) + 0 ] + 1 ) / sx ) - 1 ;
glyphVertices [ i ] [ ( 2 * 5 ) + 1 ] = ( ( glyphVertices [ i ] [ ( 2 * 5 ) + 1 ] + 1 ) / sy ) - 1 ;
glyphVertices [ i ] [ ( 3 * 5 ) + 0 ] = ( ( glyphVertices [ i ] [ ( 3 * 5 ) + 0 ] + 1 ) / sx ) - 1 ;
glyphVertices [ i ] [ ( 3 * 5 ) + 1 ] = ( ( glyphVertices [ i ] [ ( 3 * 5 ) + 1 ] + 1 ) / sy ) - 1 ;
}
void TextComponent : : resize ( const int x , const int y , const int windowWidth , const int windowHeight ) {
rasterize ( text , x , y , fontSize , bold , windowWidth , windowHeight ) ;
verticesDirty = true ;
}