hgeFontCN.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:5k
- #include "hgefontcn.h"
- #include <stdlib.h>
- #include <stdio.h>
- HGE *hgeFontCN::hge=0;
- hgeFontCN::hgeFontCN(void)
- {
- }
- hgeFontCN::~hgeFontCN(void)
- {
- for(int i=0; i<HZ_HIGH_MAX; i++)
- {
- for(int j=0; j<HZ_LOW_MAX; j++)
- {
- if(letters[i][j])
- delete letters[i][j];
- }
- }
- if(hTexture)
- hge->Texture_Free(hTexture);
- hge->Release();
- }
- //根据字体文件创建
- hgeFontCN::hgeFontCN(const char *filename)
- {
- int tex_width; //纹理宽度
- int char_width; //字符宽度
- float texx; //纹理 X
- float texy; //纹理 Y
- hge=hgeCreate(HGE_VERSION);
- fScale=1.0f;
- fRot=0.0f;
- fTracking=0.0f;
- hTexture=0;
- fZ=0.5f;
- nBlend=BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE;
- dwCol=0xFFFFFFFF;
- st_FontCN_Head fonthead;
- FILE *pFontFile = fopen(filename,"r");
- if (pFontFile)
- {
- size_t filesize = 0;
- filesize = fread(&fonthead,1,sizeof(st_FontCN_Head),pFontFile);
- if (filesize != sizeof(st_FontCN_Head))
- {
- fclose(pFontFile);
- return;
- }
- // 文件头不正确
- if(strcmp(fonthead.strHead,"[HGEFONTCN]"))
- {
- fclose(pFontFile);
- return;
- }
- // 获得字模文件名
- char *imgfile = fonthead.strImgFileName;
- 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,imgfile);
- // 加载指定字模图片
- hTexture = hge->Texture_Load(szFullImgFile);
- if(!hTexture)
- {
- fclose(pFontFile);
- return;
- }
- // 获得纹理宽度
- tex_width=hge->Texture_GetWidth(hTexture);
- // 获得字符宽度
- char_width = fonthead.dwFontWidth;
- fHeight = fonthead.dwFontHeight;
- ZeroMemory( &letters, sizeof(letters) );
- texx=texy=0.0f;
- filesize = 0;
- while(!feof(pFontFile))
- {
- st_FontCN_Body fontbody;
- filesize = fread(&fontbody,1,sizeof(st_FontCN_Body),pFontFile);
- if (filesize != sizeof(st_FontCN_Body))
- break;
- BYTE hz0 = fontbody.HZ[0];
- BYTE hz1 = fontbody.HZ[1];
- texx = (float)(fontbody.dwFontX);
- texy = (float)(fontbody.dwFontY);
- float char_w = char_width;
- // 读取汉字字模
- if(hz0 >= HZ_HIGH_FIX && hz0 <= HZ_HIGH_END)
- {
- hz0 -= HZ_HIGH_FIX;
- if(hz1 >= HZ_LOW_FIX && hz1 <= HZ_LOW_END)
- hz1 -= HZ_LOW_FIX;
- else
- continue;
- }
- // 读取标准ASCII字模
- else if (hz0 == 0xff)
- {
- hz0 = HZ_HIGH_MAX - VALUE_FIX;
- if(hz1 >= ASCII_FIX && hz1 < ASCII_END)
- hz1 -= ASCII_FIX;
- else
- continue;
- char_w = ASCII_WIDTH; //标准ASCII字模宽度
- }
- // 如果不是汉字也不是标准ASCII字模就读下一个
- else
- continue;
- letters[hz0][hz1] = new hgeSprite(hTexture, texx, texy, (float)char_w, fHeight);
- }
- fclose(pFontFile);
- }
-
- }
- //根据字体对象创建
- hgeFontCN::hgeFontCN(const hgeFontCN &fnt)
- {
- }
- //绘制字符串
- void hgeFontCN::Render(float x, float y, const char *string)
- {
- float fx=x;
- while(*string)
- {
- BYTE hz0 = *string;
- BYTE hz1 = *(string+1);
- // 遇到回车
- if(*string=='n')
- {
- y+=fHeight*fScale;
- fx=x;
- }
- // 遇到汉字
- else if(hz0 >= HZ_HIGH_FIX && hz0 <= HZ_HIGH_END)
- {
- letters[hz0 - HZ_HIGH_FIX][hz1 - HZ_LOW_FIX]->RenderEx(fx, y, fRot, fScale);
- fx+=(letters[hz0 - HZ_HIGH_FIX][hz1 - HZ_LOW_FIX]->GetWidth()+fTracking)*fScale;
- string++;
- }
- // 遇到英文
- else if(hz0 >= ASCII_FIX && hz0 < ASCII_END)
- {
- letters[HZ_HIGH_MAX - VALUE_FIX][hz0 - ASCII_FIX]->RenderEx(fx, y, fRot, fScale);
- fx+=(letters[HZ_HIGH_MAX - VALUE_FIX][hz0 - ASCII_FIX]->GetWidth()+fTracking)*fScale;
- }
- else
- {
- letters[HZ_HIGH_MAX - VALUE_FIX]['?']->RenderEx(fx, y, fRot, fScale);
- fx+=(letters[HZ_HIGH_MAX - VALUE_FIX]['?']->GetWidth()+fTracking)*fScale;
- }
- string++;
- }
- }
- //绘制格式字符串
- void hgeFontCN::printf(float x, float y, const char *format, ...)
- {
- char buffer[1024];
- char *pArg=(char *) &format+sizeof(format);
- vsprintf(buffer, format, pArg);
- Render(x,y,buffer);
- }
- //设置字体颜色
- void hgeFontCN::SetColor(DWORD col)
- {
- dwCol=col;
- for(int i=0;i<HZ_HIGH_MAX;i++)
- {
- for(int j=0;j<HZ_LOW_MAX;j++)
- {
- if(letters[i][j])
- letters[i][j]->SetColor(col);
- }
- }
- }
- //设置Z缓冲
- void hgeFontCN::SetZ(float z)
- {
- fZ=z;
- for(int i=0;i<HZ_HIGH_MAX;i++)
- {
- for(int j=0;j<HZ_LOW_MAX;j++)
- {
- if(letters[i][j])
- letters[i][j]->SetZ(z);
- }
- }
- }
- //设置混和模式
- void hgeFontCN::SetBlendMode(int blend)
- {
- nBlend=blend;
- for(int i=0;i<HZ_HIGH_MAX;i++)
- {
- for(int j=0;j<HZ_LOW_MAX;j++)
- {
- if(letters[i][j])
- letters[i][j]->SetBlendMode(blend);
- }
- }
- }
- //获得字符串的宽度(汉字按两个字节、字母按照一个字节算)
- float hgeFontCN::GetStringWidth(const char *string) const
- {
- float w = 0;
- while(*string && *string!='n')
- {
- BYTE hz0 = (unsigned char)*string;
- BYTE hz1 = (unsigned char)*(string+1);
- // 遇到汉字
- if(hz0 >= HZ_HIGH_FIX && hz0 <= HZ_HIGH_END)
- {
- w += letters[hz0 - HZ_HIGH_FIX][hz1 - HZ_LOW_FIX]->GetWidth() + fTracking;
- string++;
- }
- // 遇到英文
- else if(hz0 >= ASCII_FIX && hz0 < ASCII_END)
- {
- w += letters[HZ_HIGH_MAX - VALUE_FIX][hz0 - ASCII_FIX]->GetWidth() + fTracking;
- }
- else
- {
- w += letters[HZ_HIGH_MAX - VALUE_FIX]['?']->GetWidth() + fTracking;
- }
- string++;
- }
- return w * fScale;
- }
- //获得指定的精灵
- hgeSprite* hgeFontCN::GetSprite(BYTE chH,BYTE chL) const
- {
- if (chH < HZ_HIGH_MAX && chL < HZ_LOW_MAX)
- return letters[chH][chL];
- return NULL;
- }