using System; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace SpriteFontPlus.Samples.BMFont { /// /// This is the main type for your game. /// public class Game1 : Game { GraphicsDeviceManager _graphics; SpriteBatch _spriteBatch; private SpriteFont _font; public Game1() { _graphics = new GraphicsDeviceManager(this) { PreferredBackBufferWidth = 1024, PreferredBackBufferHeight = 768 }; Content.RootDirectory = "Content"; IsMouseVisible = true; Window.AllowUserResizing = true; } /// /// LoadContent will be called once per game and is the place to load /// all of your content. /// protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. _spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D texture; using (var stream = TitleContainer.OpenStream("Fonts/test_0.png")) { texture = Texture2D.FromStream(GraphicsDevice, stream); } string fontData; using (var stream = TitleContainer.OpenStream("Fonts/test.fnt")) { using (var reader = new StreamReader(stream)) { fontData = reader.ReadToEnd(); } } _font = BMFontLoader.LoadXml(fontData, name => new TextureWithOffset(texture)); GC.Collect(); } /// /// UnloadContent will be called once per game and is the place to unload /// game-specific content. /// protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here _spriteBatch.Begin(); // Render some text _spriteBatch.DrawString(_font, "The quick brown fox jumps over the lazy dog", new Vector2(0, 0), Color.White); _spriteBatch.End(); base.Draw(gameTime); } } }