Jump to content
  • Advertisement
Sign in to follow this  
fooman_69

OpenGL Font Width Issues

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying my best to code a 1/2 decent Pong remake using OpenGL. I have an engine that is working pretty well, and I have a class for my Bitmap fonts. I have until now simply put the fonts on screen using passed variables (x and y). I would like to center the text on the screen, but I'm not sure how. I'm not sure how wide my fonts are. I have different sized fonts and whatnot, so being able to figure this out when the font is made would be best I'd think. This is my code for my Bitmap font class. Any help is welcome.
/*=================================================================
BitMapFont.cpp
Implementing the BitmapFont class.
==================================================================*/

#include "BitMapFont.h"

BitmapFont::BitmapFont(HDC hDC, int size, char* family)
{
    // set font size for later reference when centering
    fontSize = size;

    if (hDC && family && strlen(family) > 0)
    {
        HFONT font;     //windows font id
        HFONT oldFont;  // housekeeping
        base = glGenLists(256);

        font = CreateFont(  size * -1,                  //height of font
                            0,                          //width of font
                            0,                          //angle of escapement
                            0,                          //angle of orientation
                            FW_BOLD,                    //weight of font
                            FALSE,                      //italic
                            FALSE,                      //underline
                            FALSE,                      //strikeout
                            ANSI_CHARSET,               //character set
                            OUT_TT_PRECIS,              //output precision
                            CLIP_DEFAULT_PRECIS,        //clipping precision
                            ANTIALIASED_QUALITY,        //antialiase the font
                            FF_DONTCARE|DEFAULT_PITCH,  //Family And Pitch
                            family);                    //Font name

        oldFont = (HFONT)SelectObject(hDC, font);
        wglUseFontBitmaps(hDC, 0, 256, base);
        SelectObject(hDC, oldFont);
        DeleteObject(font);
    }
}

BitmapFont::~BitmapFont()
{
    glDeleteLists(base, 256);
}

bool BitmapFont::DrawString(int x, int y, const char *text, ...)
{
    glLoadIdentity();

    char    output[256];
    va_list ap;

    if (text == NULL)
        return false;

    va_start(ap, text);             // Parses The String For Variables
    vsprintf(output, text, ap);     // And Converts Symbols To Actual Numbers
    va_end(ap);

    glRasterPos3f(x, y, 0.0f);

    // Must save/restore the list base.
    glPushAttrib(GL_LIST_BIT);
    glListBase(base);
    glCallLists(strlen(output), GL_UNSIGNED_BYTE, output);
    glPopAttrib();

    return true;
}

bool BitmapFont::DrawCentered(int y, const char *text, ...)
{
    glLoadIdentity();

    float length = 0;
    char    output[256];
    va_list ap;

    if (text == NULL)
        return false;

    va_start(ap, text);                 // Parses The String For Variables
    vsprintf(output, text, ap);         // And Converts Symbols To Actual Numbers
    va_end(ap);

    // calculate length of string
    for(unsigned int i = 0; i < (strlen(output)); i++)
        length += fontSize;

    glRasterPos3f(SCREEN_WIDTH/2 - length/2, y, 0.0f);  // HELP!!

    // Must save/restore the list base.
    glPushAttrib(GL_LIST_BIT);
    glListBase(base);
    glCallLists(strlen(output), GL_UNSIGNED_BYTE, output);
    glPopAttrib();

    return true;
}



Share this post


Link to post
Share on other sites
Advertisement

// Load the widths of the current font (our font)
if( !GetCharWidth32(hdc, 32, 127, m_charWidths) ){
util::Message::print("IBitmapFont:: Unable to load character widths for font \"%s\"", name);
return false; // FIXME: error can't get the width properly, so we assume that there is a problem
}

Where m_charWidths is an array[96] of ints, because I believe that in the first set of ascii characters there are 96 printable characters (if I'm wrong please let me know, this would be something I would have to fix).

Then, to get the width of any particular character:

int getCharWidth(char c) const
{
return m_charWidths[c-32]; // because we're not using the first 32 characters (non-printable)
}

Share this post


Link to post
Share on other sites
Thanks for the help!!

How would you specify the font to use? I'm not sure exactly how to use this function to do what I need. I'm guessing you'd simply initialize the buffer array in the constructor and then utilize it afterwards when figuring out the length of the string. Correct?

Can anyone supply some more example code that uses the function or initializes it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!