Esempio n. 1
0
        static void WritePalette(this StreamWriter writer, GBPalette palette)
        {
            // output 5 bit colors
            // 0bbb bbgg gggr rrrr
            foreach (var color in palette.Colors.Select(GetBGBytes))
            {
                var colorMask = (color[0]) | (color[1] << 5) | (color[2] << 10);

                var line = "dw %";
                for (int i = 15; i > 0; --i)
                {
                    var writerMask = 1 << i;
                    if ((colorMask & writerMask) == 0)
                    {
                        line += "0";
                    }
                    else
                    {
                        line += "1";
                    }
                }

                writer.WriteLine(line);
            }
        }
Esempio n. 2
0
 public ImageDisplayForm(BinFile dt, DataLabel ds)
 {
     InitializeComponent();
     dst      = ds;
     bin      = dt;
     gbs      = ds.Palette;
     labelOff = ds.Offset;
     labelLen = ds.Length;
     SetColors();
     InitializeImage();
 }
Esempio n. 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);
        }
Esempio n. 4
0
        public int UpsertPalette(GBPalette palette, List <int> pushedPalettes)
        {
            for (int i = 0; i < PaletteData.Count; i++)
            {
                if (palette.Match(PaletteData[i]))
                {
                    return(i);
                }
            }

            PaletteData.Add(palette);
            var index = PaletteData.Count - 1;

            pushedPalettes.Add(index);
            return(index);
        }
Esempio n. 5
0
        static GBTexture GetTexture(BitmapTexture bitmapTexture, GBPalette palette)
        {
            if (bitmapTexture == null)
            {
                return(GBTexture.Default);
            }

            var tex = new GBTexture();

            for (int y = 0; y < GBTexture.WidthPx; y++)
            {
                for (int x = 0; x < GBTexture.WidthPx; x++)
                {
                    var color   = bitmapTexture.Get(x, y);
                    var bgColor = palette.GetGBColor(color);
                    tex.Set(x, y, bgColor);
                }
            }

            return(tex);
        }
Esempio n. 6
0
 public DataLabel(int newOffset, int newLength = 1, string labelName = "", string printTemplate = "", string cmt = "", DataSectionType dst = DataSectionType.Data, GBPalette pal = null)
 {
     m_id = System.Threading.Interlocked.Increment(ref counter);
     Value = newOffset;
     _length = newLength;
     Name = labelName == string.Empty ? string.Format("DS_{0:X6}", newOffset) : labelName;
     Comment = "";
     if (!string.IsNullOrEmpty(cmt))
     {
         Comment = cmt;
     }
     dataSectType = dst;
     if (pal != null)
     {
         palette = pal;
     }
     _printTemplate = printTemplate;
     if (_printTemplate == "")
     {
         _printTemplate = "b";
     }
 }
Esempio n. 7
0
 public DataLabel(int newOffset, int newLength = 1, string labelName = "", string printTemplate = "", string cmt = "", DataSectionType dst = DataSectionType.Data, GBPalette pal = null)
 {
     m_id    = System.Threading.Interlocked.Increment(ref counter);
     Value   = newOffset;
     _length = newLength;
     Name    = labelName == string.Empty ? string.Format("DS_{0:X6}", newOffset) : labelName;
     Comment = "";
     if (!string.IsNullOrEmpty(cmt))
     {
         Comment = cmt;
     }
     dataSectType = dst;
     if (pal != null)
     {
         palette = pal;
     }
     _printTemplate = printTemplate;
     if (_printTemplate == "")
     {
         _printTemplate = "b";
     }
 }
Esempio n. 8
0
        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();
            }
        }
Esempio n. 9
0
        // TODO: Make sure everything saves and loads properly.
        public void LoadLabelFile(string fileName)
        {
            using (TextReader labelFile = new StreamReader(fileName))
            {
                using (TextWriter errFile = new StreamWriter("err.txt"))
                {
                    if (labelFile.ReadLine() != "gbr")
                    {
                        return;
                    }
                    else
                    {
                        var items = new List<Dictionary<string, string>>();
                        int curItem = -1;
                        string currentLine;
                        while ((currentLine = labelFile.ReadLine()) != null)
                        {
                            switch (currentLine)
                            {
                                case ".label":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "label" } });
                                        curItem++;
                                    }
                                    break;

                                case ".data":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "data" } });
                                        curItem++;
                                    }
                                    break;

                                case ".var":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "var" } });
                                        curItem++;
                                    }
                                    break;

                                case ".comment":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "comment" } });
                                        curItem++;
                                    }
                                    break;

                                default:
                                    if (curItem == -1)
                                    {
                                        break;
                                    }
                                    string[] opt = currentLine.Split(new[] { ':' }, 2);
                                    if (opt.Length == 1)
                                    {
                                        if (items[curItem].ContainsKey("_c"))
                                        {
                                            items[curItem]["_c"] += "\n" + currentLine;
                                        }
                                        else
                                        {
                                            items[curItem].Add("_c", currentLine);
                                        }
                                    }
                                    else
                                    {
                                        if (items[curItem].ContainsKey(opt[0]))
                                        {
                                            items[curItem][opt[0]] = opt[1];
                                        }
                                        else
                                        {
                                            items[curItem].Add(opt[0], opt[1]);
                                        }
                                    }
                                    break;
                            }
                        }
                        foreach (var currentItem in items)
                        {
                            switch (currentItem["tag"])
                            {
                                case "label":
                                    {
                                        int offset = 0;
                                        string name = "";
                                        string comment = "";
                                        bool offsetGood = false;
                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }
                                        if (offsetGood)
                                        {
                                            FunctionLabel fl = new FunctionLabel(offset, name, comment);
                                            AddFuncLabel(fl);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "data":
                                    {
                                        int offset = -1;
                                        int length = -1;
                                        string printTemp = "";
                                        string name = "";
                                        string comment = "";
                                        DataSectionType dst = DataSectionType.Data;
                                        GBPalette gbp = new GBPalette();
                                        bool offsetGood = false;
                                        bool lengthGood = false;
                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_l":
                                                    {
                                                        lengthGood = Utility.OffsetStringToInt(kvp.Value, out length);
                                                    }
                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_d":
                                                    {
                                                        printTemp = kvp.Value;
                                                    }
                                                    break;

                                                case "_p1":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_1);
                                                    }

                                                    break;

                                                case "_p2":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_2);
                                                    }

                                                    break;

                                                case "_p3":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_3);
                                                    }

                                                    break;

                                                case "_p4":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_4);
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }

                                        if (offsetGood && lengthGood)
                                        {
                                            DataLabel ds = new DataLabel(offset, length, name, printTemp, comment, dst, gbp);
                                            AddDataLabel(ds);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "var":
                                    {
                                        int variable = -1;
                                        string name = "";
                                        string comment = "";
                                        bool variableGood = false;

                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_v":
                                                    {
                                                        variableGood = Utility.OffsetStringToInt(kvp.Value, out variable);
                                                    }

                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }

                                        if (variableGood)
                                        {
                                            VarLabel vl = new VarLabel(variable, name, comment);
                                            AddVarLabel(vl);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "comment":
                                    {
                                        int offset = 0;
                                        string name = String.Empty;
                                        string comment = "";
                                        bool offsetGood = false;

                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }
                                        if (offsetGood)
                                        {
                                            AddComment(offset, comment);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }
                                    break;

                                default:

                                    break;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 10
0
        // TODO: Make sure everything saves and loads properly.

        public void LoadLabelFile(string fileName)
        {
            using (TextReader labelFile = new StreamReader(fileName))
            {
                using (TextWriter errFile = new StreamWriter("err.txt"))
                {
                    if (labelFile.ReadLine() != "gbr")
                    {
                        return;
                    }
                    else
                    {
                        var    items   = new List <Dictionary <string, string> >();
                        int    curItem = -1;
                        string currentLine;
                        while ((currentLine = labelFile.ReadLine()) != null)
                        {
                            switch (currentLine)
                            {
                            case ".label":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "label" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".data":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "data" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".var":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "var" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".comment":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "comment" }
                                    });
                                curItem++;
                            }
                            break;

                            default:
                                if (curItem == -1)
                                {
                                    break;
                                }
                                string[] opt = currentLine.Split(new[] { ':' }, 2);
                                if (opt.Length == 1)
                                {
                                    if (items[curItem].ContainsKey("_c"))
                                    {
                                        items[curItem]["_c"] += "\n" + currentLine;
                                    }
                                    else
                                    {
                                        items[curItem].Add("_c", currentLine);
                                    }
                                }
                                else
                                {
                                    if (items[curItem].ContainsKey(opt[0]))
                                    {
                                        items[curItem][opt[0]] = opt[1];
                                    }
                                    else
                                    {
                                        items[curItem].Add(opt[0], opt[1]);
                                    }
                                }
                                break;
                            }
                        }
                        foreach (var currentItem in items)
                        {
                            switch (currentItem["tag"])
                            {
                            case "label":
                            {
                                int    offset     = 0;
                                string name       = "";
                                string comment    = "";
                                bool   offsetGood = false;
                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }
                                if (offsetGood)
                                {
                                    FunctionLabel fl = new FunctionLabel(offset, name, comment);
                                    AddFuncLabel(fl);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "data":
                            {
                                int             offset     = -1;
                                int             length     = -1;
                                string          printTemp  = "";
                                string          name       = "";
                                string          comment    = "";
                                DataSectionType dst        = DataSectionType.Data;
                                GBPalette       gbp        = new GBPalette();
                                bool            offsetGood = false;
                                bool            lengthGood = false;
                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_l":
                                    {
                                        lengthGood = Utility.OffsetStringToInt(kvp.Value, out length);
                                    }
                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_d":
                                    {
                                        printTemp = kvp.Value;
                                    }
                                    break;

                                    case "_p1":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_1);
                                    }

                                    break;

                                    case "_p2":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_2);
                                    }

                                    break;

                                    case "_p3":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_3);
                                    }

                                    break;

                                    case "_p4":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_4);
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }

                                if (offsetGood && lengthGood)
                                {
                                    DataLabel ds = new DataLabel(offset, length, name, printTemp, comment, dst, gbp);
                                    AddDataLabel(ds);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "var":
                            {
                                int    variable     = -1;
                                string name         = "";
                                string comment      = "";
                                bool   variableGood = false;

                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_v":
                                    {
                                        variableGood = Utility.OffsetStringToInt(kvp.Value, out variable);
                                    }

                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }

                                if (variableGood)
                                {
                                    VarLabel vl = new VarLabel(variable, name, comment);
                                    AddVarLabel(vl);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "comment":
                            {
                                int    offset     = 0;
                                string name       = String.Empty;
                                string comment    = "";
                                bool   offsetGood = false;

                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }
                                if (offsetGood)
                                {
                                    AddComment(offset, comment);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }
                            break;

                            default:

                                break;
                            }
                        }
                    }
                }
            }
        }