From 7f9a8715989ad2f956715f2a396134e90e223395 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 8 Dec 2014 20:39:38 -0500 Subject: [PATCH] Simplify GLFont drawString usage --- src/util/GLFont.cpp | 62 +++++++++++++++++++++++++--------- src/util/GLFont.h | 10 +++++- src/visual/SpectrumContext.cpp | 11 +----- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/util/GLFont.cpp b/src/util/GLFont.cpp index 103b565..06d50d5 100644 --- a/src/util/GLFont.cpp +++ b/src/util/GLFont.cpp @@ -342,7 +342,7 @@ void GLFont::loadFont(std::string fontFile) { float GLFont::getStringWidth(std::string str, float size, float viewAspect) { - float scalex = size/viewAspect; + float scalex = size / viewAspect; float width = 0; @@ -355,14 +355,14 @@ float GLFont::getStringWidth(std::string str, float size, float viewAspect) { GLFontChar *fchar = characters[charId]; - float ofsx = (float)fchar->getXOffset()/(float)imageWidth; - float advx = (float)fchar->getXAdvance()/(float)imageWidth; + float ofsx = (float) fchar->getXOffset() / (float) imageWidth; + float advx = (float) fchar->getXAdvance() / (float) imageWidth; if (charId == 32) { advx = characters['_']->getAspect(); - } + } - width += fchar->getAspect()+advx-ofsx; + width += fchar->getAspect() + advx - ofsx; } width *= scalex; @@ -370,19 +370,51 @@ float GLFont::getStringWidth(std::string str, float size, float viewAspect) { return width; } +void GLFont::drawString(std::string str, float xpos, float ypos, int pxHeight, Align hAlign, Align vAlign) { -void GLFont::drawString(std::string str, float xpos, float ypos, float size, float viewAspect) { + GLint vp[4]; + + pxHeight *= 2; + + glGetIntegerv( GL_VIEWPORT, vp); + + float size = (float) pxHeight / (float) vp[3]; + float viewAspect = (float) vp[2] / (float) vp[3]; + float msgWidth = getStringWidth(str, size, viewAspect); glPushMatrix(); glTranslatef(xpos, ypos, 0.0f); + + switch (hAlign) { + case GLFONT_ALIGN_TOP: + glTranslatef(0.0, -size, 0.0); + break; + case GLFONT_ALIGN_CENTER: + glTranslatef(0.0, -size/2.0, 0.0); + break; + default: + break; + } + + switch (vAlign) { + case GLFONT_ALIGN_RIGHT: + glTranslatef(-msgWidth, 0.0, 0.0); + break; + case GLFONT_ALIGN_CENTER: + glTranslatef(-msgWidth / 2.0, 0.0, 0.0); + break; + default: + break; + } + glPushMatrix(); - glScalef(size/viewAspect, size, 1.0f); + glScalef(size / viewAspect, size, 1.0f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId); glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); @@ -398,18 +430,16 @@ void GLFont::drawString(std::string str, float xpos, float ypos, float size, flo GLFontChar *fchar = characters[charId]; - - float ofsx = (float)fchar->getXOffset()/(float)imageWidth; - float advx = (float)fchar->getXAdvance()/(float)imageWidth; - + float ofsx = (float) fchar->getXOffset() / (float) imageWidth; + float advx = (float) fchar->getXAdvance() / (float) imageWidth; if (charId == 32) { advx = characters['_']->getAspect(); - } + } - glTranslatef(-ofsx,0.0,0.0); - glDrawArrays(GL_QUADS, fchar->getIndex()/2, 4); - glTranslatef(fchar->getAspect()+advx,0.0,0.0); + glTranslatef(-ofsx, 0.0, 0.0); + glDrawArrays(GL_QUADS, fchar->getIndex() / 2, 4); + glTranslatef(fchar->getAspect() + advx, 0.0, 0.0); } glDisableClientState(GL_VERTEX_ARRAY); diff --git a/src/util/GLFont.h b/src/util/GLFont.h index e0a8f6f..d59f4df 100644 --- a/src/util/GLFont.h +++ b/src/util/GLFont.h @@ -52,12 +52,20 @@ private: class GLFont { public: + enum Align { + GLFONT_ALIGN_LEFT, + GLFONT_ALIGN_RIGHT, + GLFONT_ALIGN_CENTER, + GLFONT_ALIGN_TOP, + GLFONT_ALIGN_BOTTOM + }; + GLFont(); ~GLFont(); void loadFont(std::string fontFile); float getStringWidth(std::string str, float size, float viewAspect); - void drawString(std::string str, float xpos, float ypos, float height, float viewAspect); + void drawString(std::string str, float xpos, float ypos, int pxHeight, Align hAlign = GLFONT_ALIGN_LEFT, Align vAlign = GLFONT_ALIGN_TOP); private: std::string nextParam(std::istringstream &str); diff --git a/src/visual/SpectrumContext.cpp b/src/visual/SpectrumContext.cpp index 6196e35..91eecde 100644 --- a/src/visual/SpectrumContext.cpp +++ b/src/visual/SpectrumContext.cpp @@ -33,16 +33,7 @@ void SpectrumContext::Draw(std::vector &points) { glPopMatrix(); } - GLint vp[4]; - - glGetIntegerv( GL_VIEWPORT, vp); - - std::string msgStr("Welcome to CubicSDR -- This is a test string. 01234567890!@#$%^&*()_[]"); - float charHeight = 62.0; - float viewAspect = (float)vp[2]/(float)vp[3]; - float msgWidth = getFont()->getStringWidth(msgStr,charHeight/(float)vp[3],viewAspect); - - getFont()->drawString(msgStr,0.0-(msgWidth/2.0),0.0,charHeight/(float)vp[3],viewAspect); + getFont()->drawString("Welcome to CubicSDR -- This is a test string. 01234567890!@#$%^&*()_[]",0.0,0.0,31,GLFont::GLFONT_ALIGN_CENTER,GLFont::GLFONT_ALIGN_CENTER); CheckGLError(); }