This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorials:using_bitmap_fonts [2011/03/07 07:05] – created daniel | tutorials:using_bitmap_fonts [2013/03/05 10:19] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Using bitmap fonts ====== | ||
+ | Almost any game will have to display some text sooner or later. Sparrow makes it easy to do so: | ||
+ | |||
+ | <code objc> | ||
+ | SPTextField *textField = [SPTextField textFieldWithWidth: | ||
+ | text: | ||
+ | textField.fontName = @" | ||
+ | textField.fontSize = 18; | ||
+ | textField.hAlign = SPHAlignCenter; | ||
+ | textField.vAlign = SPVAlignCenter; | ||
+ | </ | ||
+ | |||
+ | There are quite a number of different fonts already available on the iPhone. Click on the following image to get a bigger view of the fonts and their names. | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | When you create a textfield in the way shown above, Sparrow asks Cocoa to render the text to a bitmap. The bitmap is then used as a texture, just like any other image. | ||
+ | |||
+ | Creating this texture is relatively fast, and once it is created, the textfield will be rendered very quickly --- it's just an image, after all. | ||
+ | |||
+ | However, there are times where this method is not perfect, e.g.: | ||
+ | |||
+ | * when you need different fonts than those installed on the iPhone, or fonts with fancy effects | ||
+ | * when you need to change the text of a textfield very often, and constantly creating new textures turns out to be too slow | ||
+ | |||
+ | That's what bitmap fonts are for. A bitmap font is very similar to a texture atlas (see <a href=" | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | In addition to a texture like this, there is also an XML-file that describes which character can be found at which position in the image, and other information. | ||
+ | |||
+ | To create a bitmap font, we recommend one of the following tools: | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | From the free options, AngelCode produces the better output, but it is only available in Windows. When you use it, export the font data in XML format, and the texture as a PNG-file with white characters on a transparent background (32 bit). | ||
+ | |||
+ | " | ||
+ | |||
+ | Now, let's say you created your bitmap font --- that means you have 2 files: " | ||
+ | |||
+ | <code objc> | ||
+ | [SPTextField registerBitmapFontFromFile: | ||
+ | </ | ||
+ | |||
+ | That's it! When you register the bitmap font like this, you can use it just like any other font. Just set the " | ||
+ | |||
+ | For even more speed, you can add the font image to your texture atlas, as well. In that case, you have to register the font the following way: | ||
+ | |||
+ | <code objc> | ||
+ | [SPTextField registerBitmapFontFromFile: | ||
+ | texture: | ||
+ | </ | ||
+ | |||
+ | That should be all you need to know about bitmap fonts! |