Exemplo n.º 1
0
        public static Color[] Create(FastBitmap fb)
        {
            if (fb == null)
            {
                throw new ArgumentNullException(nameof(fb));
            }

            var colors = new List <Color>();

            for (int y = 0; y < fb.Height; y++)
            {
                for (int x = 0; x < fb.Width; x++)
                {
                    var color = fb.GetPixel(x, y);
                    if (!colors.Contains(color))
                    {
                        colors.Add(color);
                    }
                }
            }

            return(colors.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws a specified part of the tilemap on an image.
        /// </summary>
        /// <param name="fb">The image to draw the tilemap on.</param>
        /// <param name="tileset">The tileset.</param>
        public void Draw(FastBitmap fb, Tileset tileset, int srcX, int srcY, int srcWidth, int srcHeight)
        {
            if (fb == null || tileset == null)
            {
                throw new ArgumentNullException(fb == null ? nameof(fb) : nameof(tileset));
            }

            for (int y = srcY; y < srcY + srcHeight; y++)
            {
                for (int x = srcX; x < srcX + srcWidth; x++)
                {
                    ref var tile   = ref this[x, y];
                    ref var sprite = ref tileset[tile.Index];

                    for (int j = 0; j < 8; j++)
                    {
                        for (int k = 0; k < 8; k++)
                        {
                            var dstX = x * 8 + (tile.FlipX ? 7 - k : k);
                            var dstY = y * 8 + (tile.FlipY ? 7 - j : j);
                            fb.SetPixel(dstX, dstY, tileset.Palette[sprite[k, j]]);
                        }
                    }
                }
Exemplo n.º 3
0
 /// <summary>
 /// Draws the tilemap on an image.
 /// </summary>
 /// <param name="fb">The image to draw the tilemap on.</param>
 /// <param name="tileset">The tileset.</param>
 public void Draw(FastBitmap fb, Tileset tileset)
 {
     Draw(fb, tileset, 0, 0, width, height);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Tileset"/> class from the specified source image.
        /// </summary>
        /// <param name="source">The source image.</param>
        public Tileset(Bitmap source)
        {
            if (source.Width < 8 || source.Height < 8)
            {
                throw new ArgumentException("Image must be at least 8x8 pixels.", nameof(source));
            }

            if (source.Width / 8 * source.Height / 8 > 0x400)
            {
                throw new ArgumentException("Image is too large, ensure it has no more than 0x400 (1024) tiles.", nameof(source));
            }

            // Create the tiles
            var width  = source.Width / 8;
            var height = source.Height / 8;

            tiles = new Tile[width * height];

            // Copy image data from source
            using (var fb = FastBitmap.FromImage(source))
            {
                if ((source.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                {
                    // Copy the colors from the existing palette
                    switch (source.PixelFormat)
                    {
                    case PixelFormat.Format1bppIndexed:
                        palette = new Color[1 << 1];
                        break;

                    case PixelFormat.Format4bppIndexed:
                        palette = new Color[1 << 4];
                        break;

                    case PixelFormat.Format8bppIndexed:
                        palette = new Color[1 << 8];
                        break;

                    default:
                        throw new ArgumentException("Unsupported image format.", nameof(source));
                    }

                    source.Palette.Entries.CopyTo(palette, 0);

                    // Copy the tiles
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            ref var tile = ref tiles[x + y * width];

                            for (int j = 0; j < 8; j++)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    // Find the color index
                                    var index = palette.IndexOf(Color.FromArgb(fb.Bits[(x * 8 + i) + (y * 8 + j) * fb.Width]));
                                    if (index < 0)
                                    {
                                        throw new IndexOutOfRangeException();
                                    }

                                    // Copy to the tile
                                    tile[i, j] = index;
                                }
                            }
                        }
                    }
                }