OpenGL中文字体类

.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:注意清除显示列表...否则内存泄漏

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值