Example #1
0
        /// <summary>
        /// Retrieves the entire map, including all its layers. Row-major order for tiles (y, then x).
        /// </summary>
        /// <returns>The corresponding TileMap that is contained in the .xp file</returns>
        /// <remarks>
        /// Note that (255,0,255) looks to be the 'transparent' code. But only for backgrounds?
        /// If you see magenta where you thought was black, that's why.
        /// </remarks>
        public TileMap GetMap()
        {
            CheckDisposed();
            int layers;
            int width;
            int height;

            //Get out early if the data is corrupt/malformed
            try {
                layers = GetLayerCount();
                width = GetLayerWidth(0);
                height = GetLayerHeight(0);
            } catch (Exception e) {
                ((IDisposable)Reader).Dispose();
                throw new InvalidDataException("Bad .xp data", e);
            }
            var map = new TileMap(width, height, layers);

            //Move to the first tile
            Deflated.Seek(GetFirstTileOffset(), SeekOrigin.Begin);

            //Read every tile in column-major order, and place it in the map in row-major order
            for (var layer = 0; layer < layers; layer++) {
                for (var col = 0; col < width; col++) {
                    for (var row = 0; row < height; row++) {
                        map.Layers[layer].Tiles[row, col] = new Tile {
                            CharacterCode = (byte)Reader.ReadInt32(),
                            ForegroundRed = Reader.ReadByte(),
                            ForegroundGreen = Reader.ReadByte(),
                            ForegroundBlue = Reader.ReadByte(),
                            BackgroundRed = Reader.ReadByte(),
                            BackgroundGreen = Reader.ReadByte(),
                            BackgroundBlue = Reader.ReadByte()
                        };
                    }
                }
            }
            Deflated.Seek(0, SeekOrigin.Begin);
            return map;
        }
Example #2
0
        public TileMap GetSingleLayerTestMap()
        {
            var map = new TileMap(SingleLayerWidth, SingleLayerHeight, SingleLayerLayerCount);
            for (var row = 0; row < map.Height; row++) {
                for (var col = 0; col < map.Width; col++) {
                    var tile = new Tile {
                        CharacterCode = Space,
                        BackgroundBlue = 255,
                        BackgroundGreen = 0,
                        BackgroundRed = 255,
                        ForegroundBlue = 0,
                        ForegroundGreen = 0,
                        ForegroundRed = 0
                    };

                    map.Layers[0].Tiles[row, col] = tile;
                }
            }

            //First tile
            map.Layers[0].Tiles[0, 0].CharacterCode = (byte)'u';
            map.Layers[0].Tiles[0, 0].BackgroundRed = 255;
            map.Layers[0].Tiles[0, 0].BackgroundBlue = 0;
            map.Layers[0].Tiles[0, 0].ForegroundGreen = 255;

            //First tile in second row
            map.Layers[0].Tiles[1, 0].CharacterCode = (byte)'A';
            map.Layers[0].Tiles[1, 0].BackgroundBlue = 255;
            map.Layers[0].Tiles[1, 0].BackgroundRed = 0;
            map.Layers[0].Tiles[1, 0].ForegroundRed = 255;

            //Last tile in last row
            map.Layers[0].Tiles[9, 9].CharacterCode = (byte)'e';
            map.Layers[0].Tiles[9, 9].BackgroundBlue = 255;
            map.Layers[0].Tiles[9, 9].BackgroundRed = 0;
            map.Layers[0].Tiles[9, 9].ForegroundBlue = 255;

            return map;
        }