Esempio n. 1
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 /// <param name="other"></param>
 public TilemapEntry(TilemapEntry other)
 {
     this.Index   = other.Index;
     this.Palette = other.Palette;
     this.FlipX   = other.FlipX;
     this.FlipY   = other.FlipY;
 }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Tilemap"/> class with the specified size.
        /// </summary>
        /// <param name="width">The width in tiles.</param>
        /// <param name="height">The height in tiles.</param>
        public Tilemap(int width, int height)
        {
            this.width  = width;
            this.height = height;

            tiles = new TilemapEntry[width * height];
            for (int i = 0; i < width * height; i++)
            {
                tiles[i] = new TilemapEntry();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Tilemap"/> class from the specified file.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        public Tilemap(string filename, TilemapFormat format, int width)
        {
            const int IndexMask   = 0x3FF;
            const int FlipXMask   = 0x400;
            const int FlipYMask   = 0x800;
            const int PaletteMask = 0xF;

            using (var fs = File.OpenRead(filename))
                using (var br = new BinaryReader(fs))
                {
                    // --------------------------------
                    var tileCount = (int)br.BaseStream.Length / (format == TilemapFormat.RotationScaling ? 1 : 2);

                    // --------------------------------
                    this.width  = width;
                    this.height = tileCount / width;

                    // --------------------------------
                    tiles = new TilemapEntry[width * height];
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            if (format == TilemapFormat.Text4)
                            {
                                var u = br.ReadUInt16();
                                tiles[x + y * width] = new TilemapEntry(
                                    (short)(u & IndexMask),
                                    (byte)((u >> 12) & PaletteMask),
                                    (u & FlipXMask) == FlipXMask,
                                    (u & FlipYMask) == FlipYMask
                                    );
                            }
                            else if (format == TilemapFormat.Text8)
                            {
                                var u = br.ReadUInt16();
                                tiles[x + y * width] = new TilemapEntry(
                                    (short)(u & IndexMask),
                                    (u & FlipXMask) == FlipXMask,
                                    (u & FlipYMask) == FlipYMask
                                    );
                            }
                            else // RotationScaling
                            {
                                tiles[x + y * width] = new TilemapEntry(br.ReadByte());
                            }
                        }
                    }
                }
        }
Esempio n. 4
0
        private void SaveGBA(Stream stream, TilemapFormat format, int padding)
        {
            using (var writer = new BinaryWriter(stream))
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        ref TilemapEntry tile = ref this[x, y];

                        if (format == TilemapFormat.Text4)
                        {
                            writer.Write((ushort)(
                                             (tile.Index & 0x3FF) |
                                             (tile.FlipX ? 0x400 : 0) |
                                             (tile.FlipY ? 0x800 : 0) |
                                             (tile.Palette << 12)
                                             ));
                        }
                        else if (format == TilemapFormat.Text8)
                        {
                            writer.Write((ushort)(
                                             (tile.Index & 0x3FF) |
                                             (tile.FlipX ? 0x400 : 0) |
                                             (tile.FlipY ? 0x800 : 0)
                                             ));
                        }
                        else // TilemapFormat.RotationScaling
                        {
                            writer.Write((byte)tile.Index);
                        }
                    }
                }

                for (int i = 0; i < padding; i++)
                {
                    writer.Write(byte.MinValue);
                }
            }
Esempio n. 5
0
        /// <summary>
        /// Resizes the tilemap.
        /// </summary>
        /// <param name="newWidth">The new width in tiles.</param>
        /// <param name="newHeight">The new height in tiles.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="newWidth"/> or <paramref name="newHeight"/> is negative.
        /// </exception>
        public void Resize(int newWidth, int newHeight)
        {
            if (newWidth < 0 || newHeight < 0)
            {
                throw new ArgumentOutOfRangeException(newWidth < 0 ? nameof(newWidth) : nameof(newHeight));
            }

            var temp       = new TilemapEntry[newWidth * newHeight];
            var copyWidth  = Math.Min(width, newWidth);
            var copyHeight = Math.Min(height, newHeight);

            for (int y = 0; y < copyHeight; y++)
            {
                for (int x = 0; x < copyWidth; x++)
                {
                    temp[x + y * newWidth] = tiles[x + y * width];
                }
            }

            tiles  = temp;
            width  = newWidth;
            height = newHeight;
        }