Game Development Reference
In-Depth Information
How to do it...
1.
For a monospaced font and an 8-bit ASCII character set, we can use a single pre-
rendered bitmap with all the 256 characters to simplify the rendering code. To make
this bitmap, we write a small tool, which reads a TrueType font, and outputs a square
bitmap 512 x 512 pixels, which contains a 16 × 16 characters grid:
#include <stdio.h>
#include <string.h>
2.
Include FreeType headers:
#include <ft2build.h>
#include FT_FREETYPE_H
3.
Declare the number of characters on each side, and the size of each character:
#define CHAR_SIZE 16
#define SLOT_SIZE 32
4.
Declare an array to store the output bitmap in RGBA format:
#define WIDTH CHAR_SIZE*SLOT_SIZE
#define HEIGHT CHAR_SIZE*SLOT_SIZE
unsigned char image[HEIGHT][WIDTH][4];
5. Declaring an externally deined routine to save a .bmp ile can be done using the
FreeImage library:
void write_bmp(const char *fname, int w, int h, int
bits_pp, unsigned char *img);
6.
Declaring a renderer of the FT_Bitmap at position (x, y) is as follows:
void draw_bitmap( FT_Bitmap* bitmap, FT_Int x, FT_Int y)
{
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width, y_max = y + bitmap->rows;
7.
Iterate pixels of the source bitmap:
for ( i = x, p = 0; i < x_max; i++, p++ )
for ( j = y, q = 0; j < y_max; j++, q++ )
{
if (i < 0 || j < 0 ||
i >= WIDTH || j >= HEIGHT ) continue;
 
Search WWH ::




Custom Search