From 808bb4cbfa443748082c8028cb86df1d7e0d8032 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 17 Mar 2021 09:21:43 +0200 Subject: [PATCH] horizontal text --- text.cpp | 15 ++++++++++----- text.frag | 2 +- text.h | 6 ++++-- text.vert | 10 +++++----- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/text.cpp b/text.cpp index d755e41..e945ca4 100755 --- a/text.cpp +++ b/text.cpp @@ -67,7 +67,7 @@ namespace vb01{ shader->setVec2(Vector2(width, height), "screen"); shader->setVec3(node->getPosition(), "pos"); - float advanceOffset = 0; + Vector2 advanceOffset = Vector2::VEC_ZERO; for(int i = 0; i < entry.length(); i++){ Glyph *glyphPtr = getGlyph(entry[i]); if(!glyphPtr) @@ -75,13 +75,18 @@ namespace vb01{ Glyph glyph = *glyphPtr; prepareGlyphs(glyph, advanceOffset); - advanceOffset += glyph.size.x + glyph.bearing.x; + Vector2 size = glyph.size, bearing = glyph.bearing; + if(horizontal) + advanceOffset.x += (size.x + bearing.x); + else + advanceOffset.y += size.y; } } - void Text::prepareGlyphs(Glyph glyph, float advanceOffset){ - Vector3 origin = node->getPosition(); - origin.x += advanceOffset * scale; + void Text::prepareGlyphs(Glyph glyph, Vector2 advanceOffset){ + Vector3 nodePos = node->getPosition(); + Vector2 origin = Vector2(nodePos.x, nodePos.y); + origin = origin + (advanceOffset * scale); Vector2 size = glyph.size * scale, bearing = glyph.bearing * scale; float data[] = { diff --git a/text.frag b/text.frag index 7266246..30e9533 100755 --- a/text.frag +++ b/text.frag @@ -8,5 +8,5 @@ uniform sampler2D tex; uniform vec4 color; void main(){ - FragColor=vec4(color.xyz,texture(tex,texCoords).r); + FragColor = vec4(color.xyz, texture(tex, texCoords).r); } diff --git a/text.h b/text.h index 1f19d31..8f7216b 100755 --- a/text.h +++ b/text.h @@ -24,16 +24,17 @@ namespace vb01{ ~Text(); void update(); void setText(std::wstring); - float getCharWidth(char); inline Node* getNode(){return node;} inline void setNode(Node *node){this->node = node;} inline void setScale(float s){this->scale = s;} inline void setColor(Vector4 c){this->color = c;} inline int getLength(){return entry.length();} inline std::wstring getText(){return entry;} + inline bool isHorizontal(){return horizontal;} + inline void setHorizontal(bool h){this->horizontal = h;} private: void initFont(std::string); - void prepareGlyphs(Glyph, float); + void prepareGlyphs(Glyph, Vector2); void renderGlyphs(Glyph, float[], u32); Glyph* getGlyph(u16); @@ -43,6 +44,7 @@ namespace vb01{ Shader *shader = nullptr; unsigned int VAO, VBO; float scale = 1; + bool horizontal = true; Vector4 color = Vector4::VEC_IJKL; }; } diff --git a/text.vert b/text.vert index 3f665b5..35e63ad 100755 --- a/text.vert +++ b/text.vert @@ -8,9 +8,9 @@ uniform vec2 screen; uniform vec3 pos; void main(){ - float x,y; - x=(aVert.x-screen.x*.5)/(screen.x*.5); - y=(screen.y*.5-(aVert.y))/(screen.y*.5); - gl_Position=vec4(x,y,pos.z,1); - texCoords=aVert.zw; + float x, y; + x = (aVert.x - screen.x * .5) / (screen.x * .5); + y = (screen.y * .5 - (aVert.y)) / (screen.y * .5); + gl_Position = vec4(x, y, pos.z, 1); + texCoords = aVert.zw; }