示例#1
0
        public RomData Parse(string path)
        {
            var bitmap = new System.Drawing.Bitmap(path);

            var bitmapFrames = BitmapTexture.FromBitmap(bitmap);

            var romData = new RomData()
            {
                Frames      = new List <Frame>(),
                PaletteData = new List <GBPalette>(),
                TextureData = new List <GBTexture>(),
            };

            foreach (var bitmapFrame in bitmapFrames)
            {
                var frame = ParseFrame(romData, bitmapFrame);

                frame.Index    = romData.Frames.Count;
                frame.Duration = 16 * 2;

                romData.Frames.Add(frame);
            }

            return(romData);
        }
        static void WriteRomData(this StreamWriter writer, RomData romData)
        {
            writer.WritePaletteGroups(romData);

            writer.WriteTextures(romData);

            writer.WriteFrames(romData);

            int a = 0;
        }
示例#3
0
        public static Frame ParseFrame(RomData romData, BitmapTexture[,] bitmapFrame)
        {
            var frame = new Frame();

            // parse palettes and textures
            GBPalette[,] palettes = new GBPalette[Frame.Height, Frame.Width];
            GBTexture[,] textures = new GBTexture[Frame.Height, Frame.Width];

            var pushedPalettes = new List <int>();
            var pushedTextures = new List <int>();

            foreach (var vec in ArrayUtility.ForEach(Frame.Height, Frame.Width))
            {
                var palette = GetPalette(bitmapFrame[vec.y, vec.x]);
                var texture = GetTexture(bitmapFrame[vec.y, vec.x], palette);

                palettes[vec.y, vec.x] = GetPalette(bitmapFrame[vec.y, vec.x]);
                textures[vec.y, vec.x] = GetTexture(bitmapFrame[vec.y, vec.x], palettes[vec.y, vec.x]);

                // Add Palette / Texture to rom
                var paletteIndex = romData.UpsertPalette(palette, pushedPalettes);
                var textureIndex = romData.UpsertTexture(texture, pushedTextures);

                // Add tile to frame
                var tile = new GBTile();
                tile.Palette = romData.PaletteData[paletteIndex];
                tile.Texture = romData.TextureData[textureIndex];
                frame.TileUpdates[vec.y, vec.x] = tile;
            }

            foreach (var vec in ArrayUtility.ForEach(Frame.Height, Frame.Width))
            {
                var index = romData.TextureData.IndexOf(frame.TileUpdates[vec.y, vec.x].Texture);
                if (index == -1)
                {
                    throw new Exception("Texture missing at " + vec.x + ", " + vec.y);
                }
            }

            pushedPalettes.Sort();
            pushedTextures.Sort();

            // palettes changes
            frame.PaletteUpdates = pushedPalettes.ToDictionary(i => i, i => romData.PaletteData[i]);

            // texture changes
            frame.LoadOrders = GetLoads(pushedTextures).ToList();

            return(frame);
        }
        public static void Export(string path, RomData romData)
        {
            var extension = Path.GetExtension(path);

            if (extension == ".json")
            {
                var json = JsonConvert.SerializeObject(romData, Formatting.Indented);
                File.WriteAllText(path, json);
            }
            else if (extension == ".asm")
            {
                var writer = new StreamWriter(path);
                writer.WriteRomData(romData);
                writer.Close();
            }
            else
            {
                throw new NotImplementedException(string.Format("extension {0} not implemented", extension));
            }
        }
        static void WritePaletteGroups(this StreamWriter writer, RomData romData)
        {
            var paletteGroups = new GBPalette[][]
            {
                romData.PaletteData.Take(4).ToArray(),
                romData.PaletteData.Skip(4).Take(4).ToArray(),
            };

            writer.WriteComment("Palatte Groups 2 total");
            writer.WriteLine();

            var index = 0;

            foreach (var paletteGroup in paletteGroups)
            {
                writer.WriteComment(string.Format("Palatte Groups [{0}]", index++));
                foreach (var palette in paletteGroup)
                {
                    writer.WritePalette(palette);
                }
                writer.WriteLine();
            }
        }
        static void WriteTextures(this StreamWriter writer, RomData romData)
        {
            writer.WriteComment(string.Format("Rom Textures {0}", romData.TextureData.Count));
            writer.WriteLine();

            var index = 0;

            foreach (var texture in romData.TextureData)
            {
                writer.WriteComment(string.Format("Rom Textures [{0}]", index++));

                for (int rowIndex = 0; rowIndex < GBTexture.WidthPx; rowIndex++)
                {
                    writer.Write("dw `");
                    for (int colIndex = 0; colIndex < GBTexture.WidthPx; colIndex++)
                    {
                        writer.Write(((int)texture.Get(rowIndex, colIndex)).ToString());
                    }
                    writer.WriteLine(); // finish
                }
                writer.WriteLine();
            }
        }
        static void WriteFrames(this StreamWriter writer, RomData romData)
        {
            writer.WriteComment(string.Format("Frames ({0} total)", romData.Frames.Count));
            writer.WriteLine();

            var index = 0;

            foreach (var frame in romData.Frames)
            {
                writer.WriteComment(string.Format("Frames [{0}]", index++));

                const int TileRowBytes = 32;

                writer.WriteComment(string.Format("Frames [{0}] Tiles - vram indexes", index));
                var defaultTile = frame.TileUpdates[0, 0];
                for (int row = 0; row < Frame.Height; row++)
                {
                    writer.Write("db "); // declare byte
                    for (int col = 0; col < Frame.Width; col++)
                    {
                        var tile         = frame.TileUpdates[row, col] ?? defaultTile;
                        var textureIndex = romData.TextureData.IndexOf(tile.Texture);

                        if (col > 0)
                        {
                            writer.Write(", ");
                        }
                        writer.Write(textureIndex.ToString());
                    }

                    writer.WriteLine();
                    writer.WritePadding(TileRowBytes - Frame.Width, false);
                }
                writer.WriteLine();

                writer.WriteComment(string.Format("Frames [{0}] Tiles - flags", index));
                for (int row = 0; row < Frame.Height; row++)
                {
                    writer.Write("db "); // declare byte
                    for (int col = 0; col < Frame.Width; col++)
                    {
                        var tile         = frame.TileUpdates[row, col] ?? defaultTile;
                        var paletteIndex = romData.TextureData.IndexOf(tile.Texture);

                        //0vh0bppp
                        // v = flip vert
                        // h = flip horiontal
                        // b = palette bank
                        // p = palette index

                        // defaults + set palette index
                        var flags = paletteIndex;

                        if (col > 0)
                        {
                            writer.Write(", ");
                        }
                        writer.Write(flags.ToString());
                    }

                    writer.WriteLine();
                    writer.WritePadding(TileRowBytes - Frame.Width, true);
                }
                writer.WriteLine();

                writer.WriteComment(string.Format("Frames [{0}] palette per changes", index));
                writer.WriteComment("use index 0 (144*2)");
                writer.WriteLine("ds " + (144 * 2));
                writer.WriteLine();

                writer.WriteComment(string.Format("Frames [{0}] palette (end) changes", index));
                writer.WriteComment("use index 0");
                writer.WriteLine("ds " + 2);
                writer.WriteLine();

                writer.WriteComment(string.Format("Frames [{0}] scroll", index));
                writer.WriteLine("db " + 0);
                writer.WriteLine();
            }
        }