⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 font.cpp

📁 wince 3d tutorial, it has various examples
💻 CPP
字号:
#include "font.h"
GLfixed BitmappedFont::m_textureCoordinates[];
bool BitmappedFont::m_instanced(false);

BitmappedFont::BitmappedFont(GLuint textureFontID, int fontWidth , int fontHeight):m_textureFontID(textureFontID),m_fontWidth(fontWidth),m_fontHeight(fontHeight)
{
  if(m_instanced) return;
  m_instanced = true;  

  const float rows = 8.0f;
  const float columns = 16.0f;
  const float xoffset = 1.0f / columns; // the font texture has 16 columns
  const float yoffset = 1.0f / rows; //the font texture has 8 rows   
          
  //prebuild the texture coordinates array for all characters
  for(int i=0;i<128;i++)
  {
    const float cx = (float)(i % 16) * xoffset; 
    const float cy = (float)(i / 16) * yoffset;   
    const int index = i * 8;
    m_textureCoordinates[  index  ] = FixedFromFloat(cx);
    m_textureCoordinates[index + 1] = FixedFromFloat(1 - cy - yoffset);
    m_textureCoordinates[index + 2] = FixedFromFloat(cx + xoffset);
    m_textureCoordinates[index + 3] = FixedFromFloat(1 - cy - yoffset);         
    m_textureCoordinates[index + 4] = FixedFromFloat(cx + xoffset);
    m_textureCoordinates[index + 5] = FixedFromFloat(1 - cy);
    m_textureCoordinates[index + 6] = FixedFromFloat(cx);
    m_textureCoordinates[index + 7] = FixedFromFloat(1 - cy);    
  }
}
//----------------------------------------------------------------------------
BitmappedFont::~BitmappedFont()
{
  //if you want, you can delete your font texture here
}
//----------------------------------------------------------------------------
void BitmappedFont::EnableStates()
{
  /*This function exists because we can use it to enable all needed states to display the text
    for many Print calls, instead of call this states on every Print call*/
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);  
  //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
}
//----------------------------------------------------------------------------
void BitmappedFont::DisableStates()
{
  glDisable(GL_TEXTURE_2D);
  glDisableClientState(GL_VERTEX_ARRAY);  
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnable(GL_BLEND);
}
//----------------------------------------------------------------------------  
void BitmappedFont::Print(int x, int y, const char *fmt, ...)
{
  //Convert the variable argument list, to a single char[] array
  //be careful, the final string length must fit into this array
  char text[256]; 
  va_list ap;
  if (fmt == NULL) 
    return;

  va_start(ap, fmt);
    vsprintf(text, fmt, ap);
  va_end(ap);
  if(text[0]=='\0')
    return;

  int length = (int)strlen(text);

  //Set up our arrays
  const GLushort indices[]= {1, 2, 0, 3};  //triangle strip
  GLfixed vertices[12];
  glBindTexture(GL_TEXTURE_2D, m_textureFontID);
  glVertexPointer(3, GL_FIXED, 0, vertices);  
  
  const GLfixed fy = FixedFromInt(y);  
  const GLfixed fheight = FixedFromInt(m_fontHeight);  
  
  //go throught all chars int the array
  for(int i = 0;i < length;i++)
  {
    /*We have to substract 32 units, because the characters stored in our font texture starts in the
      ASCII code 32 (the first 32 ASCII code, are control codes, usually non-displayables(*/
    const int c = text[i]-32;    
    const float originX = x + (i * m_fontWidth);
    const float offsetX = originX + m_fontWidth;
        
    //Compute the dimensions and position of the 'quad' (in reality is a triangle strip with two triangles)
    vertices[0] = FixedFromFloat(originX);    vertices[1] = fy;            vertices[2] = 0;
    vertices[3] = FixedFromFloat(offsetX);    vertices[4] = fy;            vertices[5] = 0;
    vertices[6] = FixedFromFloat(offsetX);    vertices[7] = fy + fheight;  vertices[8] = 0;
    vertices[9] = FixedFromFloat(originX);    vertices[10]= fy + fheight;  vertices[11]= 0;
    
    //we access to the corresponding texture coordinates in the precomputed texture coordinates array
    glTexCoordPointer(2, GL_FIXED, 0, &m_textureCoordinates[c*8]);  
    
    //draw the character
    glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_SHORT,indices);
  }

  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -