6 Drawing Text



Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up


Download entire guide as ODT


Part 6 – Drawing Text

In Allegro, drawing text to the screen is very easy. All you have to do is call one function and it draws it out for you. However, in SDL, you have to render text to a surface on your own.

Allegro textprintf functions

From the Allegro Quick Reference…

textprintf_ex(bmp, font, x, y, color, -1, string, …);
textprintf_centre_ex(bmp, font, x, y, color, -1, string, ...);
textprintf_right_ex(bmp, font, x, y, color, -1, string, ...);

Each of these aligns a little different. Centre will draw the center of your string of text at your x, y coordinates. In your string, you can have a %f, %i, or %s placeholder, and then include a variable name as another parameter, like this:

textprintf_ex(buffer, font, 0, 0, makecol( 255, 255, 255 ), -1, 
    “%s score: %i, %f mph”, 
    player.name, player.score, (float)(player.score/totalGameTime) );

If you want to use the default font, keep “font” as the font parameter. This outputs an 8x8px font.

For the color parameter, you’ll use the function:

makecol( r, g, b );

Where r, g, and b are from 0 to 255.

The -1 parameter is the background color. Keep this as -1 if you want it to be transparent.

© Rachel J. Morris, 2009

Print Friendly