Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Protected Attributes
BMF_BitmapFont Class Reference

***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** More...

#include <BMF_BitmapFont.h>

Collaboration diagram for BMF_BitmapFont:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 BMF_BitmapFont (BMF_FontData *fontData)
 Default constructor.
virtual ~BMF_BitmapFont (void)
 Destructor.
void drawString (const char *str)
 Draws a string at the current raster position.
void drawStringMemory (char *str)
int getStringWidth (char *str)
 Draws a string at the current raster position.
void getBoundingBox (int &xMin, int &yMin, int &xMax, int &yMax)
 Returns the bounding box of the font.
int getTexture ()
 Convert the font to a texture, and return the GL texture ID of the texture.
void drawStringTexture (char *string, float x, float y, float z)
 Draw the given string at the point x, y, z, using texture coordinates.

Protected Attributes

BMF_FontDatam_fontData
 Pointer to the font data.

Detailed Description

***** BEGIN GPL/BL DUAL LICENSE BLOCK *****

This program is free software; you can redistribute it and/or modify it under the terms of the GNU bteral Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The Blender Foundation also sells licenses for use in proprietary software under the Blender License. See http://www.blender.org/BL/ for information about this.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU bteral Public License for more details.

You should have received a copy of the GNU bteral Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. All rights reserved.

The Original Code is: all of this file.

Contributor(s): none yet.

***** END GPL/BL DUAL LICENSE BLOCK *****

Copyright (C) 2001 NaN Technologies B.V. Base class for OpenGL bitmap fonts.

Definition at line 44 of file BMF_BitmapFont.h.


Constructor & Destructor Documentation

Default constructor.

***** BEGIN GPL/BL DUAL LICENSE BLOCK *****

This program is free software; you can redistribute it and/or modify it under the terms of the GNU bteral Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The Blender Foundation also sells licenses for use in proprietary software under the Blender License. See http://www.blender.org/BL/ for information about this.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU bteral Public License for more details.

You should have received a copy of the GNU bteral Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. All rights reserved.

The Original Code is: all of this file.

Contributor(s): none yet.

***** END GPL/BL DUAL LICENSE BLOCK *****

Copyright (C) 2001 NaN Technologies B.V.

Definition at line 56 of file BMF_BitmapFont.cpp.

: m_fontData(fontData)
{
}
BMF_BitmapFont::~BMF_BitmapFont ( void  ) [virtual]

Destructor.

Definition at line 62 of file BMF_BitmapFont.cpp.

{
}

Member Function Documentation

void BMF_BitmapFont::drawString ( const char *  str)

Draws a string at the current raster position.

Parameters:
strThe string to draw.

Definition at line 67 of file BMF_BitmapFont.cpp.

{
       if (!str)
              return;

       GLint alignment;
       unsigned char c;
              
       glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

       while ((c = (unsigned char) *str++)) {
              BMF_CharData & cd = m_fontData->chars[c];
              
              if (cd.data_offset==-1) {
                     GLubyte nullBitmap = 0;
              
                     glBitmap(1, 1, 0, 0, cd.advance, 0, &nullBitmap);       
              } else {
                     GLubyte *bitmap = &m_fontData->bitmap_data[cd.data_offset];
              
                     glBitmap(cd.width, cd.height, cd.xorig, cd.yorig, cd.advance, 0, bitmap);
              }
       }

       glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
}
void BMF_BitmapFont::drawStringMemory ( char *  str)
void BMF_BitmapFont::drawStringTexture ( char *  string,
float  x,
float  y,
float  z 
)

Draw the given string at the point x, y, z, using texture coordinates.

This assumes that an appropriate texture has been bound, see BMF_BitmapFont::getTexture(). The string is drawn along the positive X axis.

Parameters:
stringThe c-string to draw.
xThe x coordinate to start drawing at.
yThe y coordinate to start drawing at.
zThe z coordinate to start drawing at.

Definition at line 177 of file BMF_BitmapFont.cpp.

{
       unsigned char c;
       float pos = 0;
       
       int baseLine = -(m_fontData->ymin);

       glBegin(GL_QUADS);
       while ((c = (unsigned char) *str++)) {
              BMF_CharData & cd = m_fontData->chars[c];
              
              if (cd.data_offset != -1) {
                     float cellX = (c%16)/16.0;
                     float cellY = (c/16)/16.0;
              
                     glTexCoord2f(cellX + 1.0/16.0, cellY);
                     glVertex3f(x + pos + 16.0, -baseLine + y + 0.0, z);

                     glTexCoord2f(cellX + 1.0/16.0, cellY + 1.0/16.0);
                     glVertex3f(x + pos + 16.0, -baseLine + y + 16.0, z);

                     glTexCoord2f(cellX, cellY + 1.0/16.0);
                     glVertex3f(x + pos + 0.0, -baseLine + y + 16.0, z);

                     glTexCoord2f(cellX, cellY);
                     glVertex3f(x + pos + 0.0, -baseLine + y + 0.0, z);
              }
              
              pos += cd.advance;
       }
       glEnd();
}
void BMF_BitmapFont::getBoundingBox ( int &  xMin,
int &  yMin,
int &  xMax,
int &  yMax 
)

Returns the bounding box of the font.

The width and height represent the bounding box of the union of all glyps. The minimum and maximum values of the box represent the extent of the font and its positioning about the origin.

Definition at line 108 of file BMF_BitmapFont.cpp.

{
       xMin = m_fontData->xmin;
       yMin = m_fontData->ymin;
       xMax = m_fontData->xmax;
       yMax = m_fontData->ymax;
}
int BMF_BitmapFont::getStringWidth ( char *  str)

Draws a string at the current raster position.

Parameters:
strThe string to draw.
Returns:
The width of the string.

Definition at line 96 of file BMF_BitmapFont.cpp.

{
       unsigned char c;
       int length = 0;

       while ((c = (unsigned char) *str++)) {
              length += m_fontData->chars[c].advance;
       }
       
       return length;
}

Here is the call graph for this function:

Convert the font to a texture, and return the GL texture ID of the texture.

If the texture ID is bound, text can be drawn using the texture by calling drawStringTexture.

Returns:
The GL texture ID of the new texture, or -1 if unable to create.

Definition at line 116 of file BMF_BitmapFont.cpp.

{
       int fWidth = m_fontData->xmax - m_fontData->xmin;
       int fHeight = m_fontData->ymax - m_fontData->ymin;
       
       if (fWidth>=16 || fHeight>=16) {
              return -1;
       }
       
       int cRows = 16, cCols = 16;
       int cWidth = 16, cHeight = 16;
       int iWidth = cCols*cWidth;
       int iHeight = cRows*cHeight;
       GLubyte *img = new GLubyte [iHeight*iWidth];
       GLuint texId;

       int baseLine = -(m_fontData->ymin);
       
       memset(img, 0, iHeight*iWidth);
       for (int i = 0; i<256; i++) {
              BMF_CharData & cd = m_fontData->chars[i];
              
              if (cd.data_offset != -1) {
                     int cellX = i%16;
                     int cellY = i/16;
                     
                     for (int y = 0; y<cd.height; y++) {
                            GLubyte* imgRow = &img[(cellY*cHeight + y + baseLine - cd.yorig)*iWidth];
                            GLubyte* chrRow = &m_fontData->bitmap_data[cd.data_offset + ((cd.width+7)/8)*y];

                            for (int x = 0; x<cd.width; x++) {
                                   GLubyte* imgPxl = &imgRow[(cellX*cWidth + x - cd.xorig)];
                                   int byteIdx = x/8;
                                   int bitIdx = 7 - (x%8);
                                   
                                   if (chrRow[byteIdx]&(1<<bitIdx)) {
                                          imgPxl[0] = 255;
                                   }
                            }
                     }
              }
       }
       
       glGenTextures(1, &texId);
       
       glBindTexture(GL_TEXTURE_2D, texId);

       glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA4, iWidth, iHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, img);
       if (glGetError()) {
              glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE4_ALPHA4, iWidth, iHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, img);
       }
       
       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
       
       delete [] img;
       
       return texId;
}

Member Data Documentation

Pointer to the font data.

Definition at line 107 of file BMF_BitmapFont.h.


The documentation for this class was generated from the following files: