Ejemplo n.º 1
0
 internal SMXPixelDecoder(byte[] data, int paletteIndex, SMXFrameCompression compression)
 {
     _reader       = new BinaryReader(new MemoryStream(data));
     _paletteIndex = paletteIndex;
     _compression  = compression;
 }
Ejemplo n.º 2
0
        private static SMXGraphics ReadGraphics(SMXLayerHeader header, int paletteIndex, SMXFrameCompression compression, IList <SMXLayerRowEdge> rowEdges, BinaryReader reader)
        {
            var commandLength = reader.ReadUInt32();
            var pixelsLength  = reader.ReadUInt32();
            var commands      = reader.ReadBytes((int)commandLength);
            var pixels        = reader.ReadBytes((int)pixelsLength);
            var decoder       = new SMXPixelDecoder(pixels, paletteIndex, compression);

            if (IsVerbose)
            {
                Console.WriteLine("\t\tGraphics Layer");
                Console.WriteLine("\t\t{0}", new string('-', Repeat));
                Console.WriteLine("\t\tCommand Length : {0}", commandLength);
                Console.WriteLine("\t\tPixels Length  : {0}", pixelsLength);

                Console.WriteLine("\t\t\tCommand Array");
                Console.WriteLine("\t\t\t{0}", new string('-', Repeat));
            }

            var x = rowEdges[0].LeftSpacing;
            var y = 0;

            using (var brc = new BinaryReader(new MemoryStream(commands)))
                using (var brp = new BinaryReader(new MemoryStream(pixels)))
                {
                    var result = new Bitmap(header.Width, header.Height);

                    for (int i = 0; i < commands.Length; i++)
                    {
                        if (rowEdges[y].IsEmpty())
                        {
                            // Todo: Draw Transparent Line!
                            x = rowEdges[++y].LeftSpacing;
                            continue;
                        }

                        if (rowEdges[y].LeftSpacing != 0 || rowEdges[y].RightSpacing != 0)
                        {
                            // Todo: Draw Transparent 'Spacing'!
                        }

                        var command = brc.ReadByte();
                        var opCode  = (SMXCommandOpCode)(command & 0b00000011);
                        var payload = (command >> 2) + 1;

                        switch (opCode)
                        {
                        case SMXCommandOpCode.Draw:
                        {
                            if (IsVerbose)
                            {
                                Console.WriteLine("\t\t\t[{0,3}] : Draw ({1,3} -> {2,3})", command, x, x + payload);
                            }

                            for (int k = 0; k < payload; k++)
                            {
                                result.SetPixel(x, y, decoder.Next());
                                x += 1;
                            }

                            break;
                        }

                        case SMXCommandOpCode.Player:
                        {
                            if (IsVerbose)
                            {
                                Console.WriteLine("\t\t\t[{0,3}] : Player ({1,3} -> {2,3})", command, x, x + payload);
                            }

                            for (int k = 0; k < payload; k++)
                            {
                                decoder.Next();
                                result.SetPixel(x, y, Color.Blue);
                                x += 1;
                            }

                            break;
                        }

                        case SMXCommandOpCode.Skip:
                        {
                            if (IsVerbose)
                            {
                                Console.WriteLine("\t\t\t[{0,3}] : Skip ({1,3} -> {2,3})", command, x, x + payload);
                            }

                            for (int k = 0; k < payload; k++)
                            {
                                result.SetPixel(x, y, Color.Transparent);
                                x += 1;
                            }

                            break;
                        }

                        case SMXCommandOpCode.End:
                        {
                            if (IsVerbose)
                            {
                                Console.WriteLine("\t\t\t[{0,3}] : End", command);
                            }

                            if (i + 1 < commands.Length)
                            {
                                x = rowEdges[++y].LeftSpacing;
                            }

                            break;
                        }

                        default:
                        {
                            throw new InvalidDataException();
                        }
                        }
                    }

                    if (IsVerbose)
                    {
                        Console.WriteLine("\t\t\t{0}", new string('-', Repeat));
                    }

                    return(new SMXGraphics
                    {
                        CommandData = commands,
                        PixelData = pixels,
                        Image = result,
                    });
                }
        }