Example #1
0
        public static List <Metatile> CreateMetatiles(List <byte> metatileBytes, List <byte> metatileAttributeBytes)
        {
            // Create Metatiles
            int numMetatiles = metatileBytes.Count / 16;

            List <Metatile> metatiles = new List <Metatile>();

            for (int i = 0; i < numMetatiles; i++)
            {
                Metatile metatile = new Metatile();
                int      index    = i * (2 * TILES_PER_LAYER * LAYERS);
                for (int j = 0; j < TILES_PER_LAYER * LAYERS; j++)
                {
                    int word = metatileBytes[index++] & 0xff;
                    word += (metatileBytes[index++] & 0xff) << 8;
                    Tile tile = new Tile((ushort)word);

                    metatile.tiles.Add(tile);
                }
                metatiles.Add(metatile);
            }

            // Set metatile attributes
            int numMetatileAttrs = metatileAttributeBytes.Count / 4;

            for (int i = 0; i < numMetatileAttrs; i++)
            {
                int value = ((metatileAttributeBytes[i * 4 + 3]) << 24) |
                            ((metatileAttributeBytes[i * 4 + 2]) << 16) |
                            ((metatileAttributeBytes[i * 4 + 1]) << 8) |
                            ((metatileAttributeBytes[i * 4 + 0]));
                metatiles[i].behavior      = (ushort)(value & 0x1FF);
                metatiles[i].terrainType   = (ushort)((value & 0x3E00) >> 9);
                metatiles[i].encounterType = (ushort)((value & 0x7000000) >> 24);
                metatiles[i].layerType     = (ushort)((value & 0x60000000) >> 29);
            }

            return(metatiles);
        }
Example #2
0
        public static List <Tileset> CreateTilesets(string readPath)
        {
            string headerIncText    = File.ReadAllText(readPath + @"\data\tilesets\headers.inc");
            string graphicsIncText  = File.ReadAllText(readPath + @"\data\tilesets\graphics.inc");
            string metatilesIncText = File.ReadAllText(readPath + @"\data\tilesets\metatiles.inc");

            List <Tileset> tilesets = new List <Tileset>();

            // Split header entries
            string[] splitString   = { "\n\t.align 2\n" };
            string[] headerEntries = headerIncText.Split(splitString, StringSplitOptions.RemoveEmptyEntries);

            // Split graphics entries
            string[] graphicsEntries = graphicsIncText.Split(splitString, StringSplitOptions.RemoveEmptyEntries);

            // Split metatiles entries
            string[] splitStringMetatiles = { "\n\t.align 1\n" };
            string[] metatilesEntries     = metatilesIncText.Split(splitStringMetatiles, StringSplitOptions.RemoveEmptyEntries);

            // Get data from .inc files
            for (int i = 0; i < headerEntries.Length; i++)
            {
                // Create tileset from header.inc
                Tileset tileset = new Tileset(headerEntries[i]);


                // Add paths from graphics.inc
                bool setTiles    = false;
                bool setPalettes = false;

                for (int j = 0; j < graphicsEntries.Length; j++)
                {
                    if (graphicsEntries[j].Contains(tileset.tiles_label))
                    {
                        tileset.tilesPath = graphicsEntries[j].Substring(
                            graphicsEntries[j].IndexOf("\"")).Replace("\"", "").Replace("\n", "");
                        setTiles = true;
                    }
                    else if (graphicsEntries[j].Contains(tileset.palettes_label))
                    {
                        string[] splitStringPalettes = { "\n\t.incbin " };
                        string[] lines = graphicsEntries[j].Split(splitStringPalettes, StringSplitOptions.RemoveEmptyEntries);

                        for (int k = 1; k < lines.Length; k++) // Skip first blank entry
                        {
                            tileset.palettePaths.Add(lines[k].Replace("\"", "").Replace("\n", ""));
                        }

                        setPalettes = true;
                    }

                    if (setTiles && setPalettes)
                    {
                        break;
                    }
                }

                // Add paths from metatiles.inc
                bool setMetatiles           = false;
                bool setMetatilesAttributes = false;

                for (int j = 0; j < metatilesEntries.Length; j++)
                {
                    if (metatilesEntries[j].Contains(tileset.metatiles_label))
                    {
                        tileset.metatiles_path = metatilesEntries[j].Substring(
                            metatilesEntries[j].IndexOf("\"")).Replace("\"", "").Replace("\n", "");
                        setMetatiles = true;
                    }
                    else if (metatilesEntries[j].Contains(tileset.metatile_attrs_label))
                    {
                        tileset.metatile_attrs_path = metatilesEntries[j].Substring(
                            metatilesEntries[j].IndexOf("\"")).Replace("\"", "").Replace("\n", "");

                        setMetatilesAttributes = true;
                    }

                    if (setMetatiles && setMetatilesAttributes)
                    {
                        break;
                    }
                }

                // WORKAROUND: Some entries are not in graphics.inc but in graphics.c, just add them using the same path structure
                if (!setTiles)
                {
                    tileset.tilesPath = tileset.metatiles_path.Replace("metatiles.bin", "tiles.png");
                }
                if (!setPalettes)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        tileset.palettePaths.Add(tileset.metatiles_path.Replace("metatiles.bin", "palettes/" + j.ToString("D2") + ".pal"));
                    }
                }

                // Assign image of tiles
                //if (tileset.tilesPath != null) /// TODO: Add fallback, either read from graphics.c or generate paths
                {
                    tileset.tilesImage = (Bitmap)Image.FromFile(readPath + @"\" + tileset.tilesPath.Replace("4bpp", "png").Replace(".lz", ""));

                    // Assign palettes
                    tileset.palettes = new List <Palette>();
                    for (int j = 0; j < tileset.palettePaths.Count; j++)
                    {
                        tileset.palettes.Add(new Palette(File.ReadAllText(readPath + @"\" + tileset.palettePaths[j].Replace("gbapal", "pal"))));
                    }
                }

                // Create metatiles
                List <byte> metatileBytes          = File.ReadAllBytes(readPath + @"\" + tileset.metatiles_path).ToList <byte>();
                List <byte> metatileAttributeBytes = File.ReadAllBytes(readPath + @"\" + tileset.metatile_attrs_path).ToList <byte>();

                tileset.metatiles = Metatile.CreateMetatiles(metatileBytes, metatileAttributeBytes);

                tilesets.Add(tileset);
            }

            return(tilesets);
        }