Example #1
0
        private static void SpriteToMap(Rectangle sprite, Point center, BitmapBits plane, List <byte[]> tiles, MappingsFrame mapframe, DPLCFrame dplcframe, byte startpal, bool pri, bool twoplayer)
        {
            BitmapBits bmp = plane.GetSection(sprite);

            if (bmp.Width % 8 != 0 || bmp.Height % 8 != 0)
            {
                int w = bmp.Width;
                if (w % 8 != 0)
                {
                    w = (w & ~7) + 8;
                }
                int h = bmp.Height;
                if (h % 8 != 0)
                {
                    h = (h & ~7) + 8;
                }
                BitmapBits tmp = new BitmapBits(w, h);
                tmp.DrawBitmapBounded(bmp, 0, 0);
                bmp = tmp;
            }
            int tw = bmp.Width / 8;
            int th = bmp.Height / 8;

            byte[,] tilepals = new byte[tw, th];
            for (int ty = 0; ty < th; ty++)
            {
                for (int tx = 0; tx < tw; tx++)
                {
                    tilepals[tx, ty] = 0xFF;
                    int[] palcnt = new int[4];
                    for (int y = 0; y < 8; y++)
                    {
                        for (int x = 0; x < 8; x++)
                        {
                            if (bmp[tx * 8 + x, ty * 8 + y] % 16 != 0)
                            {
                                palcnt[bmp[tx * 8 + x, ty * 8 + y] / 16]++;
                            }
                        }
                    }
                    if (!palcnt.FastArrayEqual(0))
                    {
                        tilepals[tx, ty] = 0;
                        for (int i = 1; i < 4; i++)
                        {
                            if (palcnt[i] > palcnt[tilepals[tx, ty]])
                            {
                                tilepals[tx, ty] = (byte)i;
                            }
                        }
                    }
                }
            }
            int numtiles = 0;

            for (int ty = 0; ty < th; ty++)
            {
                for (int tx = 0; tx < tw; tx++)
                {
                    if (tilepals[tx, ty] != 0xFF)
                    {
                        int w = Math.Min(4, (tw) - tx);
                        for (int x = 1; x < w; x++)
                        {
                            if (tilepals[tx + x, ty] != tilepals[tx, ty])
                            {
                                w = x;
                            }
                        }
                        int h = Math.Min(4, (th) - ty);
                        for (int y = 1; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                if (tilepals[tx + x, ty + y] != tilepals[tx, ty])
                                {
                                    h = y;
                                }
                            }
                        }
                        if (twoplayer && h % 2 == 1)
                        {
                            h++;
                        }
                        if (dplcframe != null)
                        {
                            mapframe.Tiles.Add(new MappingsTile((short)((tx * 8) - center.X), (short)((ty * 8) - center.Y), (byte)w, (byte)h, (ushort)numtiles, false, false, startpal, pri));
                            dplcframe.Tiles.Add(new DPLCEntry((byte)(w * h), (ushort)tiles.Count));
                        }
                        else
                        {
                            mapframe.Tiles.Add(new MappingsTile((short)((tx * 8) - center.X), (short)((ty * 8) - center.Y), (byte)w, (byte)h, (ushort)tiles.Count, false, false, startpal, pri));
                        }
                        for (int x = 0; x < w; x++)
                        {
                            for (int y = 0; y < h; y++)
                            {
                                if (ty + y < th)
                                {
                                    tiles.Add(bmp.ToTile((tx + x) * 8, (ty + y) * 8));
                                    tilepals[tx + x, ty + y] = 0xFF;
                                }
                                else
                                {
                                    tiles.Add(new byte[32]);
                                }
                            }
                        }
                        numtiles += w * h;
                        tx       += w - 1;
                    }
                }
            }
        }