Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Using SpriteFonts

Similar presentations


Presentation on theme: "Lecture 4 Using SpriteFonts"— Presentation transcript:

1 Lecture 4 Using SpriteFonts
SpriteFontDemo

2 Creating SpriteFonts using the Content Manager
1. Open the MonoGame Content Manager and choose Add New Item. 2. Select SpriteFont Description and give this file an appropriate name. 1 3. After Saving, find the spritefont file and open it with a text editor. next slide 2 3

3 Setting the Font Properties
<?xml version="1.0" encoding="utf-8"?> <!-- This file contains an xml description of a font, and will be read by the XNA Framework Content Pipeline. Follow the comments to customize the appearance of the font in your game, and to change the characters which are available to draw with. --> <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <!-- Modify this string to change the font that will be imported. --> <FontName>SegoeUImono</FontName> <!-- Size is a float value, measured in points. Modify this value to change the size of the font. --> <Size>12</Size> <!-- Spacing is a float value, measured in pixels. Modify this value to change the amount of spacing in between characters. --> <Spacing>0</Spacing> <!-- UseKerning controls the layout of the font. If this value is true, kerning information will be used when placing characters. --> <UseKerning>true</UseKerning> <!-- Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic", and "Bold, Italic", and are case sensitive. --> <Style>Regular</Style> <!-- If you uncomment this line, the default character will be substituted if you draw or measure text that contains characters which were not included in the font. --> <!-- <DefaultCharacter>*</DefaultCharacter> --> <!-- CharacterRegions control what letters are available in the font. Every character from Start to End will be built and made available for drawing. The default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin character set. The characters are ordered according to the Unicode standard. See the documentation for more information. --> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> </CharacterRegions> </Asset> </XnaContent> spritefont files are writting in XML Open Office Word or any application that uses TrutType fonts to see the FontName to use. Set the Font Size Extra spacing between letters Whether Kerning is used. Set the Font Style Regular, Bold, Italic, or Bold Italic Save the edited file. If you mistype the FontName the SpriteFont will not create the .xnb file you need but, as with most XML files you wil not get much debugging help.

4 Class-Level Variables
GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont BWayFont; SpriteFont BSMTFont; SpriteFont JJFont; SpriteFont ChFont; Vector2 fontPos1 = new Vector2(60,40); Vector2 fontPos2 = new Vector2(60, 80); Vector2 fontPos3 = new Vector2(60, 140); Vector2 fontPos4 = new Vector2(60, 180); SpriteFont is a pre-defined object type in MonoGame give SpriteFonts appropriate names. The upper left corner of the beginning of a text string is defined using a Vector2 data type.

5 LoadContent( ) Method SpriteFonts are loaded into named instances
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); BWayFont = Content.Load<SpriteFont>("Broadway"); BSMTFont = Content.Load<SpriteFont>("BrushScriptMT"); JJFont = Content.Load<SpriteFont>("JingJing"); ChFont = Content.Load<SpriteFont>("Chiller"); } SpriteFonts are loaded into named instances you defined in the class-level variables list. The name of the SpriteFont instance in Game1 and the name of the SpritFont file can be anything. It is suggested that name be chosen that make it easy to determine which Fonts are being used. The FontName you use in the XML (i.e. SpriteFont) document must EXACTLY match the TrueType name of the font.

6 the Draw( ) Method protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); spriteBatch.DrawString(BWayFont, "I never saw a purple cow", fontPos1, Color.White); spriteBatch.DrawString(BSMTFont, "I hope to never see one", fontPos2, Color.Yellow); spriteBatch.DrawString(JJFont, "but I can tell you this right now", fontPos3, Color.LightBlue); spriteBatch.DrawString(ChFont, "I'd rather see than be one.", fontPos4, Color.DeepPink); spriteBatch.End(); base.Draw(gameTime); }


Download ppt "Lecture 4 Using SpriteFonts"

Similar presentations


Ads by Google