Continuing on from my Chicken Scheme, and OpenGL tutorial, I'm going to show you how to extend it to render pretty FreeType text using the FTGL library. Follow the code below for details as always; it's really easy! :)
Users of ArchLinux can get FTGL with a simple
sudo pacman -S ftgl. Debian should be something like
sudo apt-get install libftgl-dev.
Alright lets start by loading up the code from last time ...
... and scrolling down to the "(inline .." call:
(inline #<<EOF #ifndef CHICKEN ... #endif // Here we're going to add the new stuff for FTGL! // Define a constant we're going to use later const int FTGL_RENDER_ALL = 0xffff; // And typedef FTGLfont as void so we can use it and pass it around typedef void FTGLfont; // Notice however that I didn't include the ftgl header, because I // want to define just the functions we're going to use to make // things even easier FTGLfont* ftglCreatePixmapFont(const char*); void ftglSetFontFaceSize(FTGLfont*, int, int); void ftglRenderFont(FTGLfont*, const char*, int); void ftglDestroyFont(FTGLfont*); // The above are the only ones we will be using for now, but these // ones below may also be useful, along with the SimpleLayout class // Also possible to use ftglCreateTextureFont and // render as a plain texture too! Using the same render call //FTGLfont* ftglCreatePixmapFont(const char*); //float ftglGetFontLineHeight(FTGLfont*); //void ftglGetFontBBox(FTGLfont*, const char*, int, float*); // Done!
... Now skip down to the EOF part:
EOF "-lSOIL -lftgl" ; Notice here we have added -lftgl to be linked )
... Now skip down to our old draw-text function, where we're going to replace the GLUT code with our new FTGL render code:
(define (draw-text text x y #!key (color '(1 1 1 1))) (apply gl:Color4f color) (gl:PushAttrib gl:ENABLE_BIT) (gl:Disable gl:TEXTURE_2D) (gl:RasterPos2f x y) ; This line has been replaced with a new one for FTGL ; FTGL even takes care of rendering for us! (ftglRenderFont font text FTGL_RENDER_ALL) (gl:PopAttrib))
... and down a bit further to where we loaded the texture last time:
(define texture (LoadGLTexture "circle_gradient.tga")) ; And add a new line for loading the font up (define font (ftglCreatePixmapFont "pathtoyourfont.ttf")) ; with minimal error checking (unless font (error "Couldn't load font!") (exit 1)) ; now set the font size (ftglSetFontFaceSize font 36 36)
... Nearly done! down to the last line now:
(glut:MainLoop) ; Add a call to destroy the font after we're done (ftglDestroyFont font)
And that's it! Ah the joys of libraries and high level languages! If all went well you should have sweet anti-aliased variable-pitch text in place of the standard GLUT stuff before :D