Microsoft.Xna.Framework.Graphics.SpriteBatch.DrawInternal C# (CSharp) Method

DrawInternal() private method

private DrawInternal ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector4 destinationRectangle, Rectangle sourceRectangle, System.Color color, float rotation, System.Vector2 origin, SpriteEffects effect, float depth, bool autoFlush ) : void
texture Microsoft.Xna.Framework.Graphics.Texture2D
destinationRectangle System.Vector4
sourceRectangle Rectangle
color System.Color
rotation float
origin System.Vector2
effect SpriteEffects
depth float
autoFlush bool
return void
		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;

			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) {
                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;
			}

			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();
			}
		}

Usage Example

Example #1
0
        internal void DrawInto( SpriteBatch spriteBatch, ref CharacterSource text, Vector2 position, Color color,
			                    float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
		{
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                MeasureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X *= -1;
                    scale.X *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y *= -1;
                    scale.Y *= -1;
                    flipAdjustment.Y = LineSpacing - size.Y;
                }
            }

            // TODO: This looks excessive... i suspect we could do most
            // of this with simple vector math and avoid this much matrix work.

            Matrix transformation, temp;
            Matrix.CreateTranslation(-origin.X, -origin.Y, 0f, out transformation);
            Matrix.CreateScale(scale.X, scale.Y, 1f, out temp);
            Matrix.Multiply(ref transformation, ref temp, out transformation);
            Matrix.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, 0, out temp);
            Matrix.Multiply(ref temp, ref transformation, out transformation);
            Matrix.CreateRotationZ(rotation, out temp);
            Matrix.Multiply(ref transformation, ref temp, out transformation);
            Matrix.CreateTranslation(position.X, position.Y, 0f, out temp);
            Matrix.Multiply(ref transformation, ref temp, out transformation);

            // Get the default glyph here once.
            Glyph? defaultGlyph = null;
            if (DefaultCharacter.HasValue)
                defaultGlyph = _glyphs[DefaultCharacter.Value];

            var currentGlyph = Glyph.Empty;
            var offset = Vector2.Zero;
            var hasCurrentGlyph = false;

			for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];
                if (c == '\r')
                {
                    hasCurrentGlyph = false;
                    continue;
                }

                if (c == '\n')
                {
                    offset.X = 0;
                    offset.Y += LineSpacing;
                    hasCurrentGlyph = false;
                    continue;
                }

                if (hasCurrentGlyph)
                    offset.X += Spacing + currentGlyph.Width + currentGlyph.RightSideBearing;

                hasCurrentGlyph = _glyphs.TryGetValue(c, out currentGlyph);
                if (!hasCurrentGlyph)
                {
                    if (!defaultGlyph.HasValue)
                        throw new ArgumentException(Errors.TextContainsUnresolvableCharacters, "text");

                    currentGlyph = defaultGlyph.Value;
                    hasCurrentGlyph = true;
                }
                offset.X += currentGlyph.LeftSideBearing;
                var p = offset;

				if (flippedHorz)
                    p.X += currentGlyph.BoundsInTexture.Width;
                p.X += currentGlyph.Cropping.X;

				if (flippedVert)
                    p.Y += currentGlyph.BoundsInTexture.Height - LineSpacing;
                p.Y += currentGlyph.Cropping.Y;

				Vector2.Transform(ref p, ref transformation, out p);

                var destRect = new Vector4( p.X, p.Y, 
                                            currentGlyph.BoundsInTexture.Width * scale.X,
                                            currentGlyph.BoundsInTexture.Height * scale.Y);

                // TODO: We're passing SpriteEffects thru here unchanged, but
                // it seems we're applyting the flips ourselves above.
                //
                // This just might be a bug!

				spriteBatch.DrawInternal(
                    _texture, destRect, currentGlyph.BoundsInTexture,
					color, rotation, Vector2.Zero, effect, depth);
			}
		}
All Usage Examples Of Microsoft.Xna.Framework.Graphics.SpriteBatch::DrawInternal