예제 #1
0
        public static Bitmap GetLayer2(int room, int flags)
        {
            var rInfo   = RoomInfo.RoomInfoEntries[room];
            var rGfxPal = RoomGfxPal.RoomGfxPalEntries[room];

            // Get the width and height (in 16*16 tiles)
            int w = rInfo.Width;
            int h = rInfo.Height;

            // Create the bitmap
            Bitmap     bmp = new Bitmap(w << 4, h << 4, PixelFormat.Format8bppIndexed);
            BitmapData bd  = bmp.LockBits(ImageLockMode.WriteOnly);

            // Get the graphics
            var gfx  = RoomGfx.GetTilesets(rGfxPal);
            int hash = gfx.GetHashCode();

            // Get the tiles
            var tiles = RoomTiles.GetTiles(room);

            if (tiles == null)
            {
                return(null);
            }

            // Get the map
            var map = new byte[1][];

            for (int i = 1; i < 2; i++)
            {
                map[i - 1] = RoomMap.GetMap(room, i);
            }

            // Get the palette
            var pal = RoomPal.GetPalette(rGfxPal.Pal);

            bmp.CopyPalette(pal, true);

            // For each layer...
            for (int layer = 1; layer >= 1; layer--)
            {
                // Check if it's in the flags for rendering this layer
                if (((flags & (1 << layer)) != 0) && (map[layer - 1] != null))
                {
                    // Make sure the map is big enough: some of them aren't!
                    if (map[layer - 1].Length >= (w * h * 2))
                    {
                        // We're drawing it, so for each entry in the map...
                        for (int mapy = 0; mapy < h; mapy++)
                        {
                            for (int mapx = 0; mapx < w; mapx++)
                            {
                                // Get the 16x16 tile number
                                ushort ch     = map[layer - 1].ReadUShort((mapx + (mapy * w)) * 2);
                                ushort tile16 = (ushort)(ch & 0x3FF);

                                if ((tile16 >> 6) < 12)
                                {
                                    int tpal = (ch >> 10) & 0xF;

                                    bool tflipx = (ch & 0x4000) != 0;
                                    bool tflipy = (ch & 0x8000) != 0;

                                    // For this tile... get the four 8x8 subtiles
                                    int[,] tile8   = new int[2, 2];
                                    bool[,] sflipx = new bool[2, 2];
                                    bool[,] sflipy = new bool[2, 2];

                                    uint magic = tiles.ReadUInt(tile16 * 8);

                                    tile8[0, 0] = tiles[(tile16 * 8) + 4];
                                    tile8[0, 1] = tiles[(tile16 * 8) + 5];
                                    tile8[1, 0] = tiles[(tile16 * 8) + 6];
                                    tile8[1, 1] = tiles[(tile16 * 8) + 7];

                                    for (int i = 0; i < 2; i++)
                                    {
                                        for (int j = 0; j < 2; j++)
                                        {
                                            sflipx[i, j] = (tile8[i, j] & 0x40) != 0;
                                            sflipy[i, j] = (tile8[i, j] & 0x80) != 0;

                                            tile8[i, j] &= 0x3F;
                                            tile8[i, j] |= (ch & 0x3C0);
                                        }
                                    }

                                    // magic appears to contain some sort of tile mask...
                                    uint mask = (magic >> 16) & 0xF;
                                    if ((mask & 0x1) == 0)
                                    {
                                        tile8[0, 0] = -1;
                                    }
                                    if ((mask & 0x2) == 0)
                                    {
                                        tile8[0, 1] = -1;
                                    }
                                    if ((mask & 0x4) == 0)
                                    {
                                        tile8[1, 0] = -1;
                                    }
                                    if ((mask & 0x8) == 0)
                                    {
                                        tile8[1, 1] = -1;
                                    }

                                    // For each of these subtiles...
                                    for (int tiley = 0; tiley < 2; tiley++)
                                    {
                                        for (int tilex = 0; tilex < 2; tilex++)
                                        {
                                            if (tile8[tiley, tilex] >= 0)
                                            {
                                                int tileset = tile8[tiley, tilex] >> 6;
                                                int subtile = tile8[tiley, tilex] & 0x3F;

                                                int tileoffset = subtile << 5;

                                                var pixels = gfx[tileset].Read4BppTile(tileoffset);

                                                int tx = tflipx ? 1 - tilex : tilex;
                                                int ty = tflipy ? 1 - tiley : tiley;

                                                GfxProvider.RenderToBitmap(bd, pixels,
                                                                           (mapx << 4) + (tx << 3),
                                                                           (mapy << 4) + (ty << 3),
                                                                           sflipx[tiley, tilex] ^ tflipx,
                                                                           sflipy[tiley, tilex] ^ tflipy,
                                                                           tpal, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bmp.UnlockBits(bd);
            return(bmp);
        }