示例#1
0
        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static SpriteSheet FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            List<Rectangle> rectangles = new List<Rectangle>();
            string textureFile = null;
            Color colorKey = Color.Transparent;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "SpriteSheet")
                        throw new XmlException("Invalid SpriteSheet xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");
                    colorKey = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "00000000"));

                    xml.Read();
                    while (xml.Name == "Frame")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the SpriteSheet xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, colorKey);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new SpriteSheet(texture, rectangles);
        }
示例#2
0
        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static TextureFont FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            Dictionary<char, Rectangle> rectangles = new Dictionary<char, Rectangle>();
            string textureFile = null;
            Color backgroundColor = Color.Transparent;
            string fontName = TextureFont.DefaultFontName;
            int fontSize = TextureFont.DefaultFontSize;
            int characterSpacing;
            int lineSpacing;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "TextureFont")
                        throw new XmlException("Invalid TextureFont xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");

                    backgroundColor = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "FFFFFFFF"));
                    fontName = xml.ReadAttributeValueOrDefault("FontName", DefaultFontName);
                    fontSize = xml.ReadAttributeValueOrDefault<int>("FontSize", DefaultFontSize);
                    characterSpacing = xml.ReadAttributeValueOrDefault<int>("CharacterSpacing", DefaultCharacterSpacing);
                    lineSpacing = xml.ReadAttributeValueOrDefault<int>("LineSpacing", DefaultLineSpacing);

                    xml.Read();
                    while (xml.Name == "Character")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(xml.ReadRequiredAttributeValue("Value")[0], rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the TextureFont xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, backgroundColor);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new TextureFont(texture, rectangles, fontName, fontSize)
            {
                CharacterSpacing = characterSpacing,
                LineSpacing = lineSpacing
            };
        }