SetPixels() public method

Sets the pixels in the given region of the GBA.Bitmap
public SetPixels ( Func displayfunc, Rectangle region ) : void
displayfunc Func
region Rectangle
return void
コード例 #1
0
        private void File_Tools_CreateImage_Click(Object sender, EventArgs e)
        {
            GBA.Bitmap idle = null;
            GBA.Bitmap move = null;
            byte       size;

            OpenFileDialog openWindow_idle = new OpenFileDialog();

            openWindow_idle.RestoreDirectory = true;
            openWindow_idle.Multiselect      = false;
            openWindow_idle.FilterIndex      = 1;
            openWindow_idle.Filter           =
                "'IDLE' Map sprite image file (*.png, *.bmp, *.gif)|*.png;*.bmp;*.gif|" +
                "All files (*.*)|*.*";
            if (openWindow_idle.ShowDialog() == DialogResult.OK)
            {
                if (openWindow_idle.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                    openWindow_idle.FileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) ||
                    openWindow_idle.FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        idle = new GBA.Bitmap(openWindow_idle.FileName, CurrentPalette);
                    }
                    catch (Exception ex)
                    {
                        Program.ShowError("Could not read bitmap for map sprite 'idle'.", ex);
                        return;
                    }
                    if (idle.Width == 16 && idle.Height == 16 * 3)
                    {
                        size = 0x00;
                    }
                    else if (idle.Width == 16 && idle.Height == 32 * 3)
                    {
                        size = 0x01;
                    }
                    else if (idle.Width == 32 && idle.Height == 32 * 3)
                    {
                        size = 0x02;
                    }
                    else
                    {
                        Program.ShowError("Invalid IDLE Map sprite image file, size should be either 16x48, 16x96, or 32x96 pixels");
                        return;
                    }
                }
                else
                {
                    Program.ShowError("File chosen has invalid extension.\r\n" + openWindow_idle.FileName);
                    return;
                }
            }
            else
            {
                return;
            }

            OpenFileDialog openWindow_move = new OpenFileDialog();

            openWindow_move.RestoreDirectory = true;
            openWindow_move.Multiselect      = false;
            openWindow_move.FilterIndex      = 1;
            openWindow_move.Filter           =
                "'MOVE' Map sprite image file (*.png, *.bmp, *.gif)|*.png;*.bmp;*.gif|" +
                "'MOVE' Map sprite image data (.move.chr)|*.chr|" +
                "All files (*.*)|*.*";
            if (openWindow_move.ShowDialog() == DialogResult.OK)
            {
                if (openWindow_move.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                    openWindow_move.FileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) ||
                    openWindow_move.FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        move = new GBA.Bitmap(openWindow_move.FileName, CurrentPalette);
                    }
                    catch (Exception ex)
                    {
                        Program.ShowError("Could not read bitmap for map sprite 'move'.", ex);
                        return;
                    }
                    if (move.Width != 32 && move.Height != 480)
                    {
                        Program.ShowError("Invalid MOVE Map sprite image file, size should be 32x480 pixels");
                        return;
                    }
                }
                else if (openWindow_move.FileName.EndsWith(".chr", StringComparison.OrdinalIgnoreCase))
                {
                    // TODO
                    return;
                }
                else
                {
                    Program.ShowError("File chosen has invalid extension.\r\n" + openWindow_move.FileName);
                    return;
                }
            }
            else
            {
                return;
            }

            GBA.Bitmap result = new GBA.Bitmap(
                MapSprite.WIDTH,
                MapSprite.HEIGHT);
            result.Colors = Core.ReadPalette(Core.CurrentROM.Address_MapSpritePalettes(), GBA.Palette.LENGTH * 8);
            result.SetPixels(delegate(int x, int y) { return((byte)move[x, y + 32 * 0]); }, new Rectangle(32, 0, 32, 32 * 4));      // MOVE side
            result.SetPixels(delegate(int x, int y) { return((byte)move[x, y + 32 * 4]); }, new Rectangle(64, 0, 32, 32 * 4));      // MOVE down
            result.SetPixels(delegate(int x, int y) { return((byte)move[x, y + 32 * 8]); }, new Rectangle(96, 0, 32, 32 * 4));      // MOVE up
            result.SetPixels(delegate(int x, int y) { return((byte)move[x, y + 32 * 12]); }, new Rectangle(128, 32, 32, 32 * 3));   // MOVE cursor-hover
            switch (size)
            {
            case 0x00:     // IDLE 16x16
                result.SetPixels(delegate(int x, int y) { return((byte)idle[x, y + 16 * 0]); }, new Rectangle(8, 32 * 1 + 16, 16, 16));
                result.SetPixels(delegate(int x, int y) { return((byte)idle[x, y + 16 * 1]); }, new Rectangle(8, 32 * 2 + 16, 16, 16));
                result.SetPixels(delegate(int x, int y) { return((byte)idle[x, y + 16 * 2]); }, new Rectangle(8, 32 * 3 + 16, 16, 16));
                break;

            case 0x01:     // IDLE 16x32
                result.SetPixels(delegate(int x, int y) { return((byte)idle[x, y]); }, new Rectangle(8, 32, 16, 32 * 3)); break;

            case 0x02:     // IDLE 32x32
                result.SetPixels(delegate(int x, int y) { return((byte)idle[x, y]); }, new Rectangle(0, 32, 32, 32 * 3)); break;

            default: throw new Exception("Invalid map sprite size value recieved.");
            }

            SaveFileDialog saveWindow = new SaveFileDialog();

            saveWindow.RestoreDirectory = true;
            saveWindow.OverwritePrompt  = true;
            saveWindow.CreatePrompt     = false;
            saveWindow.FilterIndex      = 1;
            saveWindow.Filter           =
                "Image file (*.png, *.bmp)|*.png;*.bmp|" +
                "All files (*.*)|*.*";

            if (saveWindow.ShowDialog() == DialogResult.OK)
            {
                if (saveWindow.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                    saveWindow.FileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        Core.SaveImage(saveWindow.FileName.Remove(saveWindow.FileName.Length - 4),
                                       Tile.SIZE * MapSprite.W_TILES,
                                       Tile.SIZE * MapSprite.H_TILES,
                                       Palette.Split(CurrentPalette, 8),
                                       delegate(int x, int y)
                        {
                            return((byte)result[x, y]);
                        });
                    }
                    catch (Exception ex)
                    {
                        Program.ShowError("Could not save image.", ex);
                    }
                    return;
                }
                else
                {
                    Program.ShowError("File chosen has invalid extension.\r\n" + saveWindow.FileName);
                    return;
                }
            }
        }