mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TextureFont.cpp
Go to the documentation of this file.
1 
6 #include "GlobalParameters.h"
7 #include "TextureFont.h"
8 #include "Textures.h"
9 
13 TextureFont::TextureFont( GLuint texindex ) {
14  texture = texindex; // texture index for the font texture
15  count = 0;
16  m_size = 0;
17 }
18 
23  this->killFont();
24 }
25 
34 GLvoid TextureFont::buildFont( GLuint cpl, GLuint cpr, GLuint size, GLuint targetsize, GLuint skipspace ) {
35  m_width = skipspace;
36  m_size = targetsize;
37  float cx; // character x coordinates
38  float cy; // character y coordinates
39 
40  count = cpr * cpl;
41  base = glGenLists( count ); // create a display list big enough for all characters
42  //glBindTexture( GL_TEXTURE_2D, texture ); // select the font texture
43  Textures::getTextures()->bindTexture( texture );
44 
45  for( unsigned int i=0; i < count; i++ ) {
46  cx = float(i%size)/(float)cpl; // x position of current character (left)
47  cy = float(i/size)/(float)cpr; // y position of current character (top)
48 
49  glNewList( base + i, GL_COMPILE ); // create a listelement
50 
51  glBegin( GL_QUADS ); // each character is a quad
52  glTexCoord2f( cx, 1-cy-(1/(float)cpr) ); // texture coordinates bottom left
53  glVertex2i( 0, 0 ); // vertex coordinates bottom left
54  glTexCoord2f( cx + (1/(float)cpl), 1-cy-(1/(float)cpr)); // texture coordinates bottom right
55  glVertex2i( targetsize, 0 ); // vertex coordinates bottom right
56  glTexCoord2f( cx + (1/(float)cpl), 1-cy); // texture coordinates top right
57  glVertex2i( targetsize, targetsize ); // vertex coordinates top right
58  glTexCoord2f( cx, 1-cy ); // texture coordinates top left
59  glVertex2i( 0, targetsize ); // vertex coordinates top left
60  glEnd();
61 
62  glTranslated( skipspace, 0, 0 ); // each character is only 10 pixels wide, move to the right
63  glEndList(); // finish list
64  }
65 }
66 
68 unsigned int TextureFont::getSize( void ) {
69  return m_size;
70 }
71 
73 unsigned int TextureFont::getWidth( void ) {
74  return m_width;
75 }
76 
80 GLvoid TextureFont::killFont( GLvoid ) {
81  glDeleteLists( base, count ); // delete all caracters
82 }
83 
85 
92 GLvoid TextureFont::print( int x, int y, const char *string, ... ) {
93  char text[256]; // created string
94  va_list ap; // pointer to list of arguments
95 
96  if( string == NULL )// quit, if there is no texst
97  return;
98 
99  va_start( ap, string ); // parse the string for variables
100  vsprintf_s( text, string, ap ); // convert symbols to actual numbers
101  va_end( ap ); // store results in text
102 
103  //glBindTexture( GL_TEXTURE_2D, texture ); // select font texture
104  Textures::getTextures()->bindTexture( texture );
105 
106  glPushMatrix();
107  glPushAttrib( GL_LIST_BIT ); // push display list bits
108  glTranslated( x, y, 0 ); // textposition (0,0 - Bottom Left)
109  glListBase( base - 32 );
110 
111  glCallLists( (int)strlen(text), GL_UNSIGNED_BYTE, text ); // print text to screen
112  glPopAttrib();
113  glPopMatrix();
114 }
115 
117 
122 void TextureFont::print( int x, int y, const std::string *text ) {
123  print(x, y, text->c_str());
124 }