Simplify GLFont drawString usage

This commit is contained in:
Charles J. Cliffe 2014-12-08 20:39:38 -05:00
parent da8d178e34
commit 7f9a871598
3 changed files with 56 additions and 27 deletions

View File

@ -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);

View File

@ -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);

View File

@ -33,16 +33,7 @@ void SpectrumContext::Draw(std::vector<float> &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();
}