Help those suffering in the Horn of Africa

VSFontLib in Action

Prev: Requirements Next: Specification
 

To start working with VSFontLib declare a variable of type VSFontLib such as:

VSFontLib vsfl;

Class VSFontLib stores the information and strings for a particular font. So if you want to work with more than one font you’ll need to declare as many variables.

A font is defined by a pair of files. An image, such as the one below, that stores a set of characters (in FontStudio you can select the range of characters to include), and a XML file with the texture coordinates and font definition data. In VSFontLib it is assumed that these files have the same name and extensions tga and xml respectively, so for instance when working with font Arial size 10, the files could be arial10.tga and arial10.xml. Below is a snapshot of arial10.tga (not the actual tga though, go to here to download the font files).

To load a font into VSFontLib do as follows:

vsfl.loadFont("arial10");

Some fonts, such as Courier New, can be treated as fixed size fonts. When loading a font it is possible to specify if the font is to be used as fixed size or not. To use it as fixed size just write:

vsfl.loadFont("couriernew10");
vsfl.setFixedSize(true);

To set the semantics of material uniforms this lib uses the method from the super class VSResourceLib:

vsfl.setMaterialBlockName("Material");

OK, now everything is ready. To manage sentences, or strings, VSFL adopts an approach similar to OpenGL. First a slot is created, then it is filled with a sentence, and finally it can be rendered.

Below is an example of how it works:

unsigned int s1 = vsfl.genSentence();
vsfl.prepareSentence(s1,"this is a sentence!");
vsfl.renderSentence(10,30,s1);

The function genSentence creates a slot for a sentence, much like the glGen* functions of OpenGL.

prepareSentence receives a slot index and a string and prepares the VAO for future rendering. From now on the string can be rendered multiple times calling renderSentence. This last function receives the window coordinates (origin is top left) to render the string stored in a slot, s1 indicates the slot index.

If a sentence changes over time, the same slot can be used to prepare a new sentence. The previous VAO and atrtribute buffers are released.

For instance, suppose we want a to display the number of frames per second. So having a global variable vsfl of type VSFontLib, and assuming that in our main we have created a slot with genSentence with a value aSentenceIndex, we can write in our rendering function:

...
sprintf(s,"FPS:%4.2f",fps);
vsfl.prepareSentence(aSentenceIndex,s);
vsfl.renderSentence(10,10,aSentenceIndex)
...

Once a sentence slot is no longer required its resources (VAO and vertex attribute buffers) can be released with:

vsfl.deleteSentence(aSentenceIndex);

And thats it!

 

Prev: Requirements Next: Specification
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

83,484 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

© 2013 Lighthouse3d.com Suffusion theme by Sayontan Sinha