using System; using System.Xml; using System.Diagnostics; namespace SpaceInvaders { public class GlyphMan : Manager { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- private Glyph poCompare; private static GlyphMan poInstance = null; //------------------------------------------------------------------- // CONSTRUCTION //------------------------------------------------------------------- private GlyphMan(int numReserve = 3, int numGrow = 1) : base(new DLinkMan(), new DLinkMan(), numReserve, numGrow) { this.poCompare = new Glyph(); Debug.Assert(this.poCompare != null); } //------------------------------------------------------------------- // PUBLIC METHODS //------------------------------------------------------------------- public static void Create(int numReserve = 3, int numGrow = 1) { if(GlyphMan.poInstance == null) { GlyphMan.poInstance = new GlyphMan(numReserve, numGrow); Debug.Assert(GlyphMan.poInstance != null); } } public static void Destroy() { } public static void AddXml(Glyph.GlyphName glyphName, string asset, Texture.TextureName textureName) { GlyphMan pManager = GlyphMan.GetInstance(); Debug.Assert(pManager != null); Glyph pNode = (Glyph)pManager.AddToFront(); Debug.Assert(pNode != null); pNode.SetGlyph(glyphName, textureName); //use load xml once invoked System.Xml.XmlTextReader reader = new XmlTextReader(asset); int key = -1; int x = -1; int y = -1; int width = -1; int height = -1; //TODO: how efficient is this? while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. if (reader.GetAttribute("key") != null) { key = Convert.ToInt32(reader.GetAttribute("key")); } else if (reader.Name == "x") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Text) { x = Convert.ToInt32(reader.Value); break; } } } else if (reader.Name == "y") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Text) { y = Convert.ToInt32(reader.Value); break; } } } else if (reader.Name == "width") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Text) { width = Convert.ToInt32(reader.Value); break; } } } else if (reader.Name == "height") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Text) { height = Convert.ToInt32(reader.Value); break; } } } break; case XmlNodeType.EndElement: //Display the end of the element if (reader.Name == "character") { // have all the data... so now create a glyph //Debug.WriteLine("key:{0} x:{1} y:{2} w:{3} h:{4}", key, x, y, width, height); pNode.SetKey(key, x, y, width, height); } break; } } } public static Glyph FindGlyph(Glyph.GlyphName name/*, int key*/) { GlyphMan pManager = GlyphMan.GetInstance(); Debug.Assert(pManager != null); pManager.poCompare.name = name; Glyph pGlyph = (Glyph)pManager.Find(pManager.poCompare); return pGlyph; } public static void RemoveGlyph(Glyph pImage) { Debug.Assert(pImage != null); GlyphMan pManager = GlyphMan.GetInstance(); Debug.Assert(pManager != null); pManager.Remove(pImage); } public static void PrintGlyph() { GlyphMan pManager = GlyphMan.GetInstance(); Debug.Assert(pManager != null); pManager.PrintStats(); } //------------------------------------------------------------------- // PROTECTED METHODS //------------------------------------------------------------------- protected override Node derivedCreate() { Node pNode = new Glyph(); Debug.Assert(pNode != null); return pNode; } //------------------------------------------------------------------- // PRIVATE METHODS //------------------------------------------------------------------- private static GlyphMan GetInstance() { Debug.Assert(GlyphMan.poInstance != null); return GlyphMan.poInstance; } } // end class } // end namespace