/// <summary> /// Function to read the sprite data from a stream. /// </summary> /// <param name="stream">The stream containing the sprite.</param> /// <param name="byteCount">The number of bytes to read from the stream.</param> /// <param name="overrideTexture">The texture to assign to the sprite instead of the texture associated with the name stored in the file.</param> /// <returns>A new <see cref="GorgonSprite"/>.</returns> protected override GorgonSprite OnReadFromStream(Stream stream, int byteCount, GorgonTexture2DView overrideTexture) { var reader = new GorgonChunkReader(stream); try { // We don't need byte count for this. return(LoadSprite(Graphics, reader, overrideTexture)); } finally { reader.Dispose(); } }
/// <summary> /// Function to read the renderable data from a stream. /// </summary> /// <param name="stream">Open file stream containing the renderable data.</param> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="stream" /> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.IO.IOException">Thrown when the stream parameter is not opened for reading data.</exception> /// <exception cref="GorgonLibrary.GorgonException">Thrown when the data in the stream does not contain valid renderable data, or contains a newer version of the renderable than Gorgon can handle.</exception> void IPersistedRenderable.Load(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new IOException(Resources.GOR2D_STREAM_WRITE_ONLY); } GorgonChunkReader chunk = null; // Read the sprite in. try { chunk = new GorgonChunkReader(stream); if (!chunk.HasChunk(FileHeader)) { // The old format doesn't use the chunker. chunk.Dispose(); chunk = null; // Check to see if it's an older version of the Gorgon sprite data. using (var oldReader = new GorgonBinaryReader(stream, true)) { GorgonV1SpriteReader.LoadSprite(this, oldReader); return; } } // Read in the file header. chunk.Begin(FileHeader); chunk.Begin("SPRTDATA"); Anchor = chunk.Read <Vector2>(); Size = chunk.Read <Vector2>(); HorizontalFlip = chunk.ReadBoolean(); VerticalFlip = chunk.ReadBoolean(); // Read vertex colors. for (int i = 0; i < Vertices.Length; i++) { Vertices[i].Color = chunk.Read <GorgonColor>(); } // Write vertex offsets. chunk.ReadRange(_offsets); chunk.End(); // Read rendering information. chunk.Begin("RNDRDATA"); CullingMode = chunk.Read <CullingMode>(); AlphaTestValues = chunk.Read <GorgonRangeF>(); Blending.AlphaOperation = chunk.Read <BlendOperation>(); Blending.BlendOperation = chunk.Read <BlendOperation>(); Blending.BlendFactor = chunk.Read <GorgonColor>(); Blending.DestinationAlphaBlend = chunk.Read <BlendType>(); Blending.DestinationBlend = chunk.Read <BlendType>(); Blending.SourceAlphaBlend = chunk.Read <BlendType>(); Blending.SourceBlend = chunk.Read <BlendType>(); Blending.WriteMask = chunk.Read <ColorWriteMaskFlags>(); DepthStencil.BackFace.ComparisonOperator = chunk.Read <ComparisonOperator>(); DepthStencil.BackFace.DepthFailOperation = chunk.Read <StencilOperation>(); DepthStencil.BackFace.FailOperation = chunk.Read <StencilOperation>(); DepthStencil.BackFace.PassOperation = chunk.Read <StencilOperation>(); DepthStencil.FrontFace.ComparisonOperator = chunk.Read <ComparisonOperator>(); DepthStencil.FrontFace.DepthFailOperation = chunk.Read <StencilOperation>(); DepthStencil.FrontFace.FailOperation = chunk.Read <StencilOperation>(); DepthStencil.FrontFace.PassOperation = chunk.Read <StencilOperation>(); DepthStencil.DepthBias = chunk.ReadInt32(); DepthStencil.DepthComparison = chunk.Read <ComparisonOperator>(); DepthStencil.StencilReference = chunk.ReadInt32(); DepthStencil.IsDepthWriteEnabled = chunk.ReadBoolean(); DepthStencil.StencilReadMask = chunk.ReadByte(); DepthStencil.StencilReadMask = chunk.ReadByte(); chunk.End(); // Read collider information. if (chunk.HasChunk("COLLIDER")) { chunk.Begin("COLLIDER"); Type colliderType = Type.GetType(chunk.ReadString()); Debug.Assert(colliderType != null, "Collider is NULL!!"); var collider = (Gorgon2DCollider)Activator.CreateInstance(colliderType); collider.ReadFromChunk(chunk); chunk.End(); } // Read texture information. chunk.Begin("TXTRDATA"); TextureSampler.BorderColor = chunk.Read <GorgonColor>(); TextureSampler.HorizontalWrapping = chunk.Read <TextureAddressing>(); TextureSampler.VerticalWrapping = chunk.Read <TextureAddressing>(); TextureSampler.TextureFilter = chunk.Read <TextureFilter>(); DeferredTextureName = chunk.ReadString(); TextureRegion = chunk.ReadRectangleF(); } finally { if (chunk != null) { chunk.Dispose(); } } }