public MonoGameGameFont(MonoGameFileHandle fntFileHandle, MonoGameFileHandle textureFileHandle) { _fontName = fntFileHandle.nameWithoutExtension(); _spriteFont = BMFontLoader.LoadXml(fntFileHandle.readString(), ((MonoGameTexture)Mdx.graphics.newTexture(textureFileHandle)).texture2D); _sharedFontGlyphLayout = newGlyphLayout(); _capHeight = _spriteFont.MeasureString("A").Y; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> 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 => texture); GC.Collect(); }
public override SpriteFont Load(Stream stream) { using var reader = new StreamReader(stream); var fontData = reader.ReadToEnd(); var font = BMFontLoader.LoadXml(fontData, name => new TextureWithOffset(_assetLoader.Get <Texture2D>(name))); return(font); }
public static MonoGameGameFont loadBitmapFont(MonoGameFileHandle fntFileHandle) { var font = new MonoGameGameFont(); font._spriteFont = BMFontLoader.LoadXml(fntFileHandle.readString(), textureFileName => { return(new TextureWithOffset(((MonoGameTexture)Mdx.graphics_.newTexture(fntFileHandle.parent().child(textureFileName))).texture2D)); }); font._sharedFontGlyphLayout = font.newGlyphLayout(); font._capHeight = font._spriteFont.GetGlyphs()['A'].BoundsInTexture.Height; return(font); }
protected override void LoadContent() { base.LoadContent(); MyraEnvironment.Game = this; // Create resource asset resolver var assetResolver = new ResourceAssetResolver(GetType().Assembly, "Myra.Samples.CustomUIStylesheet.Resources."); // Load image containing font & ui spritesheet Texture2D texture; using (var stream = assetResolver.Open("ui_stylesheet_atlas.png")) { texture = Texture2D.FromStream(GraphicsDevice, stream); } // Load ui text atlas var textureAtlas = TextureRegionAtlas.FromXml(assetResolver.ReadAsString("ui_stylesheet_atlas.xml"), texture); // Load ui font(s) var region = textureAtlas["commodore-64"]; var fonts = new Dictionary <string, SpriteFont> { ["commodore-64"] = BMFontLoader.LoadText( assetResolver.ReadAsString("commodore-64.fnt"), s => new TextureWithOffset(region.Texture, region.Bounds.Location) ) }; // Load stylesheet var stylesheet = Stylesheet.LoadFromSource( assetResolver.ReadAsString("ui_stylesheet.xml"), s => textureAtlas[s], s => fonts[s]); Stylesheet.Current = stylesheet; _desktop = new Desktop(); _allWidgets = new AllWidgets(); _allWidgets._button.Image = textureAtlas["music-off"]; _allWidgets._imageButton.Image = textureAtlas["sound-off"]; _desktop.Widgets.Add(_allWidgets); }
private Stylesheet StylesheetFromFile(string path) { if (!Path.IsPathRooted(path)) { path = Path.Combine(Path.GetDirectoryName(FilePath), path); } Stylesheet stylesheet; if (_stylesheetCache.TryGetValue(path, out stylesheet)) { return(stylesheet); } var data = File.ReadAllText(path); var doc = XDocument.Parse(data); var root = doc.Root; var textureAtlases = new Dictionary <string, TextureRegionAtlas>(); var fonts = new Dictionary <string, SpriteFont>(); var designer = root.Element("Designer"); if (designer != null) { var folder = Path.GetDirectoryName(path); foreach (var element in designer.Elements()) { if (element.Name == "TextureAtlas") { var atlasPath = BuildPath(folder, element.Attribute("Atlas").Value); var imagePath = BuildPath(folder, element.Attribute("Image").Value); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); var atlasData = File.ReadAllText(atlasPath); textureAtlases[Path.GetFileName(atlasPath)] = TextureRegionAtlas.FromXml(atlasData, texture); } } else if (element.Name == "Font") { var id = element.Attribute("Id").Value; var fontPath = BuildPath(folder, element.Attribute("File").Value); var fontData = File.ReadAllText(fontPath); fonts[id] = BMFontLoader.LoadText(fontData, s => { if (s.Contains("#")) { var parts = s.Split('#'); var region = textureAtlases[parts[0]][parts[1]]; return(new TextureWithOffset(region.Texture, region.Bounds.Location)); } var imagePath = BuildPath(folder, s); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); return(new TextureWithOffset(texture)); } }); } } } stylesheet = Stylesheet.LoadFromSource(data, s => { TextureRegion result; foreach (var pair in textureAtlases) { if (pair.Value.Regions.TryGetValue(s, out result)) { return(result); } } throw new Exception(string.Format("Could not find texture region '{0}'", s)); }, s => { SpriteFont result; if (fonts.TryGetValue(s, out result)) { return(result); } throw new Exception(string.Format("Could not find font '{0}'", s)); } ); _stylesheetCache[path] = stylesheet; return(stylesheet); }
public SpriteFont Load(AssetLoaderContext context, string assetName) { var fontData = context.Load <string>(assetName); return(BMFontLoader.Load(fontData, name => TextureGetter(context, name))); }