Ejemplo n.º 1
0
        /// <summary>
        /// Function to load a version 1.x Gorgon sprite.
        /// </summary>
        /// <param name="sprite">Sprite to fill in with data.</param>
        /// <param name="reader">Binary reader to use to read in the data.</param>
        public static void LoadSprite(GorgonSprite sprite, GorgonBinaryReader reader)
        {
            Version version;
            string  imageName = string.Empty;

            sprite.IsV1Sprite = true;

            reader.BaseStream.Position = 0;

            string headerVersion = reader.ReadString();

            if ((!headerVersion.StartsWith("GORSPR", StringComparison.OrdinalIgnoreCase)) || (headerVersion.Length < 7))
            {
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2D_SPRITE_CANNOT_READ_V1_SPRITE);
            }

            // Get the version information.
            switch (headerVersion.ToUpperInvariant())
            {
            case "GORSPR1":
                version = new Version(1, 0);
                break;

            case "GORSPR1.1":
                version = new Version(1, 1);
                break;

            case "GORSPR1.2":
                version = new Version(1, 2);
                break;

            default:
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2D_SPRITE_CANNOT_READ_V1_SPRITE);
            }

            // We don't need the sprite name.
            reader.ReadString();

            // Find out if we have an image.
            if (reader.ReadBoolean())
            {
                bool isRenderTarget = reader.ReadBoolean();

                imageName = reader.ReadString();

                // We won't be supporting reading render targets from sprites in this version.
                if (isRenderTarget)
                {
                    // Skip the target data.
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadBoolean();
                    reader.ReadBoolean();
                }
            }

            // We don't use "inherited" values anymore.  But we need them because
            // the file doesn't include inherited data.
            bool InheritAlphaMaskFunction     = reader.ReadBoolean();
            bool InheritAlphaMaskValue        = reader.ReadBoolean();
            bool InheritBlending              = reader.ReadBoolean();
            bool InheritHorizontalWrapping    = reader.ReadBoolean();
            bool InheritSmoothing             = reader.ReadBoolean();
            bool InheritStencilCompare        = reader.ReadBoolean();
            bool InheritStencilEnabled        = reader.ReadBoolean();
            bool InheritStencilFailOperation  = reader.ReadBoolean();
            bool InheritStencilMask           = reader.ReadBoolean();
            bool InheritStencilPassOperation  = reader.ReadBoolean();
            bool InheritStencilReference      = reader.ReadBoolean();
            bool InheritStencilZFailOperation = reader.ReadBoolean();
            bool InheritVerticalWrapping      = reader.ReadBoolean();
            bool InheritDepthBias             = true;
            bool InheritDepthTestFunction     = true;
            bool InheritDepthWriteEnabled     = true;

            // Get version 1.1 fields.
            if ((version.Major == 1) && (version.Minor >= 1))
            {
                InheritDepthBias         = reader.ReadBoolean();
                InheritDepthTestFunction = reader.ReadBoolean();
                InheritDepthWriteEnabled = reader.ReadBoolean();
            }

            // Get the size of the sprite.
            sprite.Size = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            // Older versions of the sprite object used pixel space for their texture coordinates.  We will have to
            // fix up these coordinates into texture space once we have a texture loaded.  At this point, there's no guarantee
            // that the texture was loaded safely, so we'll have to defer it until later.
            // Also, older versions used the size the determine the area on the texture to cover.  So use the size to
            // get the texture bounds.
            var textureOffset = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            sprite.TextureOffset = textureOffset;
            sprite.TextureSize   = sprite.Size;

            // Read the anchor.
            sprite.Anchor = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            // Get vertex offsets.
            sprite.SetCornerOffset(RectangleCorner.UpperLeft, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.UpperRight, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.LowerRight, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.LowerLeft, new Vector2(reader.ReadSingle(), reader.ReadSingle()));

            // Get vertex colors.
            sprite.SetCornerColor(RectangleCorner.UpperLeft, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.UpperRight, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.LowerLeft, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.LowerRight, new GorgonColor(reader.ReadInt32()));

            // Skip shader information.  Version 1.0 had shader information attached to the sprite.
            if ((version.Major == 1) && (version.Minor < 1))
            {
                if (reader.ReadBoolean())
                {
                    reader.ReadString();
                    reader.ReadBoolean();
                    if (reader.ReadBoolean())
                    {
                        reader.ReadString();
                    }
                }
            }

            // We no longer have an alpha mask function.
            if (!InheritAlphaMaskFunction)
            {
                reader.ReadInt32();
            }

            if (!InheritAlphaMaskValue)
            {
                // Direct 3D 9 used a value from 0..255 for alpha masking, we use
                // a scalar value so convert to a scalar.
                sprite.AlphaTestValues = new GorgonRangeF(0.0f, reader.ReadInt32() / 255.0f);
            }

            // Set the blending mode.
            if (!InheritBlending)
            {
                sprite.Blending.SourceBlend      = ConvertBlendOpToBlendType(reader.ReadInt32());
                sprite.Blending.DestinationBlend = ConvertBlendOpToBlendType(reader.ReadInt32());
                // Skip the blending mode, this gets detected automatically now.
                reader.ReadInt32();
            }

            // Get alpha blending mode.
            if ((version.Major == 1) && (version.Minor >= 2))
            {
                sprite.Blending.DestinationAlphaBlend = ConvertBlendOpToBlendType(reader.ReadInt32());
                sprite.Blending.SourceAlphaBlend      = ConvertBlendOpToBlendType(reader.ReadInt32());
            }

            // Get horizontal wrapping mode.
            if (!InheritHorizontalWrapping)
            {
                sprite.TextureSampler.HorizontalWrapping = ConvertImageAddressToTextureAddress(reader.ReadInt32());
            }

            // Get smoothing mode.
            if (!InheritSmoothing)
            {
                sprite.TextureSampler.TextureFilter = ConvertSmoothingToFilter(reader.ReadInt32());
            }

            // Get stencil stuff.
            if (!InheritStencilCompare)
            {
                sprite.DepthStencil.FrontFace.ComparisonOperator = ConvertCompare(reader.ReadInt32());
            }
            if (!InheritStencilEnabled)
            {
                // We don't enable stencil in the same way anymore, so skip this value.
                reader.ReadBoolean();
            }
            if (!InheritStencilFailOperation)
            {
                sprite.DepthStencil.FrontFace.FailOperation = ConvertStencilOp(reader.ReadInt32());
            }
            if (!InheritStencilMask)
            {
                sprite.DepthStencil.StencilReadMask = (byte)(reader.ReadInt32() & 0xFF);
            }
            if (!InheritStencilPassOperation)
            {
                sprite.DepthStencil.FrontFace.PassOperation = ConvertStencilOp(reader.ReadInt32());
            }
            if (!InheritStencilReference)
            {
                sprite.DepthStencil.StencilReference = reader.ReadInt32();
            }
            if (!InheritStencilZFailOperation)
            {
                sprite.DepthStencil.FrontFace.DepthFailOperation = ConvertStencilOp(reader.ReadInt32());
            }

            // Get vertical wrapping mode.
            if (!InheritVerticalWrapping)
            {
                sprite.TextureSampler.VerticalWrapping = ConvertImageAddressToTextureAddress(reader.ReadInt32());
            }

            // Get depth info.
            if ((version.Major == 1) && (version.Minor >= 1))
            {
                if (!InheritDepthBias)
                {
                    // Depth bias values are quite different on D3D9 than they are on D3D11, so skip this.
                    reader.ReadSingle();
                }
                if (!InheritDepthTestFunction)
                {
                    sprite.DepthStencil.DepthComparison = ConvertCompare(reader.ReadInt32());
                }
                if (!InheritDepthWriteEnabled)
                {
                    sprite.DepthStencil.IsDepthWriteEnabled = reader.ReadBoolean();
                }

                sprite.TextureSampler.BorderColor = new GorgonColor(reader.ReadInt32());
            }

            // Get flipped flags.
            sprite.HorizontalFlip = reader.ReadBoolean();
            sprite.VerticalFlip   = reader.ReadBoolean();

            // Bind the texture (if we have one bound to this sprite) if it's already loaded, otherwise defer it.
            if (string.IsNullOrEmpty(imageName))
            {
                return;
            }

            sprite.DeferredTextureName = imageName;
        }