/// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="vertexBufferSize"></param>
        /// <param name="matrixStackSize"></param>
        /// <param name="colorStackSize"></param>
        public GraphicsBatch(GraphicsDevice graphicsDevice, int vertexBufferSize, int matrixStackSize)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            this.GraphicsDevice = graphicsDevice;

            this.vertexDeclaration = new D3D.VertexDeclaration(this.GraphicsDevice.InternalDevice, new[] {
                new D3D.VertexElement(0, 0, D3D.DeclarationType.Float4, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.Position, 0),
                new D3D.VertexElement(0, 16, D3D.DeclarationType.Float4, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.Color, 0),
                new D3D.VertexElement(0, 32, D3D.DeclarationType.Float2, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.TextureCoordinate, 0),
                D3D.VertexElement.VertexDeclarationEnd
            });

            this.vertices = new Vertex[vertexBufferSize * 4];
            this.vertexCount = 0;

            this.indices = new short[vertexBufferSize * 6];

            for (short i = 0, vertex = 0; i < this.indices.Length; i += 6, vertex += 4)
            {
                this.indices[i] = vertex;
                this.indices[i + 1] = (short)(vertex + 1);
                this.indices[i + 2] = (short)(vertex + 2);
                this.indices[i + 3] = vertex;
                this.indices[i + 4] = (short)(vertex + 2);
                this.indices[i + 5] = (short)(vertex + 3);
            }

            this.basicEffect = new BasicEffect(this.GraphicsDevice);

            this.pixel = D3DHelper.CreateTexture(this.GraphicsDevice.InternalDevice, 1, 1, TextureUsage.None);

            SharpDX.DataRectangle dataRectangle = this.pixel.LockRectangle(0, D3D.LockFlags.None);

            using (SharpDX.DataStream dataStream = new SharpDX.DataStream(dataRectangle.DataPointer, dataRectangle.Pitch, true, true))
            {
                dataStream.WriteRange(new Color[] { Color.White });
            }

            this.pixel.UnlockRectangle(0);

            this.pixelSource = new Rectangle(0, 0, 1, 1);

            this.matrixStack = new Matrix[matrixStackSize];
            this.matrixStackCount = 0;

            this.texture = null;
        }
Exemple #2
0
        /// <summary>
        /// Internal constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="texture"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        internal Texture(GraphicsDevice graphicsDevice, D3D.Texture texture, int width, int height)
            : base()
        {
            if (texture == null)
                throw new ArgumentNullException("texture");

            graphicsDevice.EnsureDeviceCreated();

            this.Width = width;
            this.Height = height;
            this.Usage = TextureUsage.None;

            this.CreateInternalTexture(graphicsDevice, texture);
        }
Exemple #3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        public BasicEffect(GraphicsDevice graphicsDevice)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(@"Snowball.Graphics.BasicEffect.fx");

            if (stream == null)
                throw new FileNotFoundException("Failed to load BasicEffect.fx.");

            this.effect = Effect.FromStream(graphicsDevice, stream);
        }
Exemple #4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="usage"></param>
        public Texture(GraphicsDevice graphicsDevice, int width, int height, TextureUsage usage)
            : base()
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            this.Width = width;
            this.Height = height;
            this.Usage = usage;

            this.CalculateInternalSize(graphicsDevice);

            this.InternalTexture = D3DHelper.CreateTexture(graphicsDevice.InternalDevice, this.InternalWidth, this.InternalHeight, this.Usage);
        }
Exemple #5
0
        /// <summary>
        /// Loads a Texture from a Stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="colorKey"></param>
        /// <returns></returns>
        public static Texture FromStream(GraphicsDevice graphicsDevice, Stream stream, Color? colorKey)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

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

            graphicsDevice.EnsureDeviceCreated();

            int width;
            int height;

            using (Image image = Image.FromStream(stream))
            {
                width = image.Width;
                height = image.Height;
            }

            stream.Position = 0;

            int argb = 0;
            if (colorKey != null)
                argb = colorKey.Value.ToArgb();

            D3D.Texture texture = D3DHelper.TextureFromStream(graphicsDevice.InternalDevice, stream, width, height, argb);
            return new Texture(graphicsDevice, texture, width, height);
        }
Exemple #6
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);
        }
Exemple #7
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
            };
        }