Wednesday, October 13, 2010

Discount Platypus Flossers

Tao.SDL TTF fonts with SDL and C #

If we write text using TrueType fonts (the standard in Windows and Linux), the changes are not large:
We must include the DLL file in the folder called SDL_ttf.DLL our program executable and the TTF file for each type font you want to use.
 We must declare a new data that will be our font, the generic type "IntPtr" 
tipoDeLetra IntPtr;

SdlTtf also need to initialize after basic initialization of SDL: SdlTtf.TTF_Init ();


Then prepare the font you want to use, indicating which file from TTF and what size:


/ / A font in size 18
 SdlTtf.TTF_OpenFont tipoDeLetra = ("FreeSansBold.ttf", 18); 
if (tipoDeLetra == IntPtr.Zero) {
System.Console.WriteLine ("Type nonexistent letter: FreeSansBold.ttf! ");
Environment.Exit (5);}

 
Then we can create an image from a phrase


/ / And create an image from the text
Sdl.SDL_Color colorAzulIntenso = new Sdl.SDL_Color (50, 50, 255);
 string text = "Sample text"; 
textoComoImagen IntPtr = SdlTtf.TTF_RenderText_Solid (
tipoDeLetra text, colorAzulIntenso)

if (textoComoImagen == IntPtr.Zero) {
System.Console.WriteLine ("I can not render the text");
Environment.Exit (6);}



And we could draw that image anywhere on the screen, like any other image:
 

/ / Write the text, image source = new Sdl.SDL_Rect
(0.0, anchoPantalla, altoPantalla)
dest = new Sdl.SDL_Rect (200,100, anchoPantalla, altoPantalla)
Sdl.SDL_BlitSurface (textoComoImagen, ref origin
pantallaOculta, ref dest);


As this way of working can be cumbersome, soon we will improve, creating a class "source" that we hide all these details and allow us to easily write text.


0 comments:

Post a Comment