Using Bitmap Fonts in your game
Loading a font
To load a font you should just create a hgeFont
object and pass the font file name to it's constructor:
hgeFont *fnt;
fnt = new hgeFont("font1.fnt");
Alternatively you may want to describe a font within
resource script and use
hgeResourceManager to load it.
Rendering text
The simplest way to render a text is hgeFont::Render method.
You should just specify an anchor point on the screen, text alignment mode and the string to be rendered:
fnt->Render(20, 20, HGETEXT_LEFT, "Hello, world!");
You may use '\n' character in the string to indicate line breaks:
fnt->Render(20, 20, HGETEXT_LEFT,
"Hello, Peter!\nLet's meet in 2012.");
A more advanced hgeFont::printf methods allows to render complex formatted strings
with variables in a usual C-like syntax:
char *player;
int score;
fnt->printf(780, 10, HGETEXT_RIGHT,
"Player:%s Score:%d", player, score);
The most powerful method is hgeFont::printfb. It has all functionality
of printf plus it can automatically format the string into a rectangle on the screen.
int level;
fnt->printfb(200, 200, 400, 100, HGETEXT_CENTER | HGETEXT_MIDDLE,
"Level:%d\nOnce upon a time in a galaxy far away..."
"There was a great hero of unbelievable strength.", level);
Adjusting text appearance
You may alter the font appearance with
SetColor,
SetBlendMode,
SetScale,
SetRotation,
SetTracking,
SetSpacing
methods:
fnt->SetColor(0x80FFFF00);
fnt->SetBlendMode(BLEND_DEFAULT);
fnt->SetScale(1.3f);
fnt->SetRotation(0.1f);
fnt->SetTracking(8.0f);
fnt->SetSpacing(1.4f);
Clean up
When the font isn't needed longer, you must delete it:
delete fnt;
If the font was loaded with hgeResourceManager you must NOT delete it.
Leave this to the resource manager.
See also
hgeFont,
hgeResourceManager,
Bitmap Font Builder,
Tutorial 03
|