public object Load(Stream stream, string assetName)
        {
            Palette pal = new Palette();

            if (!pal.Load(stream))
                return null;

            stream.Close();

            return pal;
        }
Beispiel #2
0
        public void SetPalette(Palette palette)
        {
            _palette = palette;

            RecreatePalImages();
        }
Beispiel #3
0
        public bool Load(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);
            byte[] magic = reader.ReadBytes(2);

            if (magic[0] != (byte)'S' || magic[1] != (byte)'P')
            {
                return false;
            }

            _version = reader.ReadInt16();

            if (_version != 0x100 && _version != 0x101 &&
                _version != 0x200 && _version != 0x201)
            {
                return false;
            }

            _palCount = reader.ReadInt16();
            if (_version >= 0x200)
                _rgbaCount = reader.ReadInt16();

            _palData = new byte[_palCount][];
            _images = new Texture2D[_palCount + _rgbaCount];
            if (_palCount > 0)
            {
                for (int p = 0; p < _palCount; p++)
                {
                    ushort w = reader.ReadUInt16();
                    ushort h = reader.ReadUInt16();
                    int pixelCount = w * h;

                    if (pixelCount > 0)
                    {
                        byte[] data;

                        if (_version >= 0x201)
                        {
                            data = new byte[pixelCount];

                            uint next = 0;
                            ushort encoded;

                            encoded = reader.ReadUInt16();
                            while (next < pixelCount && encoded > 0)
                            {
                                byte c = reader.ReadByte();

                                encoded--;

                                if (c == 0)
                                {
                                    byte len = reader.ReadByte();

                                    encoded--;

                                    if (len == 0)
                                        len = 1;

                                    if (next + len > pixelCount)
                                        return false;

                                    for (int i = 0; i < len; i++)
                                        data[next] = 0;

                                    next += len;
                                }
                                else
                                {
                                    data[next++] = c;
                                }
                            }

                            if (next != pixelCount || encoded > 0)
                                return false;
                        }
                        else
                        {
                            data = reader.ReadBytes(pixelCount);
                        }

                        _palData[p] = data;
                        _images[p] = new Texture2D(_graphicsDevice, w, h, false, SurfaceFormat.Color);
                    }
                }
            }

            if (_rgbaCount > 0)
            {
                for (int p = 0; p < _rgbaCount; p++)
                {
                    ushort w = reader.ReadUInt16();
                    ushort h = reader.ReadUInt16();
                    byte[] texData = reader.ReadBytes(w * h * 4);

                    Texture2D tex = new Texture2D(_graphicsDevice, w, h, false, SurfaceFormat.Color);
                    tex.SetData(texData);

                    _images[_palCount + p] = tex;
                }
            }

            if (_version >= 0x101)
            {
                Palette pal = new Palette();
                pal.Load(stream);
                SetPalette(pal);
            }

            return true;
        }