Пример #1
0
        public GoldSrcSpriteFrame(GoldSrcSprite sprite, int width, int height)
        {
            this.sprite = sprite;

            this.Data   = new byte[width * height];
            this.Width  = width;
            this.Height = height;
        }
Пример #2
0
        /// <summary>
        /// Loads a sprite from a stream
        /// </summary>
        /// <param name="stream">Sprite stream</param>
        /// <returns>Sprite</returns>
        public static GoldSrcSprite FromStream(Stream stream)
        {
            var sprite = new GoldSrcSprite();

            using (BinaryReader reader = new BinaryReader(stream))
            {
                var fileID = reader.ReadInt32();
                if (fileID != 0x50534449)
                {
                    throw new InvalidDataException("Not a valid goldsrc sprite");
                }

                var version = reader.ReadInt32();
                if (version != 2)
                {
                    throw new InvalidDataException("Only version 2 sprites are supported");
                }

                sprite.Type           = (GoldSrcSpriteType)reader.ReadInt32();
                sprite.TextureFormat  = (GoldSrcSpriteTextureFormat)reader.ReadInt32();
                sprite.BoundingRadius = reader.ReadSingle();


                var maxFrameWidth  = reader.ReadInt32();
                var maxFrameHeight = reader.ReadInt32();
                var numberOfFrames = reader.ReadInt32();
                sprite.BeamLength      = reader.ReadSingle();
                sprite.Synchronization = (GoldSrcSpriteSynchronization)reader.ReadInt32();

                // Color Palette
                var sizeOfPalette = reader.ReadInt16();
                sprite.Palette = new Color[sizeOfPalette];
                for (var i = 0; i < sizeOfPalette; i++)
                {
                    sprite.Palette[i] = Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
                }

                // Frames
                for (var i = 0; i < numberOfFrames; i++)
                {
                    var group   = (int)reader.ReadInt32();
                    var originX = reader.ReadInt32();
                    var originY = reader.ReadInt32();
                    var width   = (int)reader.ReadInt32();
                    var height  = (int)reader.ReadInt32();
                    var data    = reader.ReadBytes(width * height);

                    var frame = new GoldSrcSpriteFrame(sprite, width, height)
                    {
                        Group   = group,
                        OriginX = originX,
                        OriginY = originY
                    };

                    // Copy frame data
                    data.CopyTo(frame.Data, 0);

                    sprite.Frames.Add(frame);
                }
            }


            return(sprite);
        }