CreateBatchItem() public method

Create an instance of SpriteBatchItem if there is none available in the free item queue. Otherwise, a previously allocated SpriteBatchItem is reused.
public CreateBatchItem ( ) : SpriteBatchItem
return SpriteBatchItem
Example #1
0
        public void Draw(Texture2D texture, Vector2 position, Nullable <Rectangle> sourceRectangle, Color color, float rotation,
                         Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            if (texture == null)
            {
                throw new ArgumentException("texture");
            }

            SpriteBatchItem item = _batcher.CreateBatchItem();

            item.Depth     = depth;
            item.TextureID = (int)texture.ID;

            Rectangle rect;

            if (sourceRectangle.HasValue)
            {
                rect = sourceRectangle.Value;
            }
            else
            {
                rect = new Rectangle(0, 0, texture.Image.ImageWidth, texture.Image.ImageHeight);
            }

            Vector2 texCoordTL = texture.Image.GetTextureCoord(rect.X, rect.Y);
            Vector2 texCoordBR = texture.Image.GetTextureCoord(rect.X + rect.Width, rect.Y + rect.Height);

            if ((effect & SpriteEffects.FlipVertically) != 0)
            {
                float temp = texCoordBR.Y;
                texCoordBR.Y = texCoordTL.Y;
                texCoordTL.Y = temp;
            }
            if ((effect & SpriteEffects.FlipHorizontally) != 0)
            {
                float temp = texCoordBR.X;
                texCoordBR.X = texCoordTL.X;
                texCoordTL.X = temp;
            }

            item.Set
            (
                position.X,
                position.Y,
                -origin.X * scale.X,
                -origin.Y * scale.Y,
                rect.Width * scale.X,
                rect.Height * scale.Y,
                (float)Math.Sin(rotation),
                (float)Math.Cos(rotation),
                color,
                texCoordTL,
                texCoordBR
            );
        }
Example #2
0
        public void Draw(Texture2D texture,
                         Vector2 position,
                         Nullable <Rectangle> sourceRectangle,
                         Color color,
                         float rotation,
                         Vector2 origin,
                         Vector2 scale,
                         SpriteEffects effect,
                         float depth)
        {
            if (texture == null)
            {
                throw new ArgumentException("texture");
            }

            // texture 0 is the texture beeing draw
            graphicsDevice.Textures[0] = texture;

            SpriteBatchItem item = _batcher.CreateBatchItem();

            item.Depth     = depth;
            item.TextureID = (int)texture.ID;

            if (sourceRectangle.HasValue)
            {
                tempRect = sourceRectangle.Value;
            }
            else
            {
                tempRect.X      = 0;
                tempRect.Y      = 0;
                tempRect.Width  = texture.Width;
                tempRect.Height = texture.Height;
            }

            if (texture.Image == null)
            {
                float texWidthRatio  = 1.0f / (float)texture.Width;
                float texHeightRatio = 1.0f / (float)texture.Height;
                // We are initially flipped vertically so we need to flip the corners so that
                //  the image is bottom side up to display correctly
                texCoordTL.X = tempRect.X * texWidthRatio;
                //texCoordTL.Y = (tempRect.Y + tempRect.Height) * texHeightRatio;
                texCoordTL.Y = 1.0f - tempRect.Y * texHeightRatio;

                texCoordBR.X = (tempRect.X + tempRect.Width) * texWidthRatio;
                //texCoordBR.Y = tempRect.Y * texHeightRatio;
                texCoordBR.Y = 1.0f - (tempRect.Y + tempRect.Height) * texHeightRatio;
            }
            else
            {
                texCoordTL.X = texture.Image.GetTextureCoordX(tempRect.X);
                texCoordTL.Y = texture.Image.GetTextureCoordY(tempRect.Y);
                texCoordBR.X = texture.Image.GetTextureCoordX(tempRect.X + tempRect.Width);
                texCoordBR.Y = texture.Image.GetTextureCoordY(tempRect.Y + tempRect.Height);
            }

            if ((effect & SpriteEffects.FlipVertically) != 0)
            {
                float temp = texCoordBR.Y;
                texCoordBR.Y = texCoordTL.Y;
                texCoordTL.Y = temp;
            }
            if ((effect & SpriteEffects.FlipHorizontally) != 0)
            {
                float temp = texCoordBR.X;
                texCoordBR.X = texCoordTL.X;
                texCoordTL.X = temp;
            }

            item.Set(position.X,
                     position.Y,
                     -origin.X * scale.X,
                     -origin.Y * scale.Y,
                     tempRect.Width * scale.X,
                     tempRect.Height * scale.Y,
                     (float)Math.Sin(rotation),
                     (float)Math.Cos(rotation),
                     color,
                     texCoordTL,
                     texCoordBR);
        }
Example #3
0
        internal void DrawInternal(
            Texture2D texture,
            Vector4 destinationRectangle,
            Rectangle?sourceRectangle,
            Color color,
            float rotation,
            Vector2 origin,
            SpriteEffects effect,
            float depth,
            bool autoFlush
            )
        {
            SpriteBatchItem item = _batcher.CreateBatchItem();

            item.Depth   = depth;
            item.Texture = texture;

            if (sourceRectangle.HasValue)
            {
                _tempRect = sourceRectangle.Value;
            }
            else
            {
                _tempRect.X      = 0;
                _tempRect.Y      = 0;
                _tempRect.Width  = texture.Width;
                _tempRect.Height = texture.Height;
            }

            _texCoordTL.X = _tempRect.X / (float)texture.Width;
            _texCoordTL.Y = _tempRect.Y / (float)texture.Height;
            _texCoordBR.X = (_tempRect.X + _tempRect.Width) / (float)texture.Width;
            _texCoordBR.Y = (_tempRect.Y + _tempRect.Height) / (float)texture.Height;

            if ((effect & SpriteEffects.FlipVertically) != 0)
            {
                float temp = _texCoordBR.Y;
                _texCoordBR.Y = _texCoordTL.Y;
                _texCoordTL.Y = temp;
            }
            if ((effect & SpriteEffects.FlipHorizontally) != 0)
            {
                float temp = _texCoordBR.X;
                _texCoordBR.X = _texCoordTL.X;
                _texCoordTL.X = temp;
            }

            item.Set(
                destinationRectangle.X,
                destinationRectangle.Y,
                -origin.X,
                -origin.Y,
                destinationRectangle.Z,
                destinationRectangle.W,
                (float)Math.Sin(rotation),
                (float)Math.Cos(rotation),
                color,
                _texCoordTL,
                _texCoordBR
                );

            if (autoFlush)
            {
                FlushIfNeeded();
            }
        }
Example #4
0
        internal void DrawInternal(Texture2D texture,
                                   Vector4 destinationRectangle,
                                   Rectangle?sourceRectangle,
                                   Color color,
                                   float rotation,
                                   Vector2 origin,
                                   SpriteEffects effect,
                                   float depth,
                                   bool autoFlush)
        {
            var item = _batcher.CreateBatchItem();

            item.Depth   = depth;
            item.Texture = texture;
            int   iTexWidth  = texture.width;
            int   iTexHeight = texture.height;
            float fTexWidth  = iTexWidth;
            float fTexHeight = iTexHeight;

            if (sourceRectangle.HasValue)
            {
                _tempRect = sourceRectangle.Value;
            }
            else
            {
                _tempRect.X      = 0;
                _tempRect.Y      = 0;
                _tempRect.Width  = iTexWidth;
                _tempRect.Height = iTexHeight;
            }

            _texCoordTL.X = _tempRect.X / fTexWidth;
            _texCoordTL.Y = _tempRect.Y / fTexHeight;
            _texCoordBR.X = (_tempRect.X + _tempRect.Width) / fTexWidth;
            _texCoordBR.Y = (_tempRect.Y + _tempRect.Height) / fTexHeight;

            if ((effect & SpriteEffects.FlipVertically) != 0)
            {
                var temp2 = _texCoordBR.Y;
                _texCoordBR.Y = _texCoordTL.Y;
                _texCoordTL.Y = temp2;
            }
            if ((effect & SpriteEffects.FlipHorizontally) != 0)
            {
                var temp = _texCoordBR.X;
                _texCoordBR.X = _texCoordTL.X;
                _texCoordTL.X = temp;
            }

            item.Set(destinationRectangle.X,
                     destinationRectangle.Y,
                     -origin.X,
                     -origin.Y,
                     destinationRectangle.Z,
                     destinationRectangle.W,
                     (float)Math.Sin(rotation),
                     (float)Math.Cos(rotation),
                     color,
                     _texCoordTL,
                     _texCoordBR);

            /*logCount++;
             * if(logCount > 300)
             * {
             *  logCount = 0;
             *  Console.WriteLine("Rotation: " + rotation);
             *  Console.WriteLine("item set input: destinationRectangle: " + destinationRectangle.ToString() + " origin: " + origin.ToString() +
             *      " sin: " + (float)Math.Sin(rotation) + " cos: " + (float)Math.Cos(rotation) + " _texCoordTL: " + _texCoordTL.ToString() + " _texCoordBR: " + _texCoordBR);
             * }*/

            if (autoFlush)
            {
                FlushIfNeeded();
            }
        }
Example #5
0
        internal void DrawInternal(Texture2D texture,
                                   Vector4 destinationRectangle,
                                   Rectangle?sourceRectangle,
                                   Color color,
                                   float rotation,
                                   Vector2 origin,
                                   SpriteEffects effect,
                                   float depth,
                                   bool autoFlush)
        {
            var item = _batcher.CreateBatchItem();

            item.Texture = texture;

            // set SortKey based on SpriteSortMode.
            switch (_sortMode)
            {
            // Comparison of Texture objects.
            case SpriteSortMode.Texture:
                item.SortKey = texture.SortingKey;
                break;

            // Comparison of Depth
            case SpriteSortMode.FrontToBack:
                item.SortKey = depth;
                break;

            // Comparison of Depth in reverse
            case SpriteSortMode.BackToFront:
                item.SortKey = -depth;
                break;
            }

            if (sourceRectangle.HasValue)
            {
                _tempRect     = sourceRectangle.Value;
                _texCoordTL.X = _tempRect.X / (float)texture.Width;
                _texCoordTL.Y = _tempRect.Y / (float)texture.Height;
                _texCoordBR.X = (_tempRect.X + _tempRect.Width) / (float)texture.Width;
                _texCoordBR.Y = (_tempRect.Y + _tempRect.Height) / (float)texture.Height;
            }
            else
            {
                _texCoordTL.X = 0f;
                _texCoordTL.Y = 0f;
                _texCoordBR.X = 1f;
                _texCoordBR.Y = 1f;
            }

            if ((effect & SpriteEffects.FlipVertically) != 0)
            {
                var temp = _texCoordBR.Y;
                _texCoordBR.Y = _texCoordTL.Y;
                _texCoordTL.Y = temp;
            }
            if ((effect & SpriteEffects.FlipHorizontally) != 0)
            {
                var temp = _texCoordBR.X;
                _texCoordBR.X = _texCoordTL.X;
                _texCoordTL.X = temp;
            }

            if (rotation == 0f)
            {
                item.Set(destinationRectangle.X - origin.X,
                         destinationRectangle.Y - origin.Y,
                         destinationRectangle.Z,
                         destinationRectangle.W,
                         color,
                         _texCoordTL,
                         _texCoordBR,
                         depth);
            }
            else
            {
                item.Set(destinationRectangle.X,
                         destinationRectangle.Y,
                         -origin.X,
                         -origin.Y,
                         destinationRectangle.Z,
                         destinationRectangle.W,
                         (float)Math.Sin(rotation),
                         (float)Math.Cos(rotation),
                         color,
                         _texCoordTL,
                         _texCoordBR,
                         depth);
            }

            if (autoFlush)
            {
                FlushIfNeeded();
            }
        }
Example #6
0
        /// <summary>
        /// Submit a sprite for drawing in the current batch.
        /// </summary>
        /// <param name="texture">A texture.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
        /// <param name="color">A color mask.</param>
        /// <param name="rotation">A rotation of this sprite.</param>
        /// <param name="origin">Center of the rotation. 0,0 by default.</param>
        /// <param name="scale">A scaling of this sprite.</param>
        /// <param name="effects">Modificators for drawing. Can be combined.</param>
        /// <param name="layerDepth">A depth of the layer of this sprite.</param>
        public void Draw(Texture2D texture,
                         Vector2 position,
                         Rectangle?sourceRectangle,
                         Color color,
                         float rotation,
                         Vector2 origin,
                         Vector2 scale,
                         SpriteEffects effects,
                         float layerDepth)
        {
            CheckValid(texture);

            var item = _batcher.CreateBatchItem();

            item.Texture = texture;

            // set SortKey based on SpriteSortMode.
            switch (_sortMode)
            {
            // Comparison of Texture objects.
            case SpriteSortMode.Texture:
                item.SortKey = texture.SortingKey;
                break;

            // Comparison of Depth
            case SpriteSortMode.FrontToBack:
                item.SortKey = layerDepth;
                break;

            // Comparison of Depth in reverse
            case SpriteSortMode.BackToFront:
                item.SortKey = -layerDepth;
                break;
            }

            origin = origin * scale;

            float w, h;

            if (sourceRectangle.HasValue)
            {
                var srcRect = sourceRectangle.GetValueOrDefault();
                w             = srcRect.Width * scale.X;
                h             = srcRect.Height * scale.Y;
                _texCoordTL.X = srcRect.X * texture.TexelWidth;
                _texCoordTL.Y = srcRect.Y * texture.TexelHeight;
                _texCoordBR.X = (srcRect.X + srcRect.Width) * texture.TexelWidth;
                _texCoordBR.Y = (srcRect.Y + srcRect.Height) * texture.TexelHeight;
            }
            else
            {
                w           = texture.Width * scale.X;
                h           = texture.Height * scale.Y;
                _texCoordTL = Vector2.Zero;
                _texCoordBR = Vector2.One;
            }

            if ((effects & SpriteEffects.FlipVertically) != 0)
            {
                var temp = _texCoordBR.Y;
                _texCoordBR.Y = _texCoordTL.Y;
                _texCoordTL.Y = temp;
            }
            if ((effects & SpriteEffects.FlipHorizontally) != 0)
            {
                var temp = _texCoordBR.X;
                _texCoordBR.X = _texCoordTL.X;
                _texCoordTL.X = temp;
            }

            if (rotation == 0f)
            {
                item.Set(position.X - origin.X,
                         position.Y - origin.Y,
                         w,
                         h,
                         color,
                         _texCoordTL,
                         _texCoordBR,
                         layerDepth);
            }
            else
            {
                item.Set(position.X,
                         position.Y,
                         -origin.X,
                         -origin.Y,
                         w,
                         h,
                         (float)Math.Sin(rotation),
                         (float)Math.Cos(rotation),
                         color,
                         _texCoordTL,
                         _texCoordBR,
                         layerDepth);
            }

            FlushIfNeeded();
        }