XNa tutorial (4) -- draw a font in xNa

Source: Internet
Author: User

Author: Clayman
For personal learning only,Do not repost, do not use for any commercial purposes.
Due to my limited level, errors are inevitable. If you are not clear about it, please refer to the original document. You are welcome to talk with me a lot.
Blog: http://blog.csdn.net/soilwork
Clayman_joe@yahoo.com.cn

It is very troublesome to draw fonts in xNa. Because xNa is cross-platform and does not have system fonts in Xbox 360, xNa currently does not support font drawing. Although we can still use some methods to call GDI + in xNa to draw fonts, your program will lose its cross-platform nature. At the same time, calling GDI + also affects the drawing efficiency. Therefore, this is not a recommended method. What should we do? Currently, a better solution is to use bitmap font ). The so-called bitmap font is actually to make the font as a bitmap in advance, and then you can use the bitmap as a font. Note that because the font has been made as a bitmap, the size is fixed, so you should create different versions as needed. Of course, bitmap fonts also provide the ability to create unique fonts for games. For ease of use, many fonts are usually drawn into one bitmap, which brings another question: how to identify a character from an image? A common bitmap creation tool creates an XML file while creating a bitmap. In this file, the position and size of each character in the bitmap are recorded. below is the code indicating the letter "A" in an XML file:

<Glyph CH = "a" code = "0041" BM = "0" Origin = "" size = "15x27" aw = "14" LSB = "0 "/>

Just like a dictionary, we can use this XML file to locate the desired characters. (A bitmap font) bitmap font builder and bitmap font generator are both good tools for creating bitmap charts. In particular, bitmap font generator sorts fonts Based on the font size to minimize the bitmap size. However, both tools do not support Unicode. That is to say, if you need to create a Chinese character, you may encounter some problems. The xnaextras toolkit is recommended here. Xnaextras is a series of helper programs and classes provided by xNa diares. Contains bmfontgen, a small tool used to create bitmap fonts that support Unicode. bitmapfont can be used to create bitmap fonts that are convenient to draw fonts. In addition, it includes some demos and some tools used to draw Sprite. If you have time, you 'd better study the bitmapfont source code carefully to give you a deeper understanding of Bitmap fonts. If you think the code is too complex, take a look at another tutorial on creating and drawing fonts on ziggyware. The ancient division line ------------------------------------------------------ The Last Time I mentioned that setting the allowuserresizing of the game class to true can change the window size. I don't know if you find it. A bug occurs when you zoom in the window. The current window runs under all windows on the desktop (if you open many windows ). The solution is to add system. windows. forms; reference, and then in the program initialization code is this. window. add an eventhandler to the clientsizechanged event and add the following code to the eventhandler: (system. windows. forms. form) system. windows. forms. form. fromhandle (window. handle )). bringtofront (); PS: In the DirectX SDK of December October, MDX 2.0 is no longer included ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.