Пример #1
0
 /// <summary>
 /// Function to save the sprite data to a stream.
 /// </summary>
 /// <param name="sprite">The sprite to serialize into the stream.</param>
 /// <param name="stream">The stream that will contain the sprite.</param>
 protected override void OnSaveToStream(GorgonPolySprite sprite, Stream stream)
 {
     using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
     {
         writer.Write(sprite.ToJson());
     }
 }
Пример #2
0
        /// <summary>
        /// Function to convert a JSON string into a sprite object.
        /// </summary>
        /// <param name="renderer">The renderer for the sprite.</param>
        /// <param name="overrideTexture">The texture to assign to the sprite instead of the texture associated with the name stored in the file.</param>
        /// <param name="json">The JSON string containing the sprite data.</param>
        /// <returns>A new <see cref="GorgonPolySprite"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="renderer"/>, or the <paramref name="json"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="json"/> parameter is empty.</exception>
        /// <exception cref="GorgonException">Thrown if the JSON string does not contain sprite data, or there is a version mismatch.</exception>
        public static GorgonPolySprite FromJson(Gorgon2D renderer, GorgonTexture2DView overrideTexture, string json)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }

            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            if (string.IsNullOrWhiteSpace(json))
            {
                throw new ArgumentEmptyException(nameof(json));
            }

            // Set up serialization so we can convert our more complicated structures.
            var serializer = new JsonSerializer
            {
                CheckAdditionalContent = false
            };

            serializer.Converters.Add(new JsonVector2Converter());
            serializer.Converters.Add(new JsonVector3Converter());
            serializer.Converters.Add(new JsonSize2FConverter());
            serializer.Converters.Add(new JsonGorgonColorConverter());
            serializer.Converters.Add(new JsonRectangleFConverter());
            serializer.Converters.Add(new JsonSamplerConverter(renderer.Graphics));
            serializer.Converters.Add(new JsonTexture2DConverter(renderer.Graphics, overrideTexture));
            serializer.Converters.Add(new VersionConverter());

            // Parse the string so we can extract our header/version for comparison.
            var     jobj        = JObject.Parse(json);
            ulong   jsonID      = jobj[GorgonSpriteExtensions.JsonHeaderProp].Value <ulong>();
            Version jsonVersion = jobj[GorgonSpriteExtensions.JsonVersionProp].ToObject <Version>(serializer);

            if (jsonID != CurrentFileHeader)
            {
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2DIO_ERR_JSON_NOT_SPRITE);
            }

            if (!jsonVersion.Equals(CurrentVersion))
            {
                throw new GorgonException(GorgonResult.CannotRead, string.Format(Resources.GOR2DIO_ERR_SPRITE_VERSION_MISMATCH, CurrentVersion, jsonVersion));
            }

            GorgonPolySprite workingSpriteData = jobj.ToObject <GorgonPolySprite>(serializer);

            // We have to rebuild the sprite because it's only data at this point and we need to build up its vertex/index buffers before we can render it.
            var builder = new GorgonPolySpriteBuilder(renderer);

            builder.ResetTo(workingSpriteData);

            return(builder.Build());
        }
Пример #3
0
        /// <summary>
        /// Function to create the sprites to draw.
        /// </summary>
        private static void CreateSprites()
        {
            // Create the regular sprite first.
            _normalSprite = new GorgonSprite
            {
                Anchor        = new DX.Vector2(0.5f, 0.5f),
                Size          = new DX.Size2F(_texture.Width, _texture.Height),
                Texture       = _texture,
                TextureRegion = new DX.RectangleF(0, 0, 1, 1)
            };

            _polySprite = PolygonHullParser.ParsePolygonHullString(_renderer, Resources.PolygonHull);
        }
        /// <summary>
        /// Function to save the sprite data to a file on a physical file system.
        /// </summary>
        /// <param name="sprite">The sprite to serialize into the file.</param>
        /// <param name="filePath">The path to the file to write.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="filePath" /> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="filePath" /> parameter is empty.</exception>
        /// <exception cref="NotSupportedException">This method is not supported by this codec.</exception>
        public void Save(GorgonPolySprite sprite, string filePath)
        {
            if (!CanEncode)
            {
                throw new NotSupportedException();
            }

            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentEmptyException(nameof(filePath));
            }

            using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                Save(sprite, stream);
            }
        }
        /// <summary>
        /// Function to save the sprite data to a stream.
        /// </summary>
        /// <param name="sprite">The sprite to serialize into the stream.</param>
        /// <param name="stream">The stream that will contain the sprite.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="sprite"/>, or the <paramref name="stream"/> parameter is <b>null</b>.</exception>
        /// <exception cref="GorgonException">Thrown if the stream is read only.</exception>
        /// <exception cref="NotSupportedException">This method is not supported by this codec.</exception>
        public void Save(GorgonPolySprite sprite, Stream stream)
        {
            if (!CanEncode)
            {
                throw new NotSupportedException();
            }

            if (sprite == null)
            {
                throw new ArgumentNullException(nameof(sprite));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanWrite)
            {
                throw new GorgonException(GorgonResult.CannotWrite, Resources.GOR2DIO_ERR_STREAM_IS_READ_ONLY);
            }

            OnSaveToStream(sprite, stream);
        }
        /// <summary>
        /// Function to save the sprite data to a stream.
        /// </summary>
        /// <param name="sprite">The sprite to serialize into the stream.</param>
        /// <param name="stream">The stream that will contain the sprite.</param>
        protected override void OnSaveToStream(GorgonPolySprite sprite, Stream stream)
        {
            var writer = new GorgonChunkFileWriter(stream, CurrentFileHeader);
            GorgonBinaryWriter binWriter = null;

            try
            {
                writer.Open();
                binWriter = writer.OpenChunk(VersionData);
                binWriter.Write((byte)Version.Major);
                binWriter.Write((byte)Version.Minor);
                writer.CloseChunk();

                binWriter = writer.OpenChunk(SpriteData);

                binWriter.WriteValue(sprite.Anchor);

                // If we do not have alpha test information, then skip writing its data.
                binWriter.Write(sprite.AlphaTest != null);
                if (sprite.AlphaTest != null)
                {
                    binWriter.WriteValue(sprite.AlphaTest.Value);
                }

                writer.CloseChunk();

                binWriter = writer.OpenChunk(VertexData);

                binWriter.Write(sprite.Vertices.Count);

                foreach (GorgonPolySpriteVertex vertex in sprite.Vertices)
                {
                    binWriter.WriteValue(vertex.Position);
                    binWriter.WriteValue(vertex.Color);
                    binWriter.WriteValue(vertex.TextureCoordinate);
                }

                writer.CloseChunk();

                // We have no texture data, so don't bother writing out that chunk.
                if (sprite.Texture != null)
                {
                    binWriter = writer.OpenChunk(TextureData);
                    binWriter.Write(sprite.Texture.Texture.Name);
                    binWriter.WriteValue(sprite.TextureOffset);
                    binWriter.WriteValue(sprite.TextureScale);
                    binWriter.WriteValue(sprite.TextureArrayIndex);
                    // Write out as much info about the texture as we can so we can look it up based on these values when loading.
                    binWriter.Write(sprite.Texture.Texture.Width);
                    binWriter.Write(sprite.Texture.Texture.Height);
                    binWriter.WriteValue(sprite.Texture.Texture.Format);
                    binWriter.Write(sprite.Texture.Texture.ArrayCount);
                    binWriter.Write(sprite.Texture.Texture.MipLevels);
                    binWriter.Write(sprite.Texture.ArrayIndex);
                    binWriter.Write(sprite.Texture.ArrayCount);
                    binWriter.Write(sprite.Texture.MipSlice);
                    binWriter.Write(sprite.Texture.MipCount);
                    binWriter.WriteValue(sprite.Texture.Format);
                    binWriter.Close();
                }

                if (sprite.TextureSampler == null)
                {
                    return;
                }

                // Write out information about the texture sampler used by the sprite.
                binWriter = writer.OpenChunk(TextureSamplerData);
                binWriter.WriteValue(sprite.TextureSampler.Filter);
                binWriter.WriteValue(sprite.TextureSampler.BorderColor);
                binWriter.WriteValue(sprite.TextureSampler.ComparisonFunction);
                binWriter.WriteValue(sprite.TextureSampler.WrapU);
                binWriter.WriteValue(sprite.TextureSampler.WrapV);
                binWriter.WriteValue(sprite.TextureSampler.WrapW);
                binWriter.Write(sprite.TextureSampler.MaxAnisotropy);
                binWriter.Write(sprite.TextureSampler.MinimumLevelOfDetail);
                binWriter.Write(sprite.TextureSampler.MaximumLevelOfDetail);
                binWriter.Write(sprite.TextureSampler.MipLevelOfDetailBias);
                writer.CloseChunk();
            }
            finally
            {
                binWriter?.Dispose();
                writer.Close();
            }
        }
 /// <summary>
 /// Function to save the sprite data to a stream.
 /// </summary>
 /// <param name="sprite">The sprite to serialize into the stream.</param>
 /// <param name="stream">The stream that will contain the sprite.</param>
 protected abstract void OnSaveToStream(GorgonPolySprite sprite, Stream stream);