public static TileSet Load(string file, GraphicsDevice graphicsDevice) { TileSet result = new TileSet(); result.properties = null; result.SourcePath = file; using (XmlReader xr = XmlReader.Create(file)) { while (xr.Read()) { if (xr.NodeType != XmlNodeType.Element) { continue; } if (xr.Name == "tileset") { result.Name = xr["name"]; result.TileSize = int.Parse(xr["tilewidth"]); //should be same as tileheight result.TileCount = int.Parse(xr["tilecount"]); result.columns = int.Parse(xr["columns"]); result.properties = new TiledProperties[result.TileCount]; } else if (xr.Name == "image") { result.ImagePath = Path.Combine(Path.GetDirectoryName(file), xr["source"]); using (FileStream fs = new FileStream(result.ImagePath, FileMode.Open)) result.Texture = Texture2D.FromStream(graphicsDevice, fs); } else if (xr.Name == "tile") { int id = int.Parse(xr["id"]); while (xr.Name != "properties") { xr.Read(); } using (StringReader sr = new StringReader(xr.ReadOuterXml())) using (XmlReader r = XmlReader.Create(sr)) { //r.Read(); result.properties[id] = TiledProperties.Load(r); } } } } return(result); }
public static MapLayer Load(XmlReader xr, GraphicsDevice graphicsDevice, TileSetManager tileSets, string directory) { xr.Read(); MapLayer result = new MapLayer(); result.tileSets = tileSets; result.name = xr["name"]; result.width = int.Parse(xr["width"]); result.height = int.Parse(xr["height"]); result.animatedOrInteractiveTiles = new List <BasicTile>(); while (xr.Read()) { if (xr.Name == "data" && xr.NodeType == XmlNodeType.Element) { string[] data = xr.ReadElementContentAsString().Split(','); result.data = new int[data.Length]; for (int i = 0; i < data.Length; i++) { result.data[i] = int.Parse(data[i]); } } if (xr.Name == "properties" && xr.NodeType == XmlNodeType.Element) //Properties { using (StringReader sr = new StringReader(xr.ReadOuterXml())) using (XmlReader r = XmlReader.Create(sr)) result.properties = TiledProperties.Load(r); } if (xr.Name == "layer" && xr.NodeType == XmlNodeType.EndElement) //The End { break; } } Texture2D t = new Texture2D(graphicsDevice, result.width * BasicTile.Size, result.height * BasicTile.Size); for (int i = 0; i < result.data.Length; i++) { int x = i % result.width; int y = i / result.width; bool interactive = result.tileSets.IsInteractive(result.data[i]); bool animated = result.tileSets.IsAnimation(result.data[i]); if (animated || interactive) { TiledProperties properties = result.tileSets.GetProperties(result.data[i]); result.animatedOrInteractiveTiles.Add(BasicTile.LoadTile(interactive, animated, properties, new Index2(x, y))); continue; } Color[] data = result.tileSets.GetData(result.data[i]); if (data == null) { data = new Color[BasicTile.Size * BasicTile.Size]; for (int j = 0; j < BasicTile.Size * BasicTile.Size; j++) { data[j] = Color.Transparent; } } t.SetData(0, new Rectangle(x * BasicTile.Size, y * BasicTile.Size, BasicTile.Size, BasicTile.Size), data, 0, data.Length); } foreach (var tiles in result.animatedOrInteractiveTiles) { if (tiles is InteractiveTile interactive) { interactive.Load(Path.Combine(directory, @".\..\TileSets", interactive.Properties.GetPropertyAsFile("AnimationFile")), graphicsDevice); } } result.Prerendered = t; //result.Prerendered.SaveAsPng(new FileStream(".\\prerendered" + result.name + ".png", FileMode.Create), result.Prerendered.Width, result.Prerendered.Height); //Console.WriteLine("prerendered" + result.name + "Saved"); return(result); }