Skip to main content
GameDev.net gamedev.net
🔒 Locked

basic font engine

Started by frenzY Oct 18, 2003 at 5:02 PM 1 replies 800+ views
Original Post
frenzY
frenzY
Here''s some code i wrote for a basic font display-engine for a little game im working on, but it doesn''t work (makes colored squares rather than actual text). If anyone could help, please. static int _GLOBAL_NUM_OF_TEXTURES = 0; struct font_point { float x, y; }; struct font_char { font_point ulCorner, urCorner, brCorner, blCorner; ABC widths; }; class h2o_text_font { public: h2o_text_font ( void ){} int texture_id; font_char chars[256]; void Init ( const char * pFontName, int pointSize, bool bold, bool italic, bool underlined, bool strikethru); void glPrint ( float x, float y, unsigned char r, unsigned char g, unsigned char b, const char * text, int justification ); int fontHeight; private: unsigned int fontListID; }; void h2o_text_font::Init ( const char * pfontName, int pointSize, bool bold, bool italic, bool underline, bool strikethru ) { HDC hDC = wglGetCurrentDC(); fontListID = glGenLists(256); HFONT hFont; ABC charWidths[256]; int i=0; if(!bold) { hFont=CreateFont(pointSize,0,0,0,FW_DONTCARE,italic,underline,strikethru,ANSI_CHARSET, OUT_TT_ONLY_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY, DEFAULT_PITCH|FF_DONTCARE,pfontName); } else { hFont=CreateFont(pointSize,0,0,0,FW_BOLD,italic,underline,strikethru,ANSI_CHARSET, OUT_TT_ONLY_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY, DEFAULT_PITCH|FF_DONTCARE,pfontName); } hOldFont = (HFONT)SelectObject(hDC, hFont); wglUseFontBitmapsA (hDC, 0, 255, fontListID); GetCharABCWidths( hDC, 0, 255, charWidths ); texture_id = _GLOBAL_NUM_OF_TEXTURES++; //build font into BITMAP and use it for texturing. //get pixelheight TEXTMETRIC textMetrics; GetTextMetrics(hDC, &textMetrics); fontHeight = textMetrics.tmHeight; //calculate bitmap area: int textSpace; for( i = 0; i < 256; i++ ) { textSpace += charWidths.abcA; textSpace += charWidths[i].abcB; textSpace += charWidths[i].abcC; } int bitmapWidth = textSpace; int bitmapHeight = fontHeight; int bitmapArea = bitmapWidth * bitmapHeight; //create the bitmap BITMAPINFO bmi; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = bitmapWidth; bmi.bmiHeader.biHeight = bitmapHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; char * pBits = NULL; HBITMAP hBMP = CreateDIBSection( hDC, &bmi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); HGDIOBJ oldBMP = SelectObject( hDC, hBMP ); //now render to the bitmap SetTextColor( hDC, (COLORREF)0x00FFFFFF ); SetBkColor( hDC, (COLORREF)0x00000000 ); PatBlt( hDC, 0, 0, bitmapWidth, bitmapHeight, BLACKNESS); int curX = 0, curY = 0; char temp[4]; temp[1] = 0; for(i = 0; i < 256; i++) { TextOut (hDC, curX, curY, temp, 1); //assign abc widths to char chars[i].widths = charWidths[i]; //generate texture coords curX += chars[i].widths.abcA; chars[i].ulCorner.x = (float)curX; chars[i].ulCorner.y = (float)curY; chars[i].blCorner.x = (float)curX; chars[i].blCorner.y = (float)curY + bitmapHeight; curX += chars[i].widths.abcB; chars[i].urCorner.x = (float)curX; chars[i].urCorner.y = (float)curY; chars[i].brCorner.x = (float)curX; chars[i].brCorner.y = (float)curY + bitmapHeight; curX += chars[i].widths.abcC; } unsigned char *pOut = (unsigned char *)pBits; for (i = 0; i < bitmapArea; i++) { if(pOut[0] || pOut[1] || pOut[2] ) { //filled pOut[3] = 255U; } else { //empty pOut[3] = 0; } pOut += 4; } glBindTexture( GL_TEXTURE_2D, texture_id ); glTexImage2D ( GL_TEXTURE_2D, 0, 4, bitmapWidth, bitmapHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); SelectObject( hDC, oldBMP ); SelectObject( hDC, hOldFont ); DeleteObject( hFont ); DeleteObject( hBMP ); } void h2o_text_font::glPrint ( float x, float y, unsigned char r, unsigned char g, unsigned char b, const char * text, int justification) { //disregard justification, it is supposed to be 0=left, 1=centered, 2=right, it''s here for future inclusion font_char *pLetter; unsigned char letter; glColor3ub( r, g, b ); glEnable( GL_ALPHA_TEST ); glBindTexture ( GL_TEXTURE_2D, texture_id ); do { letter = *text++; pLetter = chars + letter; if ( letter ) { x += pLetter->widths.abcA; glBegin( GL_QUADS ); glTexCoord2f( pLetter->blCorner.x, pLetter->blCorner.y ); glVertex2f( x, y ); glTexCoord2f( pLetter->ulCorner.x, pLetter->ulCorner.y ); glVertex2f( x, y + fontHeight ); glTexCoord2f( pLetter->urCorner.x, pLetter->urCorner.y ); glVertex2f( x + pLetter->widths.abcB, y + fontHeight ); glTexCoord2f( pLetter->brCorner.x, pLetter->brCorner.y ); glVertex2f( x + pLetter->widths.abcB, y ); glEnd(); x += pLetter->widths.abcC; } } while( letter ); glDisable( GL_ALPHA_TEST ); glEnable( GL_TEXTURE_2D ); } i''m kinda new, so dont go too hard on me :x (BTW, this system worked before i added the bitmap stuff code for less fps dragdown...)
frenzY
frenzY
if you could, please email me at justin2006@swbell.net if you have a solution that will work.. i really need this text system :x

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.