HGE中文显示最新修改 - 070717

今天没事就把HGE中文的(hgeFontCN)修了一下..

去除了一些不要的东西..

这样使得内存占用小了一大节... 

以前用12号字要用15M左右的内存.

现在只需要9M了...

我直接贴代码了...

原理去看我以前发的文章...

#pragma  once

#include 
" hge.h "
#include 
" hgesprite.h "

#include 
< iostream >

class  hgeFontCN
{
public :
    
//  HZ_H_MAX = HZ_H_END - HZ_H_FIX + 1 + 1 - (RM_H_END - RM_H_FIX)  //  其中 +1 为ASCII码占一行
    
//  HZ_W_MAX = HZ_W_END - HZ_W_FIX + 1;
    
//  EX_H        = RM_H_END - RM_H_FIX + 1;
     enum  FONTCN_DATA
    {
        HZ_H_MAX    
=   82 ,     //  汉字编码高位量
        HZ_W_MAX     =   95 ,     //  汉字编码底位量
        EX_H         =   6 ,

        HZ_H_FIX    
=   0xA1 ,     //  汉字编码高位
        HZ_H_END     =   0xF7 ,    

        HZ_W_FIX    
=   0xA0 ,     //  汉字编码底位
        HZ_W_END     =   0xFE ,

        ASCII_FIX    
=   0x20 ,     //  ASCII编码
        ASCII_END     =   0x7F ,

        RM_H_FIX    
=   0xAA ,     //  去除部分汉字编码高位
        RM_H_END     =   0xAF
    };

    
struct  FontCN_Head
    {
        
char          strHead[ 0x09 ];
        
char          ImgFile[ 0x40 ];
        DWORD         dwStringCol;
        DWORD         dwStringRow;
        BYTE         iFontW;
        BYTE         iFontH;
    
public :
        
void  Set( char   * head,  const   char *  file, DWORD nCol, DWORD nRow, BYTE nFWidth, BYTE nFHeight);
    };

    
struct  FontCN_Frame
    {
        BYTE         HZ[
2 ];
        WORD         dwFontX;
        WORD         dwFontY;
    
public :
        
void  Set(BYTE h, BYTE l, WORD fx, WORD fy);
    };

public :
    hgeFontCN(
const   char   * filename);     //  根据字体文件创建
     ~ hgeFontCN( void );

    
void             Render( float  x,  float  y,  const   char   * string );         // 绘制字符串
     void             printf( float  x,  float  y,  const   char   * format, ...);     //  绘制格式字符串

    
void             SetColor(DWORD col){ sprFont -> SetColor(col); }         //  设置字体颜色
     void             SetZ( float  z){ sprFont -> SetZ(z); }                     //  设置Z缓冲
    DWORD            GetColor()  const  {  return  sprFont -> GetColor(); }     //  获得颜色
     float             GetZ()  const  {  return  sprFont -> GetZ(); }             //  获得Z缓冲
     float             GetHeight()  const  {  return  fHeight; }                 //  获得字体高度
     float             GetCNWidth()  const  {  return  fCNWidth; }
    
float             GetENWidth()  const  {  return  fENWidth; }

    
void             SetBlendMode( int  blend){sprFont -> SetBlendMode(blend);}     // 设置混和模式
     int                 GetBlendMode()  const  {  return  sprFont -> GetBlendMode(); }     // 获得混和模式

    
float             GetStringWidth( const   char   * string const ;
    
float             GetStringHeight( const   char   * string const ;

private :
    
static  HGE *         hge;
    hgeSprite
*         sprFont;     //  所有字模精灵    
    FontCN_Frame    fontFrame[HZ_H_MAX][HZ_W_MAX];     //  所有字符
     float            fCNWidth;        //  汉字宽度
     float            fENWidth;        //  ASCII宽度
     float             fHeight;         //  字符高度
     char             txtBuffer[ 512 ];     //  文本缓存
};

inline 
void  hgeFontCN::FontCN_Head::Set( char   * head,  const   char *  file, DWORD nCol, DWORD nRow, BYTE nFWidth, BYTE nFHeight)
{
    strcpy(strHead, head);
    strcpy(ImgFile, file);

    dwStringCol 
=  nCol;
    dwStringRow 
=  nRow;
    iFontW        
=  nFWidth;
    iFontH        
=  nFHeight;
}

inline 
void  hgeFontCN::FontCN_Frame::Set(BYTE h, BYTE l, WORD fx, WORD fy)
{
    HZ[
0 ]     =  h;
    HZ[
1 ]     =  l;
    dwFontX 
=  fx;
    dwFontY 
=  fy;
}

欢迎转载,如有问题请与作者联系:原作BLOG (QQ:29774874);


#include 
" hgefontcn.h "
#include 
< stdlib.h >
#include 
< stdio.h >


HGE
*     hgeFontCN::hge  =   0 ;

hgeFontCN::
~ hgeFontCN( void )
{
    hge
-> Texture_Free(sprFont -> GetTexture());
    delete sprFont;
    sprFont 
=   0 ;
    hge
-> Release();
}

hgeFontCN::hgeFontCN(
const   char   * filename)
{
    hge            
=  hgeCreate(HGE_VERSION);
    HTEXTURE    hTexture 
=   0 ;
    FontCN_Head fonthead;

    FILE 
* pFontFile  =  fopen(filename,  " rb " );

    
if  (fread( & fonthead,  1 sizeof (FontCN_Head), pFontFile)  !=   sizeof (FontCN_Head))
    {
        fclose(pFontFile);
        
return ;
    }
    
if  (strcmp(fonthead.strHead, " [FONTCN] " ))
    {
        fclose(pFontFile);
        
return ;
    }

    std::
string         imgFileName(fonthead.ImgFile);
    fCNWidth 
=  ( float )fonthead.iFontW;
    fENWidth 
=  fCNWidth  /   2   +   1 ;
#ifndef _DEBUG
    fENWidth 
+=   1 ;
#endif
    fHeight     
=  ( float )fonthead.iFontH;

    
char  szFullImgFile[_MAX_PATH];
    
char  drive[_MAX_DRIVE];
    
char  dir[_MAX_DIR];
    
char  fname[_MAX_FNAME];
    
char  ext[_MAX_EXT];            

    _splitpath(filename, drive, dir, fname, ext );
    sprintf(szFullImgFile,
" %s%s " , dir, imgFileName.c_str());

    
if  ((hTexture  =  hge -> Texture_Load(szFullImgFile))  ==   0 )
    {
        fclose(pFontFile);
        
return ;
    }
    
float  texW  =  ( float )hge -> Texture_GetWidth(hTexture, true );
    
float  texH  =  ( float )hge -> Texture_GetHeight(hTexture, true );
    sprFont 
=   new  hgeSprite(hTexture,  0 0 , texW, texH);

    FontCN_Frame    _frame;
    
while  ( ! feof(pFontFile))        
    {
        
if  (fread( & _frame,  1 sizeof (FontCN_Frame), pFontFile)  !=   sizeof (FontCN_Frame))
            
break ;

        BYTE hz0 
=  _frame.HZ[ 0 ];
        BYTE hz1 
=  _frame.HZ[ 1 ];    
        
        
if  (hz0  >=  HZ_H_FIX  &&  hz0  <=  HZ_H_END)
        {
            
if  (hz1  <  HZ_W_FIX  ||  hz1  >  HZ_W_END)
                
continue ;

            BYTE _ex 
=  hz0  >=  RM_H_FIX  ?  EX_H :  0 ;
            hz0 
-=  HZ_H_FIX  +  _ex;
            hz1 
-=  HZ_W_FIX;
        }
        
else   if  (hz0  ==   0xFF )
        {
            
if  (hz1  <  ASCII_FIX  ||  hz1  >=  ASCII_END)
                
continue ;
            hz0 
=  HZ_H_MAX  -   1 ;
            hz1 
-=  ASCII_FIX;
        }
        
else
            
continue ;            

        fontFrame[hz0][hz1] 
=  _frame;
    }
    fclose(pFontFile);    

    SetBlendMode(BLEND_COLORMUL 
|  BLEND_ALPHABLEND  |  BLEND_NOZWRITE);
}

void  hgeFontCN::Render( float  x,  float  y,  const   char   * string )
{
    
float     fx  =  x;
    
int         i  =   0 , j  =   0 ;

    
while ( * string )
    {
        BYTE hz0 
=   * string ;
        BYTE hz1 
=   * ( string + 1 );

        
if  ( * string == ' ' )
        {
            y 
+=  fHeight;
            fx 
=  x;
        }
        
else   if (hz0  >=  HZ_H_FIX  &&  hz0  <=  HZ_H_END)
        {
            BYTE _ex 
=  hz0  >=  RM_H_FIX  ?  EX_H :  0 ;
            i 
=  hz0  -  HZ_H_FIX  -  _ex;
            j 
=  hz1  -  HZ_W_FIX;
            sprFont
-> SetTextureRect(( float )fontFrame[i][j].dwFontX, ( float )fontFrame[i][j].dwFontY, fCNWidth, fHeight);
            sprFont
-> Render(fx, y);
            fx 
+=  fCNWidth;
            
string ++ ;
        }
        
else   if (hz0  >=  ASCII_FIX  &&  hz0  <  ASCII_END)
        {
            i 
=  HZ_H_MAX  -   1 ;
            j 
=  hz0  -  ASCII_FIX;
            sprFont
-> SetTextureRect(( float )fontFrame[i][j].dwFontX,( float )fontFrame[i][j].dwFontY, fENWidth, fHeight);
            sprFont
-> Render(fx, y);
            fx 
+=  fENWidth;
        }
        
else
        {    
            i 
=  HZ_H_MAX  -   1 ;
            j 
=   ' ? ' ;
            sprFont
-> SetTextureRect(( float )fontFrame[i][j].dwFontX, ( float )fontFrame[i][j].dwFontY, fENWidth, fHeight);
            sprFont
-> Render(fx, y);
            fx 
+=  fENWidth;
        }
        
string ++ ;
    }

}

void  hgeFontCN::printf( float  x,  float  y,  const   char   * format, ...)
{    
    
char   * pArg  =  ( char   * ) & format  +   sizeof (format);

    _vsnprintf(txtBuffer, 
sizeof (txtBuffer)  -   1 , format, pArg);

    txtBuffer[
sizeof (txtBuffer) - 1 =   0 ;

    Render(x, y, txtBuffer);
}

float  hgeFontCN::GetStringWidth( const   char   * string const
{
    
float  w  =   0 , tw  =   0 ;

    
while ( * string )
    {
        
if  ( * string   ==   ' ' )
        {
            tw 
=  max(tw, w);
            w 
=   0 string ++ ;
            
continue ;
        }
        BYTE hz0 
=   * ( string ++ ), hz1  =   * string ;

        
if  (hz0  >=  HZ_H_FIX  &&  hz0  <=  HZ_H_END)
            w 
+=  fCNWidth,  string ++ ;
        
else
            w 
+=  fENWidth;
    }

    
return  max(tw, w);
}

float  hgeFontCN::GetStringHeight( const   char   * string const
{
    
float     _h  =  fHeight;
    
while  ( * string )
    {
        
if  ( * ( string ++ ==   ' ' )
            _h 
+=  fHeight;
    }

    
return  _h;
}

怪的地方有一个.Release和Debug版的GDI字体竟然不一样..而且差别非常大.

字模的生成工具下载:hgeFontImage.execonfig.ini

注意:下载完后将ini文件更名为:config.ini

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值