CPPGPGPU Library - Reference (Doxygen)

Main Page | General Use | Reference | Examples Info | Get/Download CPPGPGPU | SF.net Page

TextNode.cpp

Go to the documentation of this file.
00001 #include "TextNode.h"
00002 #include <math.h>
00003 
00004 void TextNode::SetText( const string & sText )
00005 {
00006     int olWidth = 10 * sText.length();
00007     float ftLog = logf( float(olWidth) ) / logf(2.f);
00008     int lWidth = (int)powf( 2.f, ceilf( ftLog ));
00009     int lHeight = 16;
00010     unsigned int * OutData = (unsigned int*)malloc( lWidth * lHeight * 4 );
00011     for( unsigned i = 0; i < sText.length(); ++i )
00012     {
00013         int xofs = (sText.c_str()[i]%16)*16+1;
00014         int yofs = (sText.c_str()[i]/16)*16;
00015         for( int y = 0; y < 16; ++y )
00016             for( int x = 0; x < 11; ++x )
00017             {
00018                 OutData[(x+(i*11))+y*lWidth] = FontMapData[(x+xofs+1)+(y+yofs)*FontWidth];
00019             }
00020     }
00021     Lock();
00022     //If we're double-updating, clear out the old stuff.
00023     if( NewData )
00024         free( NewData );
00025     NewData = (unsigned char*)OutData;
00026     Width = lWidth;
00027     RealWidth = olWidth;
00028     Height = lHeight;
00029     Unlock();
00030 }
00031 
00032 void TextNode::Render()
00033 {
00034     if( NewData )
00035     {
00036         Lock();
00037         //Tricky, we don't want to lock and unlock each time, but we have to make sure the case is still
00038         //true when locked.
00039         if( NewData )
00040         {
00041             T.LoadTexture( (char*)NewData, Width, Height, TTRGBA );
00042             free( NewData );
00043             NewData = 0;
00044         }
00045         Unlock();
00046     }
00047 
00048     T.ActivateTexture(0);
00049     glBegin( GL_QUADS );
00050     glTexCoord2f( 0.0, 1.0 );
00051     glVertex2f( -RealWidth/2+m_x, -8+m_y );
00052     glTexCoord2f( 0.0, 0.0 );
00053     glVertex2f( -RealWidth/2+m_x, 8+m_y );
00054     glTexCoord2f( float(RealWidth)/float(Width), 0.0 );
00055     glVertex2f( RealWidth/2+m_x, 8+m_y );
00056     glTexCoord2f( float(RealWidth)/float(Width), 1.0 );
00057     glVertex2f( RealWidth/2+m_x, -8+m_y );
00058     glEnd();
00059     T.DeactivateTexture(0);
00060 }
00061 
00062 void TextNode::CheckAndLoadFontData()
00063 {
00064     //This code doesn't have to be all that fast since it runs once ever.
00065     if( FontMapData )
00066         return;
00067 
00068     unsigned char * RawLoadedData;
00069     OpenPPM( "font.ppm", &RawLoadedData, &FontHeight, &FontWidth );
00070     FontMapData = (unsigned int*)malloc( FontWidth * FontHeight * 4 );
00071 
00072 //printf( "%d  %d %p %p %d\n", FontHeight, FontWidth, FontMapData, RawLoadedData, sizeof( unsigned int ));
00073     //Now, we have to transpose the data into FontData.
00074     for( int y = 0; y < FontHeight; y++ )
00075         for( int x = 0; x < FontWidth; x++ )
00076         {
00077             //printf( "%d %d %d\n", x, y, x+y*FontWidth );
00078             FontMapData[x+y*FontWidth] = 
00079                 ((unsigned int)RawLoadedData[(x+y*FontWidth)*3+0])+
00080                 ((unsigned int)RawLoadedData[(x+y*FontWidth)*3+1]<<8) +
00081                 ((unsigned int)RawLoadedData[(x+y*FontWidth)*3+1]<<16) +
00082                 ((255-(unsigned int)RawLoadedData[(x+y*FontWidth)*3+2])<<24);
00083         }
00084 
00085     free( RawLoadedData);
00086 
00087 }
00088 
00089 unsigned int * TextNode::FontMapData = 0;
00090 int TextNode::FontHeight;
00091 int TextNode::FontWidth;
00092 
00093 REGISTER_CLASS( TextNode );

© 2005-2007 Charles Lohr, Joshua Allen, David Chapman, Andrew Lohr. All material including documentation under the MIT/X11 license.