游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1195|回复: 0

opengl下可以正确显示汉字,但是却无法定义颜色?

[复制链接]

1

主题

3

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2006-3-9 03:01:00 | 显示全部楼层 |阅读模式
可以显示出汉字,但是颜色无论怎么设置也还是黑色,请高手指教,贴出相关代码如下:


HDC hMemDC = NULL;
HDC hDC = NULL;

bool ColorSelected = false;
bool Drawed = true;

float glR, glG, glB;

void CreateBitmap(BITMAP *bmp, LONG width, LONG height)
{
   FillMemory(bmp, sizeof(*bmp), 0);
   bmp->bmWidth = width;
   bmp->bmHeight = height;
   bmp->bmWidthBytes = ((width + 7) / 8 + 1) & (~1);
   bmp->bmPlanes = 1;
   bmp->bmBitsPixel = 1;
   bmp->bmBits = GlobalAlloc(GMEM_FIXED, bmp->bmWidthBytes*height);
}
unsigned char *GetBitmapBits(HDC hDC, HBITMAP hBmp, int* size)
{
   BITMAP bi;
   unsigned char   *bits;
   struct {
       BITMAPINFOHEADER bih;
       RGBQUAD col[2];
   }bic;

   BITMAPINFO *binf = (BITMAPINFO *)&bic;
   GetObject(hBmp, sizeof(bi), &bi);
   *size = bi.bmHeight*(((bi.bmWidth + 31) & (~31)) / 8);
   bits = (unsigned char *)GlobalAlloc(GPTR, *size);
   binf->bmiHeader.biSize = sizeof(binf->bmiHeader);
   binf->bmiHeader.biWidth = bi.bmWidth;
   binf->bmiHeader.biHeight = bi.bmHeight;
   binf->bmiHeader.biPlanes = 1;
   binf->bmiHeader.biBitCount = 1;
   binf->bmiHeader.biCompression = BI_RGB;
   binf->bmiHeader.biSizeImage = *size;
   binf->bmiHeader.biXPelsPerMeter = 1;
   binf->bmiHeader.biYPelsPerMeter = 1;
   binf->bmiHeader.biClrUsed = 0;
   binf->bmiHeader.biClrImportant = 0;
   GetDIBits(hDC, hBmp, 0, bi.bmHeight, bits, binf, DIB_RGB_COLORS);
   return bits;
}
unsigned char *CreateFontBitmap(HDC hDC, HFONT hFont, char *str, PSIZEL size)
{
   BITMAP  bmp;
   HBITMAP hbmp;
   unsigned char    *pbm;
   int     len = lstrlen(str);
   
   HFONT   hFontOld = (HFONT)SelectObject(hDC, hFont);

   GetTextExtentPoint32(hDC, str, len, size);
   
   CreateBitmap(&bmp, size->cx, size->cy);
   hbmp = CreateBitmapIndirect(&bmp);
   
   GlobalFree(bmp.bmBits);
   DeleteObject(&bmp);

   if(hbmp && hMemDC)
   {
     HBITMAP hPrevBmp = (HBITMAP)SelectObject(hMemDC, hbmp);
     HFONT   hPrevFont;
     int     size0;
     BITMAP  bi;

     SetBkColor(hMemDC, RGB(0,0,0));
     SetTextColor(hMemDC, RGB(255,255,255));
     SetBkMode(hMemDC, OPAQUE);

     hPrevFont = (HFONT)SelectObject(hMemDC, hFont);

     TextOut(hMemDC, 0, 0, str, len);
     GetObject(hbmp, sizeof(bi), &bi);

     pbm = GetBitmapBits(hMemDC, hbmp, &size0);

     size->cx = ((bi.bmWidth+31)&(~31));
     size->cy = bi.bmHeight;

         GlobalFree(bi.bmBits);
     DeleteObject(&bi);

         DeleteObject(hPrevFont);
         DeleteObject(hPrevBmp);
         DeleteObject(hbmp);
   }
   SelectObject(hDC, hFontOld);

   return pbm;
}
int DrawStringFont(int x,int y,char *str)
{
   LOGFONT lf;
   HFONT   hFont;
   unsigned char *lpBitmap;
   SIZE size;

   if(!hDC)
   {
         hDC = wglGetCurrentDC();
         hMemDC = CreateCompatibleDC(hDC);
   }

   FillMemory(&lf, sizeof(lf), 0);
   lf.lfHeight = 10;
   lf.lfCharSet = DEFAULT_CHARSET;
   lf.lfWeight = FW_BOLD;
   lf.lfOutPrecision = OUT_STROKE_PRECIS;
   lf.lfClipPrecision = CLIP_CHARACTER_PRECIS;
   lstrcpy(lf.lfFaceName, "MS Sans Serif");

   hFont = CreateFontIndirect(&lf);
   GlobalFree(&lf);

   lpBitmap = CreateFontBitmap(hDC, hFont, str, &size);

   DeleteObject(hFont);

   glColor3f(0,0,0);
   glRasterPos2f((float)x + 1, (float)y + 1 + ADJUST_HEIGHT);
   glBitmap(size.cx, size.cy, 0.0, 0.0, 0.0, 0.0, lpBitmap);

   if (ColorSelected)
   {
         ColorSelected = false;
     glColor3f(glR, glG, glB);
   }
   else
   {
     glColor3f(1.0, 1.0, 1.0);
   }

   glRasterPos2f((float)x, (float)y + ADJUST_HEIGHT);
   glBitmap(size.cx, size.cy, 0.0, 0.0, 0.0, 0.0, lpBitmap);

   GlobalFree(lpBitmap);

   Drawed = true;
   return x + size.cx;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-23 21:14

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表