protected static void ProcessLayer(XmlElement element, IsoMap map)
        {
            Dictionary<int, MapImage> imageOffsets = new Dictionary<int, MapImage>();

            foreach (KeyValuePair<int,MapImage> img in map.Tilesets)
            {
                TilesetInfo info = img.Value.Tag as TilesetInfo;
                if (info != null)
                {
                    imageOffsets.Add(info.FirstGUID-1, img.Value);
                }
            }
            if (imageOffsets.Count == 0)
            {
                int offset = 0;
                foreach (KeyValuePair<int, MapImage> img in map.Tilesets)
                {
                    imageOffsets.Add(offset, img.Value);
                    offset += img.Value.Width * img.Value.Height;
                }
            }

            MapLayer layer = new MapLayer();

            Size layerBounds = new Size(map.Options.Bounds.Width, map.Options.Bounds.Height);

            foreach (XmlAttribute attrib in element.Attributes)
            {
                if (attrib.Name == "name")
                    layer.Name = attrib.Value;
                else if (attrib.Name == "width")
                    layerBounds = new Size(int.Parse(attrib.Value), layerBounds.Height);
                else if (attrib.Name == "height")
                    layerBounds = new Size(layerBounds.Width, int.Parse(attrib.Value));
            }

            if (layerBounds != map.Options.Bounds)
                return;

            int[] intList = null;

            foreach (XmlElement child in element.GetElementsByTagName("data"))
            {
                XmlText data = child.LastChild as XmlText;
                if (data == null)
                    continue;
                string val = data.Data.TrimStart(new char[] { '\n', ' ' });

                if (child.GetAttribute("encoding") == "base64")
                {
                    byte[] b = Convert.FromBase64String(val);

                    MemoryStream stream = new MemoryStream(b);
                    byte[] outbuffer = null;
                    byte[] t = new byte[1024];

                    Stream readStream = null;

                    if (!child.HasAttribute("compression"))
                        readStream = stream;
                    else
                    {
                        string comp = child.GetAttribute("compression");
                        if (comp == "gzip")
                            readStream = new GZipStream(stream, CompressionMode.Decompress);
                        else if (comp == "zlib")
                            continue;// readStream = new ZOutputStream(stream);
                    }

                    if (readStream == null)
                        continue; // no clue what it is

                    int read = readStream.Read(t, 0, 1024);
                    while (read != 0)
                    {
                        if (outbuffer == null)
                        {
                            outbuffer = new byte[read];
                            Array.Copy(t, outbuffer, read);
                        }
                        else
                        {
                            byte[] n = new byte[outbuffer.Length + read];
                            Array.Copy(outbuffer, n, outbuffer.Length);
                            Array.Copy(t, 0, n, outbuffer.Length, read);

                            outbuffer = n;
                        }
                        read = readStream.Read(t, 0, 1024);
                    }
                    readStream.Close();
                    stream.Close();

                    if (outbuffer == null)
                        continue;

                    int intCount = outbuffer.Length / 4;
                    intList = new int[intCount];

                    for (int i = 0; i < intCount; i++)
                    {
                        int j = BitConverter.ToInt32(outbuffer, i * 4);
                        intList[i] = j;
                    }
                }
                else if (child.GetAttribute("encoding") == "csv")
                {
                    string[] list = val.Split(",".ToCharArray());
                    intList = new int[list.Length];

                    for (int i = 0; i < list.Length; i++)
                        intList[i] = int.Parse(list[i]);
                }
            }

            if (intList == null || intList.Length != map.Options.Bounds.Width * map.Options.Bounds.Height)
            {
                return;
            }

            for (int i = 0; i < intList.Length; i++)
            {
                MapLayerCell cell = new MapLayerCell();
                int offset = 0;
                cell.Source = ImageFromID(intList[i]-1, out offset, imageOffsets);

                if (cell.Source != null)
                {
                    int y = offset / cell.Source.Bounds.Width;
                    cell.Cell = new Point(offset - (y * cell.Source.Bounds.Width), y);
                }
                layer.Cells.Add(cell);
            }

            if (layer.Cells.Count > 0)
            {
                if (map.ForeLayers.Count > 0)
                {
                    if (layer.Name == "Collision")
                    {
                        map.CollisionMap.Clear();
                        foreach (MapLayerCell cell in layer.Cells)
                            map.CollisionMap.Add(cell.Source != null);
                    }
                    else
                        map.ForeLayers.Add(layer);
                }
                else
                {
                    if (layer.Name.ToLower() == "over" || layer.Name.ToLower() == "fringe")
                        map.ForeLayers.Add(layer);
                    else
                        map.BackgroundLayers.Add(layer);
                }
            }
        }
Example #2
0
 protected void DrawLayer(MapLayer layer, Point originGraphics, Graphics graphics, Rectangle visBounds)
 {
     for (int y = visBounds.Top; y < visBounds.Bottom; y++)
     {
         for (int x = visBounds.Left; x < visBounds.Right; x++)
         {
             int index = x + y * Options.Bounds.Width;
             if (layer.Cells[index].Source != null)
                 layer.Cells[index].Source.ImageDrawGrid(layer.Cells[index].Cell, new Point((x * Options.Grid.Width) + originGraphics.X, (y * Options.Grid.Height) - layer.Cells[index].Source.Grid.Height + Options.Grid.Height + originGraphics.Y), graphics);
         }
     }
 }