Beispiel #1
0
        public SpriteCache(Map map)
        {
            foreach (var tileset in map.tileSets)
            {
                var info = new TilesetInfo();
                info.firstGid  = tileset.firstGid;
                info.assetPath = TiledHelpers.GetAssetPath(tileset.source);

                var sprites = AssetDatabase.LoadAllAssetsAtPath(info.assetPath)
                              .OfType <Sprite>()
                              .ToArray();

                info.lastGid = (uint)(info.firstGid + sprites.Length - 1);
                _tilesetInfo.Add(info);
            }
        }
        protected static void ProcessTileset(XmlElement element, IsoMap map)
        {
            MapImage img = new MapImage();

            TilesetInfo info = new TilesetInfo();
            img.Tag = info;

            foreach (XmlAttribute attrib in element.Attributes)
            {
                if (attrib.Name == "name")
                    img.Name = attrib.Value;
                else if (attrib.Name == "firstgid")
                    info.FirstGUID = int.Parse(attrib.Value);
                else if (attrib.Name == "tilewidth")
                    img.Grid = new Size(int.Parse(attrib.Value), img.Grid.Height);
                else if (attrib.Name == "tileheight")
                    img.Grid = new Size(img.Grid.Width,int.Parse(attrib.Value));
            }

            foreach (XmlElement child in element.ChildNodes)
            {
                if (child.Name == "image")
                {
                    foreach (XmlAttribute attrib in child.Attributes)
                    {
                        if (attrib.Name == "source")
                            img.FileName = attrib.Value;
                    }
                }
            }
            img.ID = map.Tilesets.Count;
            map.Tilesets.Add(img.ID, img);
        }
Beispiel #3
0
        private void DrawTileset()
        {
            //currentTilesetInfo = TilesetInfo.ReadTilesetInfo(currentRoom.StateInfo[currentRoomState].StateData.TilesetIndex);
            currentTilesetInfo = TilesetInfo.ReadTilesetInfo(3);
            currentTileset     = Tileset.ReadTilesetFromInfo(currentTilesetInfo);

            // Load global palette list
            for (int i = 0; i < 9; ++i)
            {
                TilesetInfo info = TilesetInfo.ReadTilesetInfo(i);
                palettes.Add(Tileset.ReadPalette(info.PaletteAddress.ToPointer()));
            }

            // Combine common and unique tile graphics data
            //byte[] allTileData = new byte[currentTileset.TileGraphics.CommonTileGraphics.Length + currentTileset.TileGraphics.UniqueTileGraphics.Length];
            byte[] allTileData = new byte[0x5000 + currentTileset.TileGraphics.CommonTileGraphics.Length];  // should be 0x8000 instead of 0x5000 for tileset 26 or 17?
            Array.Copy(currentTileset.TileGraphics.UniqueTileGraphics, 0, allTileData, 0, currentTileset.TileGraphics.UniqueTileGraphics.Length);
            Array.Copy(currentTileset.TileGraphics.CommonTileGraphics, 0, allTileData, 0x5000, currentTileset.TileGraphics.CommonTileGraphics.Length);

            // Convert indexed tile graphics to a paletted pixelmap
            byte[] allTilePixelData = Tileset.IndexedGraphicsToPixelData(allTileData);

            // Apparently used to add 0x5000 in earlier versions of SMILE, but was later changed to 0x6000.
            // I've opted to simply add the length of unique tile graphics just to be more precise.
            int tileCount = allTileData.Length / 32;

            // DISABLED UNTIL I CAN FIGURE OUT WHAT'S WRONG

            int         tileDrawSize = 8;
            int         lineSize     = 32; // Number of blocks to draw per line in the block preview
            SKImageInfo imageInfo    = new SKImageInfo((tileCount * tileDrawSize * 2) / lineSize, (lineSize * tileDrawSize * 2));

            using SKSurface surface = SKSurface.Create(imageInfo);
            SKCanvas canvas = surface.Canvas;

            canvas.Clear(SKColors.Transparent);

            byte[] combinedTileTable = new byte[currentTileset.TileTables.CommonTileTable.Length + currentTileset.TileTables.UniqueTileTable.Length];
            Array.Copy(currentTileset.TileTables.CommonTileTable, 0, combinedTileTable, 0, currentTileset.TileTables.CommonTileTable.Length);
            Array.Copy(currentTileset.TileTables.UniqueTileTable, 0, combinedTileTable, currentTileset.TileTables.CommonTileTable.Length, currentTileset.TileTables.UniqueTileTable.Length);

            for (int blockNum = 0; blockNum < combinedTileTable.Length; ++blockNum)
            {
                ushort[] blockEntry = Tileset.GetTileTableEntry(blockNum, combinedTileTable);

                int lineNum = blockNum / lineSize;
                for (int e = 0; e < 4; ++e)
                {
                    SKBitmap tileBitmap = new SKBitmap(8, 8);
                    ushort   tileData   = blockEntry[e];
                    ushort   tileNum    = (ushort)(tileData & 0x3FF);
                    bool     xFlip      = ((tileData & 0b0100000000000000) >> 14) == 1;
                    bool     yFlip      = ((tileData & 0b1000000000000000) >> 15) == 1;
                    byte     paletteNum = (byte)((tileData & 0b0001110000000000) >> 10);

                    for (byte y = 0; y < 8; ++y)
                    {
                        for (byte x = 0; x < 8; ++x)
                        {
                            // Get palette color for pixel
                            int  trueOffset = (tileNum * 64) + (y * 8) + x;
                            uint colorIndex = allTilePixelData[trueOffset];

                            // Get tile palette
                            uint[] palette     = PaletteConverter.SnesPaletteToPcPalette(currentTileset.Palette, false);
                            uint[] paletteMask = PaletteConverter.SnesPaletteToPcPalette(currentTileset.Palette, true);

                            SKColor pixelColor = SKColor.Parse(palette[colorIndex].ToString("X6"));

                            if (xFlip && yFlip)
                            {
                                tileBitmap.SetPixel(7 - x, 7 - y, pixelColor);
                            }
                            else if (xFlip)
                            {
                                tileBitmap.SetPixel(7 - x, y, pixelColor);
                            }
                            else if (yFlip)
                            {
                                tileBitmap.SetPixel(x, 7 - y, pixelColor);
                            }
                            else
                            {
                                tileBitmap.SetPixel(x, y, pixelColor);
                            }
                        }
                    }

                    int drawStartX, drawStartY, drawEndX, drawEndY;
                    drawStartX = (((blockNum % lineSize) * 2) + (e % 2)) * tileDrawSize;
                    drawEndX   = (((blockNum % lineSize) * 2) + (e % 2) + 1) * tileDrawSize;
                    drawStartY = ((lineNum * 2) + (e / 2)) * tileDrawSize;
                    drawEndY   = ((lineNum * 2) + (e / 2) + 1) * tileDrawSize;

                    canvas.DrawBitmap(tileBitmap, new SKRect(drawStartX, drawStartY, drawEndX, drawEndY));
                }
            }


            /*
             * // Draw the raw tileset image instead of block data
             * int lineSize = 16;
             * int tileDrawSize = 32;
             * SKImageInfo imageInfo = new SKImageInfo(lineSize * tileDrawSize, (tileCount / lineSize) * tileDrawSize);
             * using SKSurface surface = SKSurface.Create(imageInfo);
             * SKCanvas canvas = surface.Canvas;
             * canvas.Clear(SKColors.Transparent);
             *
             * for (int tileNum = 0; tileNum < tileCount; ++tileNum)
             * {
             *  int lineNum = tileNum / lineSize;
             *  SKBitmap tileBitmap = new SKBitmap(8, 8);
             *  for (byte y = 0; y < 8; ++y)
             *  {
             *      for (byte x = 0; x < 8; ++x)
             *      {
             *          // Get palette color for pixel
             *          uint colorIndex = allTilePixelData[(tileNum * 64) + (y * 8) + x];
             *          uint pixelColor = pcPalette[colorIndex];
             *          tileBitmap.SetPixel(x, y, SKColor.Parse(pixelColor.ToString("X6")));
             *      }
             *  }
             *  int drawStartX, drawEndX, drawStartY, drawEndY;
             *  drawStartX = (tileNum % lineSize) * tileDrawSize;
             *  drawEndX = ((tileNum % lineSize) + 1) * tileDrawSize;
             *  drawStartY = lineNum * tileDrawSize;
             *  drawEndY = (lineNum + 1) * tileDrawSize;
             *  canvas.DrawBitmap(tileBitmap, new SKRect(drawStartX, drawStartY, drawEndX, drawEndY));
             *
             *  //Mark each tile with its index for debugging assistance
             *  SKTextBlobBuilder textBuilder = new SKTextBlobBuilder();
             *  SKPaint fontPaint = new SKPaint();
             *  fontPaint.Color = SKColors.Red;
             *  fontPaint.StrokeWidth = 5;
             *  fontPaint.Typeface = SKTypeface.FromFamilyName("Courier New", SKFontStyle.Bold);
             *  fontPaint.TextSize = 14;
             *  canvas.DrawText(tileNum.ToString(), drawStartX, drawEndY, fontPaint);
             * }
             */

            // Copy data to output Image
            using SKImage image        = surface.Snapshot();
            using SKData data          = image.Encode(SKEncodedImageFormat.Png, 100);
            using MemoryStream mStream = new MemoryStream(data.ToArray());
            BitmapImage bmp = new BitmapImage();

            bmp.BeginInit();
            bmp.StreamSource = mStream;
            bmp.CacheOption  = BitmapCacheOption.OnLoad;
            bmp.EndInit();

            tileset_OutputImage.Source = bmp;
        }