.h:
class GL_Chinese_Font
{
private:
unsigned int m_listBase;
const char * m_string;
HDC & m_hDC;
public:
void ClearFont();
void glPrint(const char* str,char *fontName="arial", int fontSize=1,float depth=0.1,float pos_x=0.0f, float pos_y=0.0f);
BOOL DrawChinese(float pos_x,float pos_y,float fontSize);
unsigned int CreateChineseFont(char *fontName,int fontSize,float depth);
GL_Chinese_Font(HDC &gHDC_);
virtual ~GL_Chinese_Font();
};
.cpp:
GL_Chinese_Font::GL_Chinese_Font(HDC & gHDC_):m_hDC(gHDC_)
{
}
GL_Chinese_Font::~GL_Chinese_Font()
{
}
unsigned int GL_Chinese_Font::CreateChineseFont(char *fontName, int fontSize, float depth)
{
HFONT hNew,hOld;
LOGFONT cLogFont;
GLYPHMETRICSFLOAT agmf[1];
int i=0,j=0;
int iTotal=strlen(m_string);
unsigned int nChar=0;
int iNum=_mbslen((const unsigned char *)m_string);
m_listBase=glGenLists(iNum);
memset(&cLogFont,0,sizeof(LOGFONT));
cLogFont.lfEscapement=cLogFont.lfOrientation=0;
cLogFont.lfWeight=FW_NORMAL;
cLogFont.lfCharSet=GB2312_CHARSET;
cLogFont.lfOutPrecision=OUT_DEFAULT_PRECIS;
cLogFont.lfQuality=DEFAULT_QUALITY;
cLogFont.lfPitchAndFamily=DEFAULT_PITCH|FF_SWISS;
strcpy(cLogFont.lfFaceName,(LPCTSTR)fontName);
cLogFont.lfHeight=fontSize;
cLogFont.lfWidth=0;
hNew=CreateFontIndirect(&cLogFont);
hOld=(HFONT)SelectObject(m_hDC,hNew);
while(i<iTotal)
{
++j;
if(IsDBCSLeadByte(m_string[i]))
{
TRACE("is dbcs/n");
nChar=((unsigned char)m_string[i])*0x100+(unsigned char)m_string[i+1];
i+=2;
}
else
{
nChar=m_string[i];
++i;
}
wglUseFontOutlines(m_hDC,nChar,1,m_listBase+j,0.002f,depth,WGL_FONT_POLYGONS,agmf);
}
SelectObject(m_hDC,hOld);
DeleteObject(hNew);
return m_listBase;
}
BOOL GL_Chinese_Font::DrawChinese(float pos_x, float pos_y, float fontSize)
{
int iNum=_mbslen((const unsigned char *)m_string);
for (int i(1);i<=iNum;++i)
{
TRACE("draw word/n");
glPushMatrix();
glTranslatef(pos_x+fontSize*(i-1),pos_y,0);
glScalef(fontSize,fontSize,1);
glCallList(m_listBase+i);
glPopMatrix();
}
return TRUE;
}
void GL_Chinese_Font::glPrint(const char* str,char *fontName, int fontSize,float depth,float pos_x, float pos_y)
{
m_string=str;
CreateChineseFont(fontName,fontSize,depth);
DrawChinese(pos_x,pos_y,fontSize);
ClearFont();
}
void GL_Chinese_Font::ClearFont()
{
if(glIsList(m_listBase))
{
glDeleteLists(m_listBase,_mbslen((const unsigned char *)m_string));
m_listBase=0;
}
}
关键点:1: nChar=((unsigned char)m_string[i])*0x100+(unsigned char)m_string[i+1];获得字符位置;
2:wglUseFontOutlines(m_hDC,nChar,1,m_listBase+j,0.002f,depth,WGL_FONT_POLYGONS,agmf);自动生成显示列表。无需glNewList()...
3:注意清除显示列表...否则内存泄漏