Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Font::Init( std::string fontname, int fontsize, int fontweight, int charsetstart, int charsetend )
- {
- //:: Initialize stuff
- HDC fntdc = CreateCompatibleDC( GetDC( 0 ) );
- HFONT fntobj = CreateFont( -fontsize, 0, 0, 0, fontweight,
- FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
- ANTIALIASED_QUALITY, DEFAULT_PITCH, fontname.c_str( ) );
- SelectObject( fntdc, fntobj );
- TEXTMETRIC metrics;
- GetTextMetrics( fntdc, &metrics );
- mCharHeight = metrics.tmHeight;
- int mCharSizeX = metrics.tmMaxCharWidth;
- int mCharSizeY = metrics.tmHeight;
- // Get all char widths.
- for( int i = 0; i < 256; i++ )
- {
- if( i == '\t' ) {
- GetCharWidth32( fntdc, ' ', ' ', &mChars[i].width );
- mChars[i].x = mChars[i].y = 0;
- mChars[i].width *= 4;
- mChars[i].flags = 2;
- } else if( i == '\n' ) {
- mChars[i].x = mChars[i].y = 0;
- mChars[i].width = 0;
- mChars[i].flags = 2;
- } else if (i == ' ') {
- mChars[i].x = mChars[i].y = 0;
- GetCharWidth32( fntdc, i, i, &mChars[i].width );
- mChars[i].flags = 2;
- } else if( (i >= charsetstart && i <= charsetend) || i == '?' ) {
- GetCharWidth32( fntdc, i, i, &mChars[i].width );
- mChars[i].flags = 1;
- } else {
- mChars[i].x = mChars[i].y = 0;
- mChars[i].width = 0;
- mChars[i].flags = 0;
- }
- }
- int mTexWidth = 0;
- int mTexHeight = 0;
- for (int size = 64; size <= 2048; size *= 2) {
- bool fit = true;
- for (int i = charsetstart, ix = 0, iy = mCharSizeY; i <= charsetend; i++) {
- ix += mChars[i].width;
- if (ix > size) {
- ix = mChars[i].width;
- iy += mCharSizeY;
- }
- if (iy > size) {
- fit = false;
- break;
- }
- }
- if (fit) break;
- }
- mTexWidth = mTexHeight = size;
- HBITMAP fntbmp = CreateCompatibleBitmap( GetDC( 0 ), mTexWidth, mTexHeight );
- SelectObject( fntdc, fntbmp );
- BitBlt( fntdc, 0, 0, mTexWidth, mTexHeight, 0, 0, 0, WHITENESS );
- for (int i = charsetstart, ix = 0, iy = 0; i <= charsetend; i++) {
- {
- if( ix + mChars[i] > mTexWidth ) {
- ix = 0;
- iy += mCharSizeY;
- }
- mChars[i].x = ix;
- mChars[i].y = iy;
- ix += mChars[i].width;
- RECT rect;
- rect.left = mChars[i].x; rect.right = rect.left + mCharSizeX;
- rect.top = mChars[i].y; rect.bottom = rect.top + mCharSizeY;
- DrawText( fntdc, (char*)&i, 1, &rect, DT_LEFT | DT_NOPREFIX );
- }
- unsigned int picount = mTexWidth * mTexHeight;
- unsigned int* data = new unsigned int[ picount ];
- int rval = GetBitmapBits( fntbmp, picount * 4, data );
- //:: Create real texture!
- mTex = new Halia::Texture( mTexWidth, mTexHeight, Halia::TextureFormat::A8L8 );
- unsigned short* texbuf = (unsigned short*)mTex->GetBuffer( );
- for( unsigned int i = 0; i < picount; i++ )
- *texbuf++ = ~( data[i] << 8 ) | 0x00FF;
- delete data;
- //:: Clean shit up!
- DeleteObject( fntobj );
- DeleteObject( fntbmp );
- DeleteObject( fntdc );
- return true;
- };
- void Font::DrawString( int x, int y, std::string text ) const
- {
- int ix = x;
- int iy = y;
- for( std::string::iterator i = text.begin( ); i != text.end( ); i++ )
- {
- char& c = *i;
- if (c == '\n') {
- ix = x;
- iy += mCharHeight;
- } else if( mChars[c].flags == 2 ) {
- ix += mChars[ c ].width;
- } else {
- if( mChars[c].flags == 0 ) {
- c = '?';
- HRS->DrawSprite(
- ix,
- iy,
- mChars[ c ].width,
- mCharHeight,
- mChars[ c ].x,
- mChars[ c ].y,
- mChars[ c ].width,
- mCharHeight,
- mTex,
- 0xFFFF0000
- );
- ix += mChars[ c ].width;
- }
- }
- };
Add Comment
Please, Sign In to add comment