mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2024-11-04 16:31:15 -05:00
Simplify GLFont drawString usage
This commit is contained in:
parent
da8d178e34
commit
7f9a871598
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user