|
|
发表于 2006-8-24 10:03:00
|
显示全部楼层
Re: 中文显示速度的问题
用位图字体应该可以解决你的难题,我的程序里面一直用这个,挺好用。基本思想就是用Windows里的字体输出字符串到内存位图,然后再用用这个位图贴到你想要显示字符串的地方。
代码实例:
注意字体最好在2D模式下显示。用GLuOrtho2D 投影。
void CGLFont::print(float xPosition, float yPosition, //位置
float r, float g, float b,
const char* string)
{
HBITMAP hbm;
SIZE size;
glPushMatrix();
glColor3f(r,g,b);
HDC MDC = ::CreateCompatibleDC(0);
HFONT originFont=CreateFont(-10, 6,0,0,400,0,0,0,GB2312_CHARSET,0,0,0,FF_MODERN,"宋体");
SelectObject(MDC,originFont);
::GetTextExtentPoint32(MDC,(LPCSTR)string, strlen(string),&size);
hbm=CreateBitmap(size.cx, size.cy, 1, 1, NULL);
HBITMAP oldBmp=(HBITMAP)SelectObject(MDC,hbm);
SetBkColor (MDC, RGB(0, 0, 0));
SetTextColor(MDC, RGB(255, 255, 255));
TextOut(MDC, 0, 0, ,(LPCSTR)string, strlen(string));
size.cx = (size.cx + 31) & (~31);
int bufsize =size.cy * size.cx;
struct { BITMAPINFOHEADER bih;
RGBQUAD col[2];
}bic;
BITMAPINFO *binf = (BITMAPINFO *)&bic;
binf->bmiHeader.biSize = sizeof(binf->bmiHeader);//
binf->bmiHeader.biWidth = size.cx;
binf->bmiHeader.biHeight = size.cy;
binf->bmiHeader.biPlanes = 1;
binf->bmiHeader.biBitCount = 1;
binf->bmiHeader.biCompression = BI_RGB;
binf->bmiHeader.biSizeImage = bufsize;
UCHAR* Bits = new UCHAR[bufsize];
::GetDIBits(MDC,hbm,0,size.cy,Bits,binf,DIB_RGB_COLORS);
glPixelStorei(GL_UNPACK_ALIGNMENT ,1);
glRasterPos2f(xPosition,yPosition);
glBitmap(size.cx,size.cy,0,0,0,0,Bits);
deleteArray(Bits);
SelectObject(MDC, oldBmp);
: eleteDC(MDC);
glColor3i(1,1,1);
glPopMatrix();
if(hbm) DeleteObject(hbm);
} |
|