#include #include #include "gamefontgl.h" #include #include #include #include #include const int c_Width = 800, c_Height = 600; const char *c_displayMode = "800x600:16@60"; ILuint ilutFontId =0, ilutPictureId =0; void cbDisplay() { static bool initialized = false; static unsigned int fontId, texFont, texPicture; static int count = 0; if (!initialized) { ilutRenderer( ILUT_OPENGL ); // Load the font image ilGenImages( 1, &ilutFontId ); ilBindImage( ilutFontId ); if (!ilLoadImage( "fatmarker512.png" )) { printf("Loading image failed\n"); } texFont = ilutGLBindTexImage(); // Load the picture image ilGenImages( 1, &ilutPictureId ); ilBindImage( ilutPictureId ); if (!ilLoadImage( "bridge.jpg" )) { printf("Loading image failed\n"); } texPicture = ilutGLBindTexImage(); // Create a font by passing in an opengl texture id fontId = gfCreateFont( texFont ); // A ,finfo file contains the metrics for a font. These // are generated by the Fontpack utility. gfLoadFontMetrics( fontId, "fatmarker512.finfo"); printf("font has %d chars\n", gfGetFontMetric( fontId, GF_FONT_NUMCHARS ) ); gfEnableFont( fontId, 64 ); initialized = true; } // Clear the screen glClear( GL_COLOR_BUFFER_BIT ); // draw a 3d scene glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 90, (float)c_Width/(float)c_Height, 0.1, 10.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glBindTexture( GL_TEXTURE_2D, texPicture ); float angle = (float)count; glTranslated( 0.0, 0.0, -1.5 ); glRotated( angle, 0.0, 1.0, 0.0 ); glBegin( GL_QUADS ); glColor3f( 1.0, 1.0, 1.0 ); glBegin( GL_QUADS ); glTexCoord2d( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 0.0 ); glTexCoord2d( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 0.0 ); glTexCoord2d( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 0.0 ); glTexCoord2d( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 0.0 ); glEnd(); // Now draw some text on top. // Use gfBeginText(), gfDrawString() and gfEndText() to draw text. glColor3f( 1.0, 1.0, 1.0 ); gfEnableFont( fontId, 64 ); gfBeginText(); glPushMatrix(); glTranslated( 300, 100, 0 ); gfDrawString("Hello World!\nWhats up?" ); glPopMatrix(); // You can use gfGetStringWidth/Height to get the size of text. // Let's draw a box around "Hello World" int textW = gfGetStringWidth( "Hello World!"), textH = gfGetStringHeight( "Hello World!"); // turn off the texutre to draw some lines glColor3f( 1.0, 0.0, 0.0 ); glDisable( GL_TEXTURE_2D ); glLineWidth( 2 ); // gfBeginText() has set up 2d coordinates already. // we can draw in screen space between gfBegin/EndText() // // Our box starts at the baseline, and characters can drop // below that (especially if they have decenders, like j,y,q, etc.. // so it won't completely contain the text. // // If the box looks too tall, that's because it's using the "leading" // of the font. You can change the leading with glSetCharsetMetric(...) glBegin( GL_LINE_LOOP ); glVertex3f( 300, 100, 0 ); glVertex3f( 300 + textW, 100, 0 ); glVertex3f( 300 + textW, 100+textH, 0 ); glVertex3f( 300, 100+textH, 0 ); glEnd(); gfEndText(); // turn texture back on glEnable( GL_TEXTURE_2D ); // Scaling text allows you to approximate other font sizes. // You can change the text color with regular gl commands gfBeginText(); glColor3f( 1.0, 0.6, 1.0 ); glTranslated( 400, 500, 0 ); glScaled( 0.3, 0.4, 1.0 ); gfDrawString( "This is the gamefontgl example.\n" "glgamefont is an easy way to display\n" "text in an OpenGL program.\n" ); gfEndText(); // gfDrawStringFmt works like printf! gfBeginText(); glTranslated( 20, 300, 0 ); glColor3f( 0.0, 0.0, 1.0 ); gfDrawStringFmt( "Redraw Count %d.\n", count ); gfEndText(); glutSwapBuffers(); count++; } void cbIdle() { glutPostRedisplay(); } int main( int argc, char **argv ) { glutInitWindowPosition ( 0, 0 ) ; glutInitWindowSize( c_Width, c_Height ); glutInit( &argc, argv ) ; glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ; if ((argc==2) && (!strcmp(argv[1], "-fullscreen"))) { glutGameModeString( c_displayMode ); glutEnterGameMode(); } else { glutCreateWindow( "Font Test/Demo" ) ; } // initilize il ilInit(); glClearColor( 0.5, 0.5, 1, 1.0 ); glutDisplayFunc( cbDisplay ) ; glutIdleFunc( cbIdle ) ; // glutSpecialFunc( cbSpecial ) ; // glutMouseFunc( cbMouse ) ; glDisable(GL_DEPTH_TEST ); //glutMotionFunc ( motionfn ) ; // glutKeyboardFunc ( cbKey ) ; glutMainLoop () ; return 0; }