|
发表于 2004-6-11 12:51:00
|
显示全部楼层
Re:OPENGL中的汉字显示问题
class CFont
{
public:
CFont();
~CFont();
void Die();
//中文字符支持
void CreateChineseFont(HWND hWnd,HDC hdc,char * fontname,unsigned int fontSize, bool bold);
void CreateTrueTypeTexture( char* filename );
void CreateTrueType(char *fontname = "Verdana",
unsigned int fontSize = 16,
bool bold = false,
bool italic = false,
bool underline = false,
bool strikeout = false,
bool antialiased = true );
//输出函数
void Print( int x, int y, float size, const char* text,... );
void Print( float x, float y, float size, const char* text,... );
void PrintW(const char* text);
void SetHorizontalAligment( FontAligment aligment );
void SetVerticalAligment( FontAligment aligment );
void SetPosition( FontPosition position );
int GetLength( char* line ); // length
CTexture texture;
int fontheight;
private:
GLuint fontList;
FontPosition position;
FontAligment horizonatlAligment;
FontAligment verticalAligment;
int charWidth[256]; //保存每个字符宽度的数组
bool loaded;
//为支持中文字符
//#ifdef _CHINESE
HDC m_hDC;
HFONT m_hFont;
bool m_bUnicode;
GLYPHMETRICSFLOAT gmf[256];
map<DWORD,WORD> m_fontMap;
valarray<WORD> chTable;
//#endif
};
void CFont::CreateChineseFont(HWND hWnd,HDC hdc,char *fontname, unsigned int fontSize, bool bold)
{
Zero_log.Add(" - Try to Support UNICODE fonts from %s\n", fontname);
if (!IsDBCSLeadByte(fontname[0])){//UNICODE
m_bUnicode=false;
Zero_log.Warning("Is't Unicode font name!!");
return;
}
chTable.resize(256);
m_fontMap.clear();
m_bUnicode=true;
int x, y;
unsigned int fontPadding = 2;
fontSize += 2;
//m_hDC=hdc;
m_hDC=GetDC(hWnd);
if((fontList=glGenLists( 256 ))==0)
{
Zero_log.Error("Cannot Gen dl\n");
return;
}
LOGFONT lf;
memset( &lf, 0, sizeof(LOGFONT) );
lf.lfEscapement = lf.lfOrientation = 0;
lf.lfWeight = bold?FW_BOLD:FW_NORMAL;
lf.lfCharSet = GB2312_CHARSET;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
lf.lfQuality=DEFAULT_QUALITY; //ANTIALIASED_QUALITY : NONANTIALIASED_QUALITY
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
strcpy( lf.lfFaceName, LPCTSTR(fontname) );
lf.lfHeight=-10;
lf.lfWidth=0;
m_hFont=CreateFontIndirect(&lf);
if ( m_hFont == NULL )
{
Zero_log.Error("Cannot create font object\n");
ReleaseDC( hWnd, m_hDC );
return;
}
SelectObject( m_hDC, m_hFont );
wglUseFontBitmaps(m_hDC, 0, 256, fontList);
}
void CFont: rintW(const char *text)
{
GLsizei size =MultiByteToWideChar(CP_ACP,0,LPCSTR(text),-1,NULL,0);
int i=0,j=0;
while(i<strlen(text))
{
DWORD dwChar=text;
if (IsDBCSLeadByte(text)){
dwChar= (dwChar<<8) + text[i+1]+256; //256 offset of chinese char
i+=2;
}
else{
dwChar=text;
i++;
}
//
if(m_fontMap.find(dwChar)==m_fontMap.end())//未找到
{
wglUseFontOutlines(m_hDC, dwChar, 1, fontList+m_fontMap.size(),
0.2f, 0.0f, WGL_FONT_POLYGONS, &gmf[m_fontMap.size()]);
m_fontMap[dwChar]=m_fontMap.size();
if(m_fontMap.size()>256)
{
return;
}
}
chTable[j++]= m_fontMap[dwChar];
}
glListBase(fontList);
glCallLists(size, GL_UNSIGNED_SHORT , (const GLvoid*)&chTable[0]);
glPopMatrix();
} |
|