SDL显示中文字体

时间:2021-11-26 06:34:45

 首先从ttf中读取字体,根据字体生成SDL_surface->opengl纹理.

basedef.h:

#ifndef BASEDEF_H
#define BASEDEF_H

//先区分操作系统
#undef WIN32
#undef LINUX

#ifdef _MSC_VER   //desktop or mobile win32 environment
    #define WIN32
#else
    #define LINUX     //Linux environment
#endif

#ifdef WIN32
    #include "windows.h"
    #include "direct.h"          //在win32和linux下所用的头是不同的
    #include <time.h>
#else
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/time.h>
#endif


#include <SDL.h>             //加入SDL的内容
#include <SDL_image.h>
#include <SDL_opengl.h>
//#include <GL/glext.h>

#define HHM_PI 3.14159


#ifndef TRUE
#define TRUE   1
#endif

#ifndef FALSE
#define FALSE  0
#endif

//定义行末识别符 win32与linux不同
#ifdef WIN32
    #define LINEEND  '/0'
#else
    #define LINEEND  '/r'
#endif

#ifdef WIN32
    #define PATHDIV '//'
#else
    #define PATHDIV '/'
#endif

#define Min(x,y)  ((x < y) ? x : y)
#define Max(x,y)  ((x > y) ? x : y)


#define GLMAXTEXSIZE 1024     //根据情况来调整 与游戏无关  


//0x0001  0x0002  预留出来 给逻辑控制   与具体游戏无关
#define TODOMULTIPASS    0x0004    //使用MultiPass来实现特效
#define TODOMULTITEXTRUE 0x0008    //使用MultiTexture来实现特效
#define SHOWGRID         0x0010    //显示网格
#define OPENLIGHT        0x0020    //开灯
#define ANTIALIASE       0x0040    //反走样
#define MIRRORCCW        0x0080    //将正面取反(如在绘制镜面时)
#define CARDANTIALIASE   0x0100    //牌反走样
#define USEMIPMAP        0x0200    //运用mipmap
#define USELINEARFILTER  0x0400    //运用线性过滤


#endif

convert-charset..h

#ifndef __header_convert_charset__
#define __header_convert_charset__

#define _UNICODE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


//先区分操作系统
#undef WIN32
#undef LINUX

#ifdef _MSC_VER   //desktop or mobile win32 environment
    #define WIN32
#else
    #define LINUX     //Linux environment
#endif


#ifdef LINUX
    #define HAVE_ICONV
#endif


#ifdef HAVE_ICONV
#include <iconv.h>
#endif

#ifdef _WIN32
    #include <windows.h>
    //#include <WINNLS.H>
#endif

#ifdef __cplusplus
extern "C" {
#endif
 
 bool GB2312_AnsiToUnicode(char *lpszOut, const char* lpszIn,
  const char* lpszCurCode = NULL,
  const char* lpszOutCode = NULL);
 
 int GB2312_AnsiToUnicode_Size(const char* lpszIn);
 
 
#ifdef __cplusplus
}
#endif


#endif      //  End of __header_convert_charset__

convert-charset.cpp:

#include "convert-charset.h"
//#include <WINNLS.H>

bool GB2312_AnsiToUnicode(char *lpszOut, const char* lpszIn,
                          const char* lpszCurCode/* = NULL*/,
                          const char* lpszOutCode/* = NULL*/)
{
#ifdef WIN32       //  _WIN32
       //Do nothing

 int lpszOut_size=GB2312_AnsiToUnicode_Size(lpszIn);


    //这函数与操作系统相关,XP下用CP_THREAD_ACP,win2000下用CP_ACP
 MultiByteToWideChar(CP_THREAD_ACP, 0,lpszIn,strlen(lpszIn)+1,(unsigned short*)lpszOut,lpszOut_size);
   

#else               //  _WIN32
#ifdef HAVE_ICONV
    /* Use iconv to convert the message into utf-16.
    * "char" and "" are aliases for the local 8-bit encoding */
    iconv_t cd;
    //  ICONV_CONST char *from_str = message;
    char *from_str = (char*)lpszIn;
    char *to_str = lpszOut;
    size_t from_sz = strlen(lpszIn) + 1;
    //      size_t to_sz = from_sz*4;
    size_t to_sz = GB2312_AnsiToUnicode_Size(lpszIn);
    size_t res;
    int i;
 char lpszCodeName_From[30], lpszCodeName_To[30];
 if (lpszCurCode)
  strcpy(lpszCodeName_From, lpszCurCode);
 else
  strcpy(lpszCodeName_From, "gb2312");
 if (lpszOutCode)
  strcpy(lpszCodeName_To, lpszOutCode);
 else
  strcpy(lpszCodeName_To, "UTF-16");
#ifdef _DEBUG
 fprintf(stdout, "Convert '%s' from %s to %s/n", lpszIn, lpszCodeName_From, lpszCodeName_To);
#endif
    //  if ((cd = iconv_open("unicode", "gb2312")) == (iconv_t)-1) {
    if ((cd = iconv_open(lpszCodeName_To, lpszCodeName_From)) == (iconv_t)-1) {
        perror("Couldn't open iconv");
        return false;
     }
     res = iconv(cd, &from_str, &from_sz, &to_str, &to_sz);
     if (res == -1) {
  perror("Couldn't use iconv");
  return false;
     }
     iconv_close(cd);

#endif
#ifdef _DEBUG
 fprintf(stdout, "Convert result: '%s'/n", lpszOut);
#endif
#endif      //  End of _WIN32

 return true;
}

int GB2312_AnsiToUnicode_Size(const char* lpszIn)
{
    int iCounter = 0;
 for (int index = 0; lpszIn[index]; index++) {
  if( (unsigned char)lpszIn[index] > 127 )
  {
   index++;
  }
  else
  {
            ;
  }
  iCounter+=2;
 }
    iCounter += 4;
#ifdef _DEBUG
    fprintf(stdout, "GB2312_AnsiToUnicode_Size() return %d/n", iCounter);
#endif
    return iCounter;
    //  return 48;
}

 

//字幕纹理对象信息
struct TexInfo
{
 GLuint TexId;    //字幕纹理号
 int w;
 int h;
};

 

font.h内容:

#include <string>
#include "RendText.h"


class CFont  :public CRendText
{
public:
 CFont();
 /* *style: -b(黑体)-i(斜体)-u(下划线)
 -s(以指定颜色做文字背景)-S(以图片做文字背景)
 *finename: 字库名字不包扩路径
 size: 字体大小 */
 void T_SetFontStyle(char *style,char *filename,int size);
 //文字颜色 
 bool T_SetFontColor(int r,int g,int b);
 //文字背景色
 bool T_SetFontBgColor(int r,int g,int b);
 //文字坐标和内容
 bool T_TextOut(int x,int y,char *message);
    SDL_Surface* GL_GenImage(char *message);
 /*
  如下函数是继承来的   可直接引用
 const char* get_FontPath() const;           //获得字库的路径
    int get_RenderStyle() const;    //获得文字的风格信息
    int set_RenderStyle(const int newStyle);    //设置文字的风格
    int get_FontSize() const;     //获得文字大小
    SDL_Color get_ForeColor() const;   //获得文字的颜色
    SDL_Color set_ForeColor(const SDL_Color newColor);//设置文字的颜色
    SDL_Color get_BackColor() const;   //获得文字的背景色
    SDL_Color set_BackColor(const SDL_Color newColor);//设置文字的背景色
    const TTF_Font * get_Font() const;          //获得文字的TTF结构 有字库名和字体大小

    //  Added by zhanzb, 20041222
    const bool get_ReleaseFontAfterUsed() const;
    bool set_ReleaseFontAfterUsed(const bool bReleaseFontAfterUsed);


 */
    bool initsuccessflag;
 virtual ~CFont();
protected:
 void draw(SDL_Surface *image, int x, int y);
 char name[255]; 
 SDL_Color textcolor;
 SDL_Color textbg;
 int fontsize;
 int rendersolid;
 int rendertype;


};

 

font.cpp:

 


#include "basedef.h"
#include "Font.h"


#ifdef WIN32
    #include "direct.h"          //在win32和linux下所用的头是不同的  为了getcwd
#else
    #include <unistd.h> 
#endif

#define  ZKPATH    "zk"

CFont::CFont()
{
textcolor.r=0xff;
textcolor.g=0xff;
textcolor.b=0xff;
textbg.r=0;
textbg.g=0;
textbg.b=0;
initsuccessflag=false;

}
void CFont::T_SetFontStyle(char *style,char *filename,int size)
{
 
 int i;
 rendertype=0;
 for ( i=0; style[i]!='/0';i++ )
 {
  if(style[i]=='-')
  {
   i++;
   if (style[i] == 's' )
   {
    rendersolid = 1;
   }
   if(style[i]  == 'S')
   {
    rendersolid =0;
   }
   if ( style[i] == 'b' )
   {
    rendertype |= TTF_STYLE_BOLD;
   }
   if ( style[i] == 'i' )
   {
    rendertype |= TTF_STYLE_ITALIC;
   }
   if ( style[i] == 'u')
   {
    rendertype |= TTF_STYLE_UNDERLINE;
   }
  }
 }
 fontsize=size;
    name[0]='/0';

#ifdef LINUXSO
    char filepname[255];

    getcwd( filepname, 255 );      //获得工作目录
    sprintf(filepname, "%s%c",  filepname, PATHDIV);   
    sprintf(filepname, "%s%s",  filepname, "res");         //res/SuperPokerE/
    sprintf(filepname, "%s%c",  filepname, PATHDIV);
    sprintf(filepname, "%s%s",  filepname, "SuperPokerE");  
    sprintf(filepname, "%s%c",  filepname, PATHDIV);
    strcpy(name,filepname);
#endif

    strcat(name,ZKPATH);
 sprintf(name, "%s%c", name,  PATHDIV);
 strcat(name,filename);
    initsuccessflag=true;
}

bool CFont::T_SetFontColor(int r,int g,int b)
{
 textcolor.r =r;
 textcolor.g =g;
 textcolor.b =b;
 set_ForeColor(textcolor);
 return true;
}

bool CFont::T_SetFontBgColor(int r,int g,int b)
{
 textbg.r =r;
 textbg.g =g;
 textbg.b =b;
 set_BackColor(textbg);
 return true;
}

bool CFont::T_TextOut(int x,int y,char *message)
{
    if (message[0]=='/0')
        return false;
    SDL_Surface *image
    =CRendText::RendText(name,message,rendertype,fontsize,rendersolid,textcolor,textbg);
    name[0]='/0';
 draw(image,x,y);
 SDL_FreeSurface(image);
 return true;
}

SDL_Surface* CFont::GL_GenImage(char *message)
{
    if (message[0]=='/0')
        return NULL;
    SDL_Surface *image
  =CRendText::RendText(name,message,rendertype,fontsize,rendersolid,textcolor,textbg);
    name[0]='/0';
 return image;
}

void CFont::draw(SDL_Surface *image, int x, int y)
{
   
    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
 if(image->w>800)
    dest.w = 800;
 else
  dest.w=image->w;
 if(image->h>600)
  dest.h=600;
 else
    dest.h = image->h;
    SDL_Surface* mainSurface = SDL_GetVideoSurface();

 SDL_BlitSurface(image, NULL, mainSurface, &dest);

    //SDL_UpdateRect(mainSurface, 0, 0, 0, 0);   
}

 

CFont::~CFont()
{
}

renderText.h

#ifndef __header_RendText__
#define __header_RendText__

#define _UNICODE

#include <stdlib.h>
#include <stdio.h>
#include <string>

#include "SDL.h"
#include "SDL_ttf.h"

#include <stddef.h>
#include "convert-charset.h"

class CRendText
{
public:
 CRendText();
 ~CRendText();
 SDL_Surface *RendText(
  const char      *lpszFontFileName,
  const char      *lpszText,
  const int       renderstyle,
  const int       fontSize,
  const int       rendersolid,
  const SDL_Color fgcol,
  const SDL_Color bgcol);
    void ReleaseFont();

    const char* get_FontPath() const;
    int get_RenderStyle() const;
    int set_RenderStyle(const int newStyle);
    int get_FontSize() const;
    SDL_Color get_ForeColor() const;
    SDL_Color set_ForeColor(const SDL_Color newColor);
    SDL_Color get_BackColor() const;
    SDL_Color set_BackColor(const SDL_Color newColor);
    const TTF_Font * get_Font() const;

    //  Added by zhanzb, 20041222
    const bool get_ReleaseFontAfterUsed() const;
    bool set_ReleaseFontAfterUsed(const bool bReleaseFontAfterUsed);

private:
 void Init();
 char m_lpszFontPath[260];
 int  m_RenderStyle;
 int  m_fontSize;
 int  m_rendersolid;
 SDL_Color m_fgcol,m_bgcol;
 TTF_Font  *m_Font;

    //  Added by zhanzb, 20041222
    bool m_bReleaseFontAfterUsed;
};


#endif      //  End of __header_RendText__

 

renderText.cpp:

 

#define _UNICODE

#include "RendText.h"


CRendText::CRendText()
{
 if ( TTF_Init() < 0 ) {
  fprintf(stderr, "Couldn't initialize TTF: %s/n",SDL_GetError());
  //exit(2);
 }
 Init();
}

void CRendText::Init()
{
    m_lpszFontPath[0] = 0;
    m_RenderStyle = TTF_STYLE_NORMAL;
    m_fontSize = 12;
    m_rendersolid=0;
    m_Font = NULL;
    m_bReleaseFontAfterUsed = false;
}

CRendText::~CRendText()
{
    ReleaseFont();
}

void CRendText::ReleaseFont()
{
#ifdef _DEBUG
    fprintf(stdout, "Release font '%s', m_Font(%d)/n", m_lpszFontPath, (long)m_Font);
#endif
    if (m_Font) TTF_CloseFont(m_Font);
 Init();
}

SDL_Surface *CRendText::RendText(
         const char      *lpszFontFileName,
         const char      *lpszText,
         const int       renderstyle,
         const int       fontSize,
         const int       rendersolid,
         const SDL_Color fgcol,
         const SDL_Color bgcol)
{
#ifdef _DEBUG
    fprintf(stdout, "/n/nFontFileName: %s, %s/n", lpszFontFileName, m_lpszFontPath);
    fprintf(stdout, "renderstyle: %d, %d/n", renderstyle, m_RenderStyle);
    fprintf(stdout, "fontSize: %d, %d/n", fontSize, m_fontSize);
#endif
 
 SDL_Surface *text = NULL;
    if (
        (lpszFontFileName[0] != 0 &&                        //  使用新字型
        strcmp(lpszFontFileName, m_lpszFontPath) != 0) ||
        m_fontSize != fontSize)                             //  或者字体大小改变了
    {
        TTF_Font *newFont = NULL;
        newFont = TTF_OpenFont(lpszFontFileName, fontSize);
  if ( NULL == m_Font && NULL == newFont )
        {
   fprintf(stderr, "huang Couldn't load %d pt font from %s: %s/n", fontSize, lpszFontFileName, SDL_GetError());
   return NULL;
  }
        else if (NULL != m_Font && NULL == newFont )
        {
            fprintf(stderr, "Couldn't load %d pt font from %s: %s, use old font/n", fontSize, lpszFontFileName, SDL_GetError());
        }
        else if (NULL != newFont )
        {
#ifdef _DEBUG
            fprintf(stdout, "Load %d pt font from '%s' succeed!/n", fontSize, lpszFontFileName);
#endif
            ReleaseFont();
            strcpy(m_lpszFontPath, lpszFontFileName);
            m_Font = newFont;
            m_fontSize = fontSize;
        }
    }
    else
    {
        if(!m_Font) return NULL;
#ifdef _DEBUG
        fprintf(stdout, "Reuse old font: %d pt font from '%s' succeed!/n", fontSize, lpszFontFileName);
#endif
    }
 
 m_RenderStyle = renderstyle;
    TTF_SetFontStyle(m_Font, renderstyle); 
 
    unsigned int iCount = GB2312_AnsiToUnicode_Size(lpszText);
    if (0 == iCount)
        return NULL;
    //  else
    //      iCount += 2;
 
 char *unicode_text = new char[iCount];
 
 if( !GB2312_AnsiToUnicode(unicode_text, lpszText, "GB2312", "UTF-16"))
    {
        delete unicode_text;
        return NULL;
    }
 
#ifdef _DEBUG
    fprintf(stdout, "fgcol: %d,%d,%d/n", fgcol.r, fgcol.g, fgcol.b);
    fprintf(stdout, "bgcol: %d,%d,%d/n", bgcol.r, bgcol.g, bgcol.b);
#endif
    if (memcmp(&m_fgcol, &fgcol, sizeof(m_fgcol)) != 0)
        memcpy(&m_fgcol, &fgcol, sizeof(m_fgcol));
    if (memcmp(&m_bgcol, &bgcol, sizeof(m_fgcol)) != 0)
        memcpy(&m_bgcol, &bgcol, sizeof(m_fgcol));
#ifdef _DEBUG
    fprintf(stdout, "m_fgcol: %d,%d,%d/n", m_fgcol.r, m_fgcol.g, m_fgcol.b);
    fprintf(stdout, "m_bgcol: %d,%d,%d/n", m_bgcol.r, m_bgcol.g, m_bgcol.b);
    fprintf(stdout, "rendersolid: %d/n",rendersolid); 
#endif
 m_rendersolid=rendersolid;
 if(m_rendersolid==0)
  text = TTF_RenderUNICODE_Solid(m_Font, (Uint16*)unicode_text, m_fgcol);
 else
  text = TTF_RenderUNICODE_Shaded(m_Font, (Uint16*)unicode_text, m_fgcol, m_bgcol);
 
 delete unicode_text;
 if ( text == NULL ) {
  fprintf(stderr, "Couldn't render text: %s/n", SDL_GetError());
  TTF_CloseFont(m_Font);
  return NULL;
 }
#ifdef _DEBUG
 else
  SDL_SaveBMP( text, "hello.bmp" );
#endif
 //  TTF_CloseFont(m_Font);
    if (m_bReleaseFontAfterUsed)
        ReleaseFont();
 return text;
 };
 const char* CRendText::get_FontPath() const
 {
  return m_lpszFontPath;
 }
 
 int CRendText::get_RenderStyle() const
 {
  return m_RenderStyle;
 }
 
 int CRendText::set_RenderStyle(const int newStyle)
 {
  int old = m_RenderStyle;
  m_RenderStyle = newStyle;
  return old;
 }
 
 int CRendText::get_FontSize() const
 {
  return m_fontSize;
 }
 
 SDL_Color CRendText::get_ForeColor() const
 {
  return m_fgcol;
 }
 
 SDL_Color CRendText::set_ForeColor(const SDL_Color newColor)
 {
  SDL_Color old = m_fgcol;
  m_fgcol =  newColor;
  return old;
 }
 
 SDL_Color CRendText::get_BackColor() const
 {
  return m_bgcol;
 }
 
 SDL_Color CRendText::set_BackColor(const SDL_Color newColor)
 {
  SDL_Color old = m_bgcol;
  m_bgcol = newColor;
  return old;
 }
 
 const TTF_Font * CRendText::get_Font() const
 {
  return m_Font;
 }
 
 const bool CRendText::get_ReleaseFontAfterUsed() const
 {
  return m_bReleaseFontAfterUsed;
 }
 
 bool CRendText::set_ReleaseFontAfterUsed(const bool bReleaseFontAfterUsed)
 {
  bool old = m_bReleaseFontAfterUsed;
  m_bReleaseFontAfterUsed = bReleaseFontAfterUsed;
  return old;
 }

 

main函数:

 

#include <gl/glut.h>
#include <gl/gl.h>   // Header File For The OpenGL32 Library
#include <gl/glu.h>   // Header File For The GLu32 Library
//#include <gl/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include "font.h"


GLfloat LightAmbient[]=  { 0.01f, 0.01f, 0.01f, 1.0f };
GLfloat LightDiffuse[]=  { 0.033f, 0.033f, 0.033f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 1.0f, 1.0f };
void init(void);          //初始化 
void display(void);                                     //显示
void reshape(int w,int h);        //窗口变化
void mouse(int button,int state,int x, int y);   //鼠标操作
const GLfloat X = 2;         //房间大小
TexInfo getTexInfo(char *message);       //获得字符纹理
int ConvertToPowerOfTwo(int input) ;
GLfloat i=2;          //字符纹理
GLfloat V[8][3]={{X,X,X},{X,X,-X},{-X,X,-X},{-X,X,X},
       {-X,-X,X},{X,-X,X},{X,-X,-X},{-X,-X,-X}};

void displayMenu();          //显示菜单
bool isShow;           //是否显示
int xPos,yPos;

int main(int argc,char** argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
 glutInitWindowSize(800,600);
 glutInitWindowPosition(100,100);
 glutCreateWindow(argv[0]);
 init();
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
 glutMouseFunc(mouse);
 glutMainLoop();
 return 0;
}

void init(void)
{
 isShow = true;
 glClearColor(0.0,0.0,0.0,0.0);
 glShadeModel(GL_FLAT);       // Enable Smooth Shading
}

void display(void)
{
 TexInfo texInfo;
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1.0,1.0,1.0);
 glEnable(GL_TEXTURE_2D);

 char *message = "所在目录:";
 texInfo=getTexInfo(message);
 glBindTexture(GL_TEXTURE_2D, texInfo.TexId);
 //压崭
 glPushMatrix();

 glColor3f(1.0,0.0,0.0);
 glBegin(GL_QUADS);                                           //绘制立方体top
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[0]);
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[1]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[2]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[3]);
 glEnd();
 glColor3f(0.0,0.0,1.0);
 glBegin(GL_QUADS);                                           //绘制立方体right
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[3]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[2]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[7]);
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[4]);
 glEnd();
 glBegin(GL_QUADS);                                           //绘制立方体left
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[0]);
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[1]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[6]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[5]);
 glEnd();
 glColor3f(0.0,1.0,1.0);
 glBegin(GL_QUADS);                                           //绘制立方体bottom
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[7]);
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[6]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[5]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[4]);
 glEnd();
 glColor3f(1.0,0.0,0.0);
 glBegin(GL_QUADS);                                           //绘制立方体front
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[0]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[3]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[4]);
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[5]);
 glEnd();
 glColor3f(1.0,1.0,0.0);
 glBegin(GL_QUADS);                                           //绘制立方体back
  glTexCoord2f(1.0f, 0.0f);glVertex3fv(V[1]);
  glTexCoord2f(1.0f, 1.0f);glVertex3fv(V[6]);
  glTexCoord2f(0.0f, 1.0f);glVertex3fv(V[7]);
  glTexCoord2f(0.0f, 0.0f);glVertex3fv(V[2]);
 glEnd();
 glPopMatrix();
 if(isShow)
 {
  displayMenu();
 }
 

 glutSwapBuffers();
 glFlush();
}

void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

void mouse(int button,int state,int x, int y)
{
 printf("The mouse state is %d , %d/n",state,button);
 switch(button)
 {
 case GLUT_LEFT_BUTTON:
  {
   xPos=x;
   yPos=y;
   isShow=true;
   printf("GLUT_LEFT_BUTTON/n");
   glutPostRedisplay();
  }
  break;
 case GLUT_RIGHT_BUTTON:
  {
   isShow=false;
   printf("GLUT_RIGHT_BUTTON/n");
   glutPostRedisplay();
  }
  break;
 default:
  break;
 }
}

TexInfo getTexInfo( char *message)
{
 TexInfo texInfo;
 CFont myFont;
 myFont.T_SetFontColor(255,0,255);
 myFont.T_SetFontBgColor(200,200,200);
 myFont.T_SetFontStyle("-s","STKAITI.TTF",40);

 SDL_Surface  *fontSurface = NULL;
 SDL_Surface  *tmp = NULL;

 fontSurface = myFont.GL_GenImage(message);

 texInfo.w = fontSurface->w;
 texInfo.h = fontSurface->h;

 printf("texInfo is %d and %d/n",texInfo.w,texInfo.h);
 texInfo.w = ConvertToPowerOfTwo(texInfo.w);
 texInfo.h = ConvertToPowerOfTwo(texInfo.h);
 printf("texInfo is %d and %d/n",texInfo.w,texInfo.h);

 tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, texInfo.w, texInfo.h, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);

 if (!tmp)
    {
  SDL_FreeSurface(fontSurface);
  return texInfo;
 }

 if (SDL_BlitSurface(fontSurface, NULL, tmp, NULL))
    {
  SDL_FreeSurface(tmp);
  SDL_FreeSurface(fontSurface);
  return texInfo;
 }

 glGenTextures(1, &texInfo.TexId);

 glBindTexture(GL_TEXTURE_2D, texInfo.TexId);

 glTexImage2D(GL_TEXTURE_2D, 0, 4, texInfo.w, texInfo.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp->pixels);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

 SDL_FreeSurface(tmp);
 SDL_FreeSurface(fontSurface);

    glBindTexture(GL_TEXTURE_2D, 0);
 return texInfo;

}

//调整为规则尺寸2的幂
int ConvertToPowerOfTwo(int input)
{
 int value = 1;

 while ( value < input )
  value <<= 1;

 return value;
}

void displayMenu()
{
 
 TexInfo texInfo[4];
 char *message = "所在目录:";
 texInfo[0]=getTexInfo(message);
 message = "修改时间:";
 texInfo[1]=getTexInfo(message);
 message = "创建时间:";
 texInfo[2]=getTexInfo(message);
 message = "文件大小:";
 texInfo[3]=getTexInfo(message);

 glPushMatrix();
 glTranslatef(-1.0,0.0,-1.0);
 glScalef(1.0,1.2,1.0);
 glColor3f(0.0,1.0,1.0);
 glBindTexture(GL_TEXTURE_2D, texInfo[0].TexId);
 glBegin(GL_QUADS);                                          
  glTexCoord2f(0.8f, 0.0f);glVertex3f(1.0,0.5,0.0);
  glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0,0.5,0.0);
  glTexCoord2f(0.0f, 0.8f);glVertex3f(0.0,0.0,0.0);
  glTexCoord2f(0.8f, 0.8f);glVertex3f(1.0,0.0,0.0);
 glEnd();
 
 glBindTexture(GL_TEXTURE_2D, texInfo[1].TexId);
 glBegin(GL_QUADS);                                          
  glTexCoord2f(0.8f, 0.0f);glVertex3f(1.0,1.0,0.0);
  glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0,1.0,0.0);
  glTexCoord2f(0.0f, 0.8f);glVertex3f(0.0,0.5,0.0);
  glTexCoord2f(0.8f, 0.8f);glVertex3f(1.0,0.5,0.0);
 glEnd();
 
 glBindTexture(GL_TEXTURE_2D, texInfo[2].TexId);
 glBegin(GL_QUADS);                                          
  glTexCoord2f(0.8f, 0.0f);glVertex3f(1.0,1.5,0.0);
  glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0,1.5,0.0);
  glTexCoord2f(0.0f, 0.8f);glVertex3f(0.0,1.0,0.0);
  glTexCoord2f(0.8f, 0.8f);glVertex3f(1.0,1.0,0.0);
 glEnd();
 
 glBindTexture(GL_TEXTURE_2D, texInfo[3].TexId);
 glBegin(GL_QUADS);                                          
  glTexCoord2f(0.8f, 0.0f);glVertex3f(1.0,2.0,0.0);
  glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0,2.0,0.0);
  glTexCoord2f(0.0f, 0.8f);glVertex3f(0.0,1.5,0.0);
  glTexCoord2f(0.8f, 0.8f);glVertex3f(1.0,1.5,0.0);
 glEnd();

 glPopMatrix();
}