Game Development Reference
In-Depth Information
Now to display a string we can use a simple function. First, we set the
transformation matrix, disable lighting calculations, set the current colour
and set the base number for list use. To actually display the characters we
use the GL function glCallLists. This function takes three parameters: the
number of lists that are to be drawn, a type value which we set as an
unsigned byte and finally an array of numerical values that define the lists
to use. A string is actually an array of unsigned bytes. A string that reads
'This is a string' is stored in memory as
T
h
i
s
i
s
a
084
104
105
115
032
105
115
032
097
032
s
t
r
i
n
g
115
116
114
105
110
103
000
Since for our list value 32 or space is actually the first list, we need to
make sure that when the value 32 is encountered we use the first list we
stored. This is achieved by setting the listbase to 32 less than the stored
value. Now when displaying the string above, list 52 is used to display 'T'
and list 0 displays a space.
void CGLText::DrawText(int x, int y, int scale, CString str)
{
//Create a transformation matrix
glLoadIdentity();
glTranslatef(x, y, -5.0f);
glScalef(scale, scale, scale);
glDisable(GL_LIGHTING);
glRGB(m_red,m_green,m_blue);
// Display a string
glListBase(m_listbase - 32); // Indicates the start of display
// lists
// Draw the characters in a string
glCallLists(str.GetLength(), GL_UNSIGNED_BYTE, (LPCTSTR)str);
glEnable(GL_LIGHTING);
}
An alternative approach to text involves using a bitmap-based font. You
could create a bitmap which contains an array of an ANSI character set. If
the font is elaborate then this is often a good way to proceed. Since most
Search Nedrilad ::




Custom Search