public static void WriteString(this SpriteBatch sb, Font writefont, string text, int x, int y, Color color, float scale = 1f) { if (writefont == null) { throw new MissingFieldException("Missing font for WriteString"); } writefont.ResetWriter(); for (int i = 0; i < text.Length; i++) { FontLetter fn = writefont.GetLetter(text[i]); //new Rectangle(x + ((int)Math.Round((float)writefont.XOffset * scale)) + ((int)Math.Round((float)fn.xOff * scale)), y + ((int)Math.Round((float)fn.yOff * scale)), ((int)Math.Round((float)fn.rect.Width * scale)), ((int)Math.Round((float)fn.rect.Height * scale))) sb.Draw(writefont.texture, new Rectangle(x + (int)((float)writefont.XOffset * scale) + (int)((float)fn.xOff * scale), y + (int)((float)fn.yOff * scale), (int)((float)fn.rect.Width * scale), (int)((float)fn.rect.Height * scale)), fn.rect, color); writefont.AdvanceHead(fn); } }
public static void LoadFont(string fontName, string sfDirectory = "Spritefonts") { if (sfDirectory.Substring(sfDirectory.Length - 1, 1) != "\\") { sfDirectory += "\\"; } if (!System.IO.File.Exists(DCSG.Contents.RootDirectory + "\\" + sfDirectory + fontName + ".fnt")) throw new System.IO.FileNotFoundException("No font file associated with " + fontName); if (FontDocument == null) FontDocument = new XmlDocument(); FontDocument.Load(DCSG.Contents.RootDirectory + "\\" + sfDirectory + fontName + ".fnt"); XmlNode font = FontDocument.FirstChild; if (font.Name == "xml") { font = FontDocument.ChildNodes[1]; } if (font.Name != "font") throw new XmlException("Wrong first node in font file"); Font nf = new Font(); foreach (XmlElement item in font.ChildNodes) { switch (item.Name) { case "info": nf.Facename = item.GetAttribute("face"); nf.Size = Convert.ToInt32(item.GetAttribute("size")); break; case "common": break; case "chars": int count = Convert.ToInt32(item.GetAttribute("count")); for (int i = 0; i < item.ChildNodes.Count; i++) { int w = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("width")); int h = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("height")); int x = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("xadvance")); int yp = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("y")); int xp = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("x")); int yoff = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("yoffset")); int xoff = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("xoffset")); int id = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("id")); string let = ((XmlElement)item.ChildNodes[i]).GetAttribute("letter"); FontLetter fl = new FontLetter() { id = id, name = let, xadvance = x, xOff = xoff, yOff = yoff, rect = new Microsoft.Xna.Framework.Rectangle(xp, yp, w, h) }; nf.Letters.Add(fl); } break; case "kernings": break; } } nf.texture = DCSG.Contents.Load<Texture2D>(sfDirectory + fontName); nf.Name = new System.IO.FileInfo(DCSG.Contents.RootDirectory + "\\" + fontName + ".fnt").Name.Replace(".fnt", ""); LoadedFonts.Add(nf); Console.WriteLine(nf.Name); }
public Debug() { debugFont = Fonts.GetFont("std_small"); DCSG.OnDraw += new DCSG.XNADrawEvent(DCSG_OnDraw); DCSG.OnUpdate += new DCSG.XNAHookEvent(DCSG_OnUpdate); }