draw() public method

public draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle destinationRectangle ) : void
texture Microsoft.Xna.Framework.Graphics.Texture2D
destinationRectangle Microsoft.Xna.Framework.Rectangle
return void
Example #1
0
        void drawLine(Batcher batcher, Vector2 start, Vector2 end, Color color, float thickness = 2f)
        {
            var delta = end - start;
            var angle = (float)Math.Atan2(delta.Y, delta.X);

            batcher.draw(Graphics.instance.pixelTexture, start + Entity.transform.position + localOffset, Graphics.instance.pixelTexture.sourceRect, color, angle, new Vector2(0, 0.5f), new Vector2(delta.Length(), thickness), SpriteEffects.None, layerDepth);
        }
Example #2
0
 public static void drawRect(this Batcher batcher, float x, float y, float width, float height, Color color)
 {
     _tempRect.X      = (int)x;
     _tempRect.Y      = (int)y;
     _tempRect.Width  = (int)width;
     _tempRect.Height = (int)height;
     batcher.draw(Graphics.instance.pixelTexture, _tempRect, Graphics.instance.pixelTexture.sourceRect, color);
 }
Example #3
0
        public static void drawPixel(this Batcher batcher, Vector2 position, Color color, int size = 1)
        {
            var sourceRect = Graphics.instance.pixelTexture.sourceRect;

            if (size != 1)
            {
                position.X        -= size * 0.5f;
                position.Y        -= size * 0.5f;
                sourceRect.Width  *= size;
                sourceRect.Height *= size;
            }
            batcher.draw(Graphics.instance.pixelTexture, position, sourceRect, color);
        }
 public static void drawLineAngle(this Batcher batcher, Vector2 start, float radians, float length, Color color)
 {
     batcher.draw(
         Graphics.instance.pixelTexture,
         start,
         Graphics.instance.pixelTexture.sourceRect,
         color,
         radians,
         Vector2.Zero,
         new Vector2(length, 1),
         SpriteEffects.None,
         0);
 }
Example #5
0
 public static void drawRect(this Batcher batcher, Rectangle rect, Color color)
 {
     _tempRect = rect;
     batcher.draw(Graphics.instance.pixelTexture, rect, Graphics.instance.pixelTexture.sourceRect, color);
 }
Example #6
0
        public void drawInto(
            Batcher batcher,
            ref FontCharacterSource 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;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = _font.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.
            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.createTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.createScale(flippedHorz ? -scale.X : scale.X, flippedVert ? -scale.Y : scale.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.createRotation(rotation, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(position.X, position.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

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

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = requiresTransformation ? Vector2.Zero : position - origin;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X         = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

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

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X        += Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                var p = offset;

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

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

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.transform(ref p, ref _transformationMatrix, out p);
                }

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

                batcher.draw(
                    _font.Texture,
                    destRect,
                    currentGlyph.BoundsInTexture,
                    color,
                    rotation,
                    Vector2.Zero,
                    effect,
                    depth);

                offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
            }
        }
Example #7
0
		void drawLine( Batcher batcher, Vector2 start, Vector2 end, Color color, float thickness = 2f )
		{
			var delta = end - start;
			var angle = (float)Math.Atan2( delta.Y, delta.X );
			batcher.draw( Graphics.instance.pixelTexture, start + entity.transform.position + localOffset, Graphics.instance.pixelTexture.sourceRect, color, angle, new Vector2( 0, 0.5f ), new Vector2( delta.Length(), thickness ), SpriteEffects.None, layerDepth );
		}
Example #8
0
		public void drawInto( Batcher batcher, ref FontCharacterSource 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;
					flipAdjustment.X = -size.X;
				}

				if( flippedVert )
				{
					origin.Y *= -1;
					flipAdjustment.Y = _font.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.
			var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;
			if( requiresTransformation )
			{
				Matrix2D temp;
				Matrix2D.createTranslation( -origin.X, -origin.Y, out _transformationMatrix );
				Matrix2D.createScale( ( flippedHorz ? -scale.X : scale.X ), ( flippedVert ? -scale.Y : scale.Y ), out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
				Matrix2D.createTranslation( flipAdjustment.X, flipAdjustment.Y, out temp );
				Matrix2D.multiply( ref temp, ref _transformationMatrix, out _transformationMatrix );
				Matrix2D.createRotation( rotation, out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
				Matrix2D.createTranslation( position.X, position.Y, out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
			}

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

			var currentGlyph = SpriteFont.Glyph.Empty;
			var offset = requiresTransformation ? Vector2.Zero : position - origin;
			var firstGlyphOfLine = true;

			for( var i = 0; i < text.Length; ++i )
			{
				var c = text[i];

				if( c == '\r' )
					continue;

				if( c == '\n' )
				{
					offset.X = requiresTransformation ? 0f : position.X - origin.X;
					offset.Y += _font.LineSpacing;
					firstGlyphOfLine = true;
					continue;
				}

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

					currentGlyph = defaultGlyph.Value;
				}

				// The first character on a line might have a negative left side bearing.
				// In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
				// so that text does not hang off the left side of its rectangle.
				if( firstGlyphOfLine )
				{
					offset.X += Math.Max( currentGlyph.LeftSideBearing, 0 );
					firstGlyphOfLine = false;
				}
				else
				{
					offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
				}

				var p = offset;

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

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

				// transform our point if we need to
				if( requiresTransformation )
					Vector2Ext.transform( ref p, ref _transformationMatrix, out p );

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

				batcher.draw( _font.Texture, destRect, currentGlyph.BoundsInTexture, color, rotation, Vector2.Zero, effect, depth );

				offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
			}
		}