コード例 #1
0
        public static BitmapBits DrawLayout(SphereType?[,] layout, int gridsize)
        {
            int        width     = layout.GetLength(0);
            int        height    = layout.GetLength(1);
            int        off       = (gridsize - 24) / 2;
            BitmapBits layoutbmp = new BitmapBits(width * gridsize, height * gridsize);

            for (int y = -gridsize / 2; y < layoutbmp.Height; y += gridsize * 2)
            {
                for (int x = -gridsize / 2; x < layoutbmp.Width; x += gridsize * 2)
                {
                    layoutbmp.FillRectangle(1, x, y, gridsize, gridsize);
                }
            }
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    SphereType?sp = layout[x, y];
                    if (sp.HasValue && sp.Value != SphereType.Empty)
                    {
                        layoutbmp.DrawBitmapComposited(SphereBmps[sp.Value], x * gridsize + off, y * gridsize + off);
                    }
                }
            }
            return(layoutbmp);
        }
コード例 #2
0
ファイル: Sprite.cs プロジェクト: Alriightyman/SonLVL
        public Sprite(IEnumerable <Sprite> sprites)
        {
            List <Sprite> sprlst = new List <Sprite>(sprites);
            int           left   = 0;
            int           right  = 0;
            int           top    = 0;
            int           bottom = 0;
            bool          first  = true;

            foreach (Sprite spr in sprlst)
            {
                if (spr.Image != null)
                {
                    if (first)
                    {
                        left   = spr.Left;
                        right  = spr.Right;
                        top    = spr.Top;
                        bottom = spr.Bottom;
                        first  = false;
                    }
                    else
                    {
                        left   = Math.Min(spr.Left, left);
                        right  = Math.Max(spr.Right, right);
                        top    = Math.Min(spr.Top, top);
                        bottom = Math.Max(spr.Bottom, bottom);
                    }
                }
            }
            Offset = new Point(left, top);
            Image  = new BitmapBits(right - left, bottom - top);
            for (int i = 0; i < sprlst.Count; i++)
            {
                if (sprlst[i].Image != null)
                {
                    bool comp = false;
                    for (int j = 0; j < i; j++)
                    {
                        if (sprlst[j].Image != null && sprlst[i].Bounds.IntersectsWith(sprlst[j].Bounds))
                        {
                            comp = true;
                            break;
                        }
                    }
                    if (comp)
                    {
                        Image.DrawBitmapComposited(sprlst[i].Image, new Point(sprlst[i].X - left, sprlst[i].Y - top));
                    }
                    else
                    {
                        Image.DrawBitmap(sprlst[i].Image, new Point(sprlst[i].X - left, sprlst[i].Y - top));
                    }
                }
            }
        }
コード例 #3
0
        public static BitmapBits DrawLayout(LayoutData layout, bool shownum)
        {
            BitmapBits layoutbmp = DrawLayout(layout.Layout, shownum);

            if (layout.StartPosition != null)
            {
                layoutbmp.DrawBitmapComposited(StartPosBmp, layout.StartPosition.X - 736 - (StartPosBmp.Width / 2), layout.StartPosition.Y - 688 - (StartPosBmp.Height / 2));
            }
            return(layoutbmp);
        }
コード例 #4
0
        public static BitmapBits DrawLayout(LayoutData layout, int gridsize)
        {
            BitmapBits layoutbmp = DrawLayout(layout.Layout.ToArray(), gridsize);
            int        off       = (gridsize - 24) / 2;

            switch (layout)
            {
            case SSLayoutData ss:
                layoutbmp.DrawBitmapComposited(StartBmps[ss.Angle >> 14], (ss.StartX / 0x100) * gridsize + off, (ss.StartY / 0x100) * gridsize + off);
                break;

            case BSChunkLayoutData bsc:
                layoutbmp.DrawBitmapComposited(StartBmps[3], 15 * gridsize + off, 3 * gridsize + off);
                break;

            case BSStageLayoutData bss:
                layoutbmp.DrawBitmapComposited(StartBmps[1], 16 * gridsize + off, 3 * gridsize + off);
                break;
            }
            return(layoutbmp);
        }
コード例 #5
0
		/**
		 * Extracts image pixels into byte array "pixels"
		 */
		protected void GetImagePixels()
		{
			int w = image.Width;
			int h = image.Height;
			//		int type = image.GetType().;
			if ((w != width)
				|| (h != height)
				)
			{
				// create new image with right size/format
				BitmapBits bmp = new BitmapBits(width, height);
				bmp.DrawBitmapComposited(image, Point.Empty);
				image = bmp;
			}
			indexedPixels = image.Bits;
		}
コード例 #6
0
        public static BitmapBits DrawLayout(byte[,] layout, bool shownum)
        {
            int        width     = layout.GetLength(0);
            int        height    = layout.GetLength(1);
            BitmapBits layoutbmp = new BitmapBits(width * 24, height * 24);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    byte sp = layout[x, y];
                    if (sp != 0 && ObjectBmps.ContainsKey(sp))
                    {
                        layoutbmp.DrawBitmapComposited(shownum ? ObjectBmps[sp] : ObjectBmpsNoNum[sp], x * 24, y * 24);
                    }
                }
            }
            return(layoutbmp);
        }
コード例 #7
0
 private void exportGIFButton_Click(object sender, EventArgs e)
 {
     using (SaveFileDialog fd = new SaveFileDialog()
     {
         DefaultExt = "gif", FileName = curanim.Name + ".gif", Filter = "GIF Images|*.gif"
     })
         if (fd.ShowDialog(this) == DialogResult.OK)
         {
             string           file     = fd.FileName;
             string           filebase = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file));
             string           fileext  = Path.GetExtension(file);
             Animation        anim     = curanim;
             List <Animation> anims    = new List <Animation>()
             {
                 anim
             };
             while (anims[anims.Count - 1].EndType == 0xFD)
             {
                 anims.Add(animations[anims[anims.Count - 1].ExtraParam.Value]);
             }
             bool isFE = anims[anims.Count - 1].EndType == 0xFE;
             if (isFE)
             {
                 file = filebase + "_intro" + fileext;
             }
             AnimatedGifEncoder gif = new AnimatedGifEncoder();
             gif.Start(file);
             if (anim.EndType == 0xFF || anim.EndType == 0xFB)
             {
                 gif.SetRepeat(0);
             }
             else
             {
                 gif.SetRepeat(-1);
             }
             double delay    = 0;
             double leftover = 0;
             gif.SetDelay(delay);
             gif.SetPalette(palette.Entries);
             int left   = 0;
             int right  = 0;
             int top    = 0;
             int bottom = 0;
             for (int a = 0; a < anims.Count; a++)
             {
                 for (int i = 0; i < anims[a].Count; i++)
                 {
                     left   = Math.Min(sprites[anims[a][i]].Left, left);
                     right  = Math.Max(sprites[anims[a][i]].Right, right);
                     top    = Math.Min(sprites[anims[a][i]].Top, top);
                     bottom = Math.Max(sprites[anims[a][i]].Bottom, bottom);
                 }
             }
             for (int a = 0; a < anims.Count; a++)
             {
                 for (int i = 0; i < anims[a].Count; i++)
                 {
                     delay = (anims[a].Speed < 0xFD ? anims[a].Speed : 4) * Frame;
                     int samecnt = 0;
                     while (i + samecnt < anims[a].Count && anims[a][i + samecnt] == anims[a][i])
                     {
                         samecnt++;
                     }
                     delay    *= samecnt;
                     leftover += delay % 1;
                     if (leftover >= 1)
                     {
                         delay++;
                     }
                     leftover %= 1;
                     gif.SetDelay(delay);
                     BitmapBits image = new BitmapBits(right - left, bottom - top);
                     image.DrawBitmapComposited(sprites[anims[a][i]].Image, sprites[anims[a][i]].X - left, sprites[anims[a][i]].Y - top);
                     gif.AddFrame(image);
                     i += samecnt - 1;
                 }
             }
             gif.Finish();
             if (isFE)
             {
                 anim = anims[anims.Count - 1];
                 file = filebase + "_loop" + fileext;
                 gif  = new AnimatedGifEncoder();
                 gif.Start(file);
                 gif.SetRepeat(0);
                 gif.SetPalette(palette.Entries);
                 left   = 0;
                 right  = 0;
                 top    = 0;
                 bottom = 0;
                 for (int i = anim.Count - anim.ExtraParam.Value; i < anim.Count; i++)
                 {
                     left   = Math.Min(sprites[anim[i]].Left, left);
                     right  = Math.Max(sprites[anim[i]].Right, right);
                     top    = Math.Min(sprites[anim[i]].Top, top);
                     bottom = Math.Max(sprites[anim[i]].Bottom, bottom);
                 }
                 for (int i = anim.Count - anim.ExtraParam.Value; i < anim.Count; i++)
                 {
                     delay = (anim.Speed < 0xFD ? anim.Speed : 4) * Frame;
                     int samecnt = 0;
                     while (i + samecnt < anim.Count && anim[i + samecnt] == anim[i])
                     {
                         samecnt++;
                     }
                     delay    *= samecnt;
                     leftover += delay % 1;
                     if (leftover >= 1)
                     {
                         delay++;
                     }
                     leftover %= 1;
                     gif.SetDelay(delay);
                     BitmapBits image = new BitmapBits(right - left, bottom - top);
                     image.DrawBitmapComposited(sprites[anim[i]].Image, sprites[anim[i]].X - left, sprites[anim[i]].Y - top);
                     gif.AddFrame(image);
                     i += samecnt - 1;
                 }
                 gif.Finish();
             }
         }
 }
コード例 #8
0
        static void Main(string[] args)
        {
            LongOpt[] opts = new[] {
                new LongOpt("help", Argument.No, null, 'h'),
                new LongOpt("padding", Argument.Required, null, 'p'),
                new LongOpt("startpal", Argument.Required, null, 's'),
                new LongOpt("nullfirst", Argument.No, null, 'n'),
                new LongOpt("twoplayer", Argument.No, null, '2'),
                new LongOpt("format", Argument.Required, null, 'f'),
                new LongOpt("game", Argument.Required, null, 'g'),
                new LongOpt("artcmp", Argument.Required, null, 'c'),
                new LongOpt("lowplane", Argument.Required, null, 0),
                new LongOpt("highplane", Argument.Required, null, 0),
                new LongOpt("palette", Argument.Required, null, 0),
                new LongOpt("center", Argument.Required, null, 0),
                new LongOpt("artfile", Argument.Required, null, 0),
                new LongOpt("mapfile", Argument.Required, null, 0),
                new LongOpt("dplcfile", Argument.Required, null, 0),
                new LongOpt("palfile", Argument.Required, null, 0)
            };
            if (args.Length == 0)
            {
                args = new string[] { "-h" }
            }
            ;
            Getopt          getopt        = new Getopt("SpritePlotter.NET", args, Getopt.digest(opts), opts);
            int             padding       = 2;
            byte            startpal      = 0;
            bool            nullfirst     = false;
            bool            twoplayer     = false;
            MappingsFormat  mapfmt        = MappingsFormat.Binary;
            EngineVersion   mapgame       = EngineVersion.Invalid;
            bool            s3kp          = false;
            CompressionType artcmp        = CompressionType.Uncompressed;
            Bitmap          lowplanefile  = null;
            Bitmap          highplanefile = null;

            Color[] palette    = null;
            Bitmap  centerfile = null;
            string  artfile    = null;
            string  mapfile    = null;
            string  dplcfile   = null;
            string  palfile    = null;
            int     opt        = getopt.getopt();

            while (opt != -1)
            {
                switch (opt)
                {
                case 'h':
                    Console.Write(Properties.Resources.HelpText);
                    return;

                case 'p':
                    padding = int.Parse(getopt.Optarg);
                    break;

                case 's':
                    startpal = byte.Parse(getopt.Optarg);
                    break;

                case 'n':
                    nullfirst = true;
                    break;

                case '2':
                    twoplayer = true;
                    break;

                case 'f':
                    mapfmt = (MappingsFormat)Enum.Parse(typeof(MappingsFormat), getopt.Optarg, true);
                    break;

                case 'g':
                    if (getopt.Optarg.Equals("s3kp", StringComparison.OrdinalIgnoreCase))
                    {
                        mapgame = EngineVersion.S3K;
                        s3kp    = true;
                    }
                    else
                    {
                        mapgame = (EngineVersion)Enum.Parse(typeof(EngineVersion), getopt.Optarg, true);
                    }
                    break;

                case 'c':
                    artcmp = (CompressionType)Enum.Parse(typeof(CompressionType), getopt.Optarg, true);
                    break;

                case 0:
                    switch (opts[getopt.Longind].Name)
                    {
                    case "lowplane":
                        lowplanefile = new Bitmap(getopt.Optarg);
                        if (palette == null && (lowplanefile.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                        {
                            palette = lowplanefile.Palette.Entries;
                        }
                        break;

                    case "highplane":
                        highplanefile = new Bitmap(getopt.Optarg);
                        if (palette == null && (highplanefile.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                        {
                            palette = highplanefile.Palette.Entries;
                        }
                        break;

                    case "palette":
                        switch (System.IO.Path.GetExtension(getopt.Optarg))
                        {
                        case ".bin":
                            palette = SonLVLColor.Load(getopt.Optarg, EngineVersion.S2).Select(a => a.RGBColor).ToArray();
                            break;

                        case ".bmp":
                        case ".png":
                        case ".jpg":
                        case ".gif":
                            using (Bitmap bmp = new Bitmap(getopt.Optarg))
                            {
                                if ((bmp.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                                {
                                    palette = bmp.Palette.Entries;
                                }
                                else
                                {
                                    List <Color> pal = new List <Color>();
                                    for (int y = 0; y < bmp.Height; y += 8)
                                    {
                                        for (int x = 0; x < bmp.Width; x += 8)
                                        {
                                            pal.Add(bmp.GetPixel(x, y));
                                        }
                                    }
                                    palette = pal.ToArray();
                                }
                            }
                            break;
                        }
                        break;

                    case "center":
                        centerfile = new Bitmap(getopt.Optarg);
                        break;

                    case "artfile":
                        artfile = getopt.Optarg;
                        break;

                    case "mapfile":
                        mapfile = getopt.Optarg;
                        break;

                    case "dplcfile":
                        dplcfile = getopt.Optarg;
                        break;

                    case "palfile":
                        palfile = getopt.Optarg;
                        break;
                    }
                    break;
                }
                opt = getopt.getopt();
            }
            if (mapgame == EngineVersion.Invalid)
            {
                Console.Error.WriteLine("No game specified.");
                return;
            }
            if (lowplanefile == null && highplanefile == null)
            {
                Console.Error.WriteLine("No image file specified.");
                return;
            }
            if (palette == null)
            {
                Console.Error.WriteLine("No palette specified, and image(s) did not contain palette.");
                return;
            }
            if (artfile == null)
            {
                Console.Error.WriteLine("No output art file specified.");
                return;
            }
            if (mapfile == null)
            {
                Console.Error.WriteLine("No output mappings file specified.");
                return;
            }
            if (palette.Length > 64)
            {
                palette = palette.Take(64).ToArray();                 // lazy
            }
            BitmapBits lowplane = null, highplane = null, combinedplanes = null;

            if (lowplanefile != null)
            {
                if ((lowplanefile.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                {
                    lowplane = new BitmapBits(lowplanefile);
                }
                else
                {
                    using (Bitmap bmp = lowplanefile.To32bpp())
                        lowplane = LoadBitmap32BppArgb(bmp, palette);
                }
                lowplanefile.Dispose();
                if (highplanefile == null)
                {
                    combinedplanes = lowplane;
                }
            }
            if (highplanefile != null)
            {
                if ((highplanefile.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                {
                    highplane = new BitmapBits(highplanefile);
                }
                else
                {
                    using (Bitmap bmp = highplanefile.To32bpp())
                        highplane = LoadBitmap32BppArgb(bmp, palette);
                }
                highplanefile.Dispose();
                if (lowplanefile == null)
                {
                    combinedplanes = highplane;
                }
            }
            if (combinedplanes == null)
            {
                combinedplanes = new BitmapBits(Math.Max(lowplane.Width, highplane.Width), Math.Max(lowplane.Height, highplane.Height));
                combinedplanes.DrawBitmap(lowplane, 0, 0);
                combinedplanes.DrawBitmapComposited(highplane, 0, 0);
            }
            List <Point> centers = null;

            if (centerfile != null)
            {
                centers = new List <Point>();
                BitmapBits cntr;
                if ((centerfile.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                {
                    cntr = new BitmapBits(centerfile);
                }
                else
                {
                    using (Bitmap bmp = centerfile.To32bpp())
                        cntr = LoadBitmap32BppArgb(bmp, Color.Black, Color.White);
                }
                centerfile.Dispose();
                for (int y = 0; y < cntr.Height; y++)
                {
                    for (int x = 0; x < cntr.Width; x++)
                    {
                        if (cntr[x, y] != 0)
                        {
                            centers.Add(new Point(x, y));
                        }
                    }
                }
            }
            List <Rectangle> sprites = new List <Rectangle>();

            for (int y = 0; y < combinedplanes.Height; y++)
            {
                for (int x = 0; x < combinedplanes.Width; x++)
                {
                    if (combinedplanes[x, y] % 16 != 0)
                    {
                        Rectangle newrect = new Rectangle(x - padding, y - padding, (padding * 2) + 1, (padding * 2) + 1);
                        for (int i = 0; i < sprites.Count; i++)
                        {
                            if (newrect.IntersectsWith(sprites[i]))
                            {
                                newrect = Rectangle.Union(newrect, sprites[i]);
                                sprites.RemoveAt(i);
                                i = -1;
                            }
                        }
                        sprites.Add(newrect);
                    }
                }
            }
            List <Rectangle> rows = new List <Rectangle>();

            for (int i = 0; i < sprites.Count; i++)
            {
                Rectangle rect = sprites[i];
                rect.Inflate(-padding, -padding);
                sprites[i] = rect;
                if (rows.Count > 0 && sprites[i].IntersectsWith(rows[rows.Count - 1]))
                {
                    rows[rows.Count - 1] = Rectangle.Union(sprites[i], rows[rows.Count - 1]);
                }
                else
                {
                    rows.Add(new Rectangle(0, sprites[i].Y, combinedplanes.Width, sprites[i].Height));
                }
            }
            List <Rectangle> newsprites = new List <Rectangle>(sprites.Count);

            foreach (Rectangle rect in rows)
            {
                newsprites.AddRange(sprites.Where(a => a.IntersectsWith(rect)).OrderBy(a => a.X));
            }
            sprites = newsprites;
            List <byte[]>             tiles = new List <byte[]>();
            NamedList <MappingsFrame> map   = new NamedList <MappingsFrame>("Map_" + Path.GetFileNameWithoutExtension(mapfile), sprites.Count);
            NamedList <DPLCFrame>     dplc  = null;

            if (dplcfile != null)
            {
                dplc = new NamedList <DPLCFrame>("DPLC_" + Path.GetFileNameWithoutExtension(dplcfile), sprites.Count);
            }
            if (nullfirst)
            {
                map.Add(new MappingsFrame(map.Name + "_Null"));
                if (dplc != null)
                {
                    dplc.Add(new DPLCFrame(dplc.Name + "_Null"));
                }
            }
            for (int i = 0; i < sprites.Count; i++)
            {
                Point center = new Point(sprites[i].Width / 2, sprites[i].Height / 2);
                if (centers != null)
                {
                    foreach (Point item in centers)
                    {
                        if (sprites[i].Contains(item))
                        {
                            center = new Point(item.X - sprites[i].Left, item.Y - sprites[i].Top);
                            break;
                        }
                    }
                }
                MappingsFrame mapframe = new MappingsFrame(map.Name + "_" + i);
                map.Add(mapframe);
                DPLCFrame dplcframe = null;
                if (dplc != null)
                {
                    dplcframe = new DPLCFrame(dplc.Name + "_" + i);
                    dplc.Add(dplcframe);
                }
                if (lowplane != null)
                {
                    SpriteToMap(sprites[i], center, lowplane, tiles, mapframe, dplcframe, startpal, false, twoplayer);
                }
                if (highplane != null)
                {
                    SpriteToMap(sprites[i], center, highplane, tiles, mapframe, dplcframe, startpal, true, twoplayer);
                }
            }
            using (MemoryStream ms = new MemoryStream(tiles.Count * 32))
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                    foreach (byte[] b in tiles)
                    {
                        bw.Write(b);
                    }
                Compression.Compress(ms.ToArray(), artfile, artcmp);
            }
            if (mapfmt == MappingsFormat.Binary)
            {
                File.WriteAllBytes(mapfile, MappingsFrame.GetBytes(map, mapgame));
                if (dplc != null)
                {
                    File.WriteAllBytes(dplcfile, DPLCFrame.GetBytes(dplc, s3kp ? EngineVersion.S2 : mapgame));
                }
            }
            else
            {
                MappingsFrame.ToASM(mapfile, map, mapgame, mapfmt == MappingsFormat.Macro);
                if (dplc != null)
                {
                    DPLCFrame.ToASM(dplcfile, dplc, mapgame, mapfmt == MappingsFormat.Macro, s3kp);
                }
            }
            if (palfile != null)
            {
                using (FileStream fs = File.Create(palfile))
                    using (BinaryWriter bw = new BinaryWriter(fs))
                        foreach (Color c in palette)
                        {
                            bw.Write(ByteConverter.GetBytes(new SonLVLColor(c).MDColor));
                        }
            }
        }
コード例 #9
0
        public static void Init()
        {
            using (Bitmap tmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
                Palette = tmp.Palette;
            SonLVLColor[] pal = SonLVLColor.Load(Properties.Resources.Palette, EngineVersion.S1);
            for (int i = 0; i < pal.Length; i++)
            {
                Palette.Entries[i] = pal[i].RGBColor;
            }
            Palette.Entries[0] = Color.Transparent;
            BitmapBits bmp = new BitmapBits(Properties.Resources.GFX);

            BitmapBits[] sprites = new BitmapBits[bmp.Width / 24];
            for (int i = 0; i < sprites.Length; i++)
            {
                sprites[i] = bmp.GetSection(i * 24, 0, 24, 24);
            }
            bmp = new BitmapBits(Properties.Resources.Font);
            BitmapBits[] font = new BitmapBits[11];
            for (int i = 0; i < 10; i++)
            {
                font[i] = bmp.GetSection(i * 8, 0, 8, 8);
            }
            font[10]      = bmp.GetSection(10 * 8, 0, 16, 8);
            ObjectBmps[0] = new BitmapBits(24, 24);
            byte ind = 1;

            // walls
            for (int p = 0; p < 4; p++)
            {
                BitmapBits tmp = new BitmapBits(sprites[0]);
                ObjectBmps[ind++] = tmp;
                for (int i = 1; i < 9; i++)
                {
                    BitmapBits t2 = new BitmapBits(tmp);
                    t2.DrawBitmapComposited(font[i], 16, 16);
                    ObjectBmps[ind++] = t2;
                }
                sprites[0].IncrementIndexes(16);
            }
            // misc
            for (int i = 1; i < 7; i++)
            {
                ObjectBmps[ind++] = sprites[i];
            }
            // "R"
            sprites[7].IncrementIndexes(16);
            ObjectBmps[ind++] = sprites[7];
            // red/white
            ObjectBmps[ind++] = sprites[8];
            // diamonds
            for (int p = 0; p < 4; p++)
            {
                BitmapBits tmp = new BitmapBits(sprites[9]);
                ObjectBmps[ind++] = tmp;
                sprites[9].IncrementIndexes(16);
            }
            // unused blocks
            ind += 3;
            // "ZONE" blocks
            for (int i = 10; i < 16; i++)
            {
                ObjectBmps[ind++] = sprites[i];
            }
            // ring
            sprites[16].IncrementIndexes(16);
            ObjectBmps[ind++] = sprites[16];
            // blue/yellow/pink/green emerald
            for (int p = 0; p < 4; p++)
            {
                BitmapBits tmp = new BitmapBits(sprites[19]);
                ObjectBmps[ind++] = tmp;
                sprites[19].IncrementIndexes(16);
            }
            // red emerald
            ObjectBmps[ind++] = sprites[17];
            // gray emerald
            ObjectBmps[ind++] = sprites[18];
            // pass-through barrier
            ObjectBmps[ind++] = sprites[20];
            ObjectBmps[0x4A]  = new BitmapBits(sprites[20]);
            ObjectBmps[0x4A].DrawBitmapComposited(font[10], 4, 8);
            ObjectBmpsNoNum = new Dictionary <byte, BitmapBits>(ObjectBmps);
            for (int p = 0; p < 4; p++)
            {
                BitmapBits tmp = ObjectBmps[(byte)(p * 9 + 1)];
                for (int i = 1; i < 9; i++)
                {
                    ObjectBmpsNoNum[(byte)(p * 9 + 1 + i)] = tmp;
                }
            }
            StartPosBmp = new BitmapBits(Properties.Resources.StartPos);
        }
コード例 #10
0
        static void Main(string[] args)
        {
            byte[]               art;
            ColorPalette         palette;
            List <MappingsFrame> map;
            List <DPLCFrame>     dplc;
            SpriteInfo           spriteInfo;

            LevelData.littleendian = false;
            LongOpt[] opts = new[] {
                new LongOpt("help", Argument.No, null, 'h'),
                new LongOpt("padding", Argument.Required, null, 'p'),
                new LongOpt("columns", Argument.Required, null, 'c'),
                new LongOpt("width", Argument.Required, null, 'w'),
                new LongOpt("background", Argument.Required, null, 'b'),
                new LongOpt("grid", Argument.No, null, 'g')
            };
            Getopt getopt     = new Getopt("SpriteSheetGen", args, Getopt.digest(opts), opts);
            int    padding    = 2;
            int    columns    = 8;
            int    width      = 0;
            bool   fixedwidth = false;
            int    gridsize   = -1;
            Color  background = Color.Transparent;
            int    opt        = getopt.getopt();

            while (opt != -1)
            {
                switch (opt)
                {
                case 'h':
                    Console.Write(Properties.Resources.HelpText);
                    return;

                case 'p':
                    padding = int.Parse(getopt.Optarg);
                    break;

                case 'c':
                    columns = int.Parse(getopt.Optarg);
                    break;

                case 'w':
                    width      = int.Parse(getopt.Optarg);
                    columns    = -1;
                    fixedwidth = true;
                    break;

                case 'g':
                    gridsize = 0;
                    break;

                case 'b':
                    if (getopt.Optarg.StartsWith("#"))
                    {
                        background = Color.FromArgb((int)(0xFF000000 | uint.Parse(getopt.Optarg.Substring(1), System.Globalization.NumberStyles.HexNumber)));
                    }
                    else
                    {
                        background = Color.FromName(getopt.Optarg.Replace(" ", ""));
                    }
                    break;
                }
                opt = getopt.getopt();
            }
            string filename;

            Console.Write("File: ");
            if (getopt.Optind == args.Length)
            {
                filename = Console.ReadLine();
            }
            else
            {
                filename = args[getopt.Optind];
                Console.WriteLine(filename);
            }
            filename = Path.GetFullPath(filename);
            Environment.CurrentDirectory = Path.GetDirectoryName(filename);
            spriteInfo = IniSerializer.Deserialize <SpriteInfo>(filename);
            if (spriteInfo.MappingsGame == EngineVersion.Invalid)
            {
                spriteInfo.MappingsGame = spriteInfo.Game;
            }
            if (spriteInfo.DPLCGame == EngineVersion.Invalid)
            {
                spriteInfo.DPLCGame = spriteInfo.Game;
            }
            MultiFileIndexer <byte> tiles = new MultiFileIndexer <byte>();

            foreach (SonicRetro.SonLVL.API.FileInfo file in spriteInfo.Art)
            {
                tiles.AddFile(new List <byte>(Compression.Decompress(file.Filename, spriteInfo.ArtCompression)), file.Offset);
            }
            art = tiles.ToArray();
            tiles.Clear();
            byte[] tmp = null;
            using (Bitmap palbmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
                palette = palbmp.Palette;
            for (int i = 0; i < 256; i++)
            {
                palette.Entries[i] = Color.Black;
            }
            foreach (PaletteInfo palent in spriteInfo.Palette)
            {
                tmp = File.ReadAllBytes(palent.Filename);
                SonLVLColor[] palfile;
                palfile = new SonLVLColor[tmp.Length / 2];
                for (int pi = 0; pi < tmp.Length; pi += 2)
                {
                    palfile[pi / 2] = new SonLVLColor(SonicRetro.SonLVL.API.ByteConverter.ToUInt16(tmp, pi));
                }
                for (int pa = 0; pa < palent.Length; pa++)
                {
                    palette.Entries[pa + palent.Destination] = palfile[pa + palent.Source].RGBColor;
                }
            }
            palette.Entries[0] = background;
            Dictionary <string, int> labels = new Dictionary <string, int>();

            tmp = null;
            switch (spriteInfo.MappingsFormat)
            {
            case MappingsFormat.Binary:
                tmp = File.ReadAllBytes(spriteInfo.MappingsFile);
                break;

            case MappingsFormat.ASM:
            case MappingsFormat.Macro:
                tmp = LevelData.ASMToBin(spriteInfo.MappingsFile, spriteInfo.MappingsGame, out labels);
                break;
            }
            map = MappingsFrame.Load(tmp, spriteInfo.MappingsGame, labels);
            if (!string.IsNullOrEmpty(spriteInfo.DPLCFile))
            {
                labels = new Dictionary <string, int>();
                tmp    = null;
                switch (spriteInfo.DPLCFormat)
                {
                case MappingsFormat.Binary:
                    tmp = File.ReadAllBytes(spriteInfo.DPLCFile);
                    break;

                case MappingsFormat.ASM:
                case MappingsFormat.Macro:
                    tmp = LevelData.ASMToBin(spriteInfo.DPLCFile, spriteInfo.DPLCGame, out labels);
                    break;
                }
                dplc = DPLCFrame.Load(tmp, spriteInfo.DPLCGame, labels);
            }
            else
            {
                dplc = null;
            }
            List <BitmapBits> spritesLow    = new List <BitmapBits>(map.Count);
            List <BitmapBits> spritesHigh   = new List <BitmapBits>(map.Count);
            List <BitmapBits> spritesMerged = new List <BitmapBits>(map.Count);
            List <Point>      offsets       = new List <Point>(map.Count);
            List <Point>      centers       = new List <Point>(map.Count);

            for (int i = 0; i < map.Count; i++)
            {
                if (map[i].TileCount == 0)
                {
                    //File.AppendAllText("log.txt", "Frame " + i + " empty.\r\n");
                    continue;
                }
                Sprite[] spr;
                if (dplc != null)
                {
                    spr = LevelData.MapFrameToBmp(art, map[i], dplc[i], spriteInfo.StartPalette);
                }
                else
                {
                    spr = LevelData.MapFrameToBmp(art, map[i], spriteInfo.StartPalette);
                }
                BitmapBits sprLow    = spr[0].Image;
                BitmapBits sprHigh   = spr[1].Image;
                BitmapBits sprMerged = new BitmapBits(sprLow);
                sprMerged.DrawBitmapComposited(sprHigh, 0, 0);
                int cx = -spr[0].X;
                int cy = -spr[0].Y;
                for (int _x = 0; _x < sprMerged.Width; _x++)
                {
                    for (int _y = 0; _y < sprMerged.Height; _y++)
                    {
                        if (sprMerged[_x, _y] != 0)
                        {
                            sprMerged = sprMerged.GetSection(_x, 0, sprMerged.Width - _x, sprMerged.Height);
                            sprLow    = sprLow.GetSection(_x, 0, sprLow.Width - _x, sprLow.Height);
                            sprHigh   = sprHigh.GetSection(_x, 0, sprHigh.Width - _x, sprHigh.Height);
                            cx       -= _x;
                            goto checkright;
                        }
                    }
                }
checkright:
                for (int _x = sprMerged.Width - 1; _x >= 0; _x--)
                {
                    for (int _y = 0; _y < sprMerged.Height; _y++)
                    {
                        if (sprMerged[_x, _y] != 0)
                        {
                            sprMerged = sprMerged.GetSection(0, 0, _x + 1, sprMerged.Height);
                            sprLow    = sprLow.GetSection(0, 0, _x + 1, sprLow.Height);
                            sprHigh   = sprHigh.GetSection(0, 0, _x + 1, sprHigh.Height);
                            goto checktop;
                        }
                    }
                }
checktop:
                for (int _y = 0; _y < sprMerged.Height; _y++)
                {
                    for (int _x = 0; _x < sprMerged.Width; _x++)
                    {
                        if (sprMerged[_x, _y] != 0)
                        {
                            sprMerged = sprMerged.GetSection(0, _y, sprMerged.Width, sprMerged.Height - _y);
                            sprLow    = sprLow.GetSection(0, _y, sprLow.Width, sprLow.Height - _y);
                            sprHigh   = sprHigh.GetSection(0, _y, sprHigh.Width, sprHigh.Height - _y);
                            cy       -= _y;
                            goto checkbottom;
                        }
                    }
                }
checkbottom:
                for (int _y = sprMerged.Height - 1; _y >= 0; _y--)
                {
                    for (int _x = 0; _x < sprMerged.Width; _x++)
                    {
                        if (sprMerged[_x, _y] != 0)
                        {
                            sprMerged = sprMerged.GetSection(0, 0, sprMerged.Width, _y + 1);
                            sprLow    = sprLow.GetSection(0, 0, sprLow.Width, _y + 1);
                            sprHigh   = sprHigh.GetSection(0, 0, sprHigh.Width, _y + 1);
                            goto checkdone;
                        }
                    }
                }
checkdone:
                spritesMerged.Add(sprMerged);
                spritesLow.Add(sprLow);
                spritesHigh.Add(sprHigh);
                centers.Add(new Point(cx, cy));
            }
            if (gridsize == 0)
            {
                for (int i = 0; i < spritesMerged.Count; i++)
                {
                    gridsize = Math.Max(gridsize, Math.Max(spritesMerged[i].Width, spritesMerged[i].Height));
                }
                if (!fixedwidth)
                {
                    width = (padding * 2 + gridsize) * columns;
                }
            }
            int x         = padding;
            int y         = padding;
            int height    = 0;
            int rowcnt    = 0;
            int rowheight = 0;

            for (int i = 0; i < spritesMerged.Count; i++)
            {
                BitmapBits spr = spritesMerged[i];
                Point      off;
                if (gridsize == -1)
                {
                    if (fixedwidth && x + spr.Width + padding > width)
                    {
                        x         = padding;
                        y        += rowheight;
                        rowheight = 0;
                    }
                    off        = new System.Drawing.Point(x, y);
                    centers[i] = new Point(centers[i].X + off.X, centers[i].Y + off.Y);
                    if (!fixedwidth)
                    {
                        width = Math.Max(width, x + spr.Width + padding);
                    }
                    height = Math.Max(height, y + spr.Height + padding);
                    if (spr.Height + 2 * padding > rowheight)
                    {
                        rowheight = spr.Height + 2 * padding;
                    }
                    if (!fixedwidth && ++rowcnt == columns)
                    {
                        x         = padding;
                        y        += rowheight;
                        rowcnt    = 0;
                        rowheight = 0;
                    }
                    else
                    {
                        x += spr.Width + 2 * padding;
                    }
                }
                else
                {
                    if (fixedwidth && x + gridsize + padding > width)
                    {
                        x  = padding;
                        y += gridsize + 2 * padding;
                    }
                    off        = new Point(x + (gridsize - spr.Width) / 2, y + (gridsize - spr.Height) / 2);
                    centers[i] = new Point(centers[i].X + off.X, centers[i].Y + off.Y);
                    height     = Math.Max(height, y + gridsize + padding);
                    if (!fixedwidth && ++rowcnt == columns)
                    {
                        x      = padding;
                        y     += gridsize + 2 * padding;
                        rowcnt = 0;
                    }
                    else
                    {
                        x += gridsize + 2 * padding;
                    }
                }
                offsets.Add(off);
            }
            BitmapBits bmpMerged = new BitmapBits(width, height);

            for (int i = 0; i < spritesMerged.Count; i++)
            {
                bmpMerged.DrawBitmap(spritesMerged[i], offsets[i]);
            }
            BitmapBits bmpLow = new BitmapBits(width, height);

            for (int i = 0; i < spritesLow.Count; i++)
            {
                bmpLow.DrawBitmap(spritesLow[i], offsets[i]);
            }
            BitmapBits bmpHigh = new BitmapBits(width, height);

            for (int i = 0; i < spritesHigh.Count; i++)
            {
                bmpHigh.DrawBitmap(spritesHigh[i], offsets[i]);
            }
            BitmapBits bmpCenter = new BitmapBits(width, height);

            for (int i = 0; i < centers.Count; i++)
            {
                bmpCenter[centers[i].X, centers[i].Y] = 1;
            }
            Console.Write("Save as: ");
            if (getopt.Optind + 1 >= args.Length)
            {
                filename = Console.ReadLine();
            }
            else
            {
                filename = args[getopt.Optind + 1];
                Console.WriteLine(filename);
            }
            filename = Path.GetFullPath(filename);
            bmpMerged.ToBitmap(palette).Save(filename);
            if (!bmpLow.Bits.FastArrayEqual(0) && !bmpHigh.Bits.FastArrayEqual(0))
            {
                bmpLow.ToBitmap(palette).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_low" + Path.GetExtension(filename)));
                bmpHigh.ToBitmap(palette).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_high" + Path.GetExtension(filename)));
            }
            bmpCenter.ToBitmap1bpp(Color.Black, Color.White).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_center" + Path.GetExtension(filename)));
        }