I have all printable ASCII characters in this picture, excluding empty space:

I converted it to raw binary:
convert font2.png -size 1128x16 -depth 8 RGBA:font.dat
Then I wrote some code to load and display that font:
static uint8_t **pixels = NULL;
static const uint16_t font_w = 1128;
static const uint16_t font_h = 16;
static const uint16_t char_w = 12;
static int read_pixels( void )
{
FILE *fp;
int c;
size_t ch_size;
fp = fopen( "data/font.dat", "r" );
if ( !fp )
return 0;
pixels = malloc( sizeof(uint8_t*) * 95 );
ch_size = font_h * char_w * 4;
for( c=0; c<95; c++ )
{
pixels[c] = malloc( ch_size );
fseek( fp, SEEK_SET, c*ch_size );
fread( pixels[c], ch_size, 1, fp );
}
fclose( fp );
return 1;
}
int load_font( void )
{
printf( "Loading font...\n" );
if ( !read_pixels() )
return 0;
return 1;
}
void draw_text( int x, int y, char *str )
{
char *c;
int index;
for( c=str; *c; c++ )
{
if ( *c == ' ' )
{
x++;
continue;
}
if ( !isprint(*c) )
index = 32;
else
index = (*c) - 32;
if ( index < 0 || index > 127 )
index = 32;
glWindowPos2i( x++ * char_w, y );
glDrawPixels( char_w, font_h, GL_RGBA, GL_UNSIGNED_BYTE, pixels[index] );
}
}
There are 2 problems with draw_text(). It draws the text at wrong coordinates, and the text looks like this:

Above should read "Hello world!" (the space shows as green background color because it is not drawn).
Do you see the problem in my code?
[Edited by - usbman3 on December 12, 2010 10:06:10 AM]






